Found 4338 Articles for Java 8

Set the location of component in a GridBagLayout with Java

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

389 Views

To set the location of component, use the GridBagConstraints. Here, we have two components −GridBagConstraints gbc = new GridBagConstraints(); JLabel label = new JLabel("Email-Id − "); JTextArea text = new JTextArea(); text.setText("Add id here...");Set the location with gridx and gridy −gbc.gridx = 0; gbc.gridy = 0; layout.setConstraints(label, gbc); panel.add(label);The following is an example to set the location of component in a GridBagLayout −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Demo Frame");     ... Read More

How to create a GridLayout with rows and columns in Java?

George John
Updated on 30-Jul-2019 22:30:26

642 Views

While creating a GridLayout, you need to set the rows and columns as parenthesis. A GridLayout is used to create a layout the specified number of rows and columns.Let’s say we have a GridLayout, with 1 row and 4 columns −GridLayout layout = new GridLayout(1, 4);The following is an example to create a GridLayout with rows and columns −Examplepackage my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new ... Read More

How to create Horizontal Slider in Java?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

394 Views

To create Horizontal slider in Java, use the Swing JSlider. Let us first create a frame and a Horizontal slider in it −JFrame frame = new JFrame("Frame with Slider"); JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 70);Now, we will set the values for the slider. Display the ticks −slider.setMinorTickSpacing(5); slider.setMajorTickSpacing(20); slider.setPaintTicks(true); slider.setPaintLabels(true); Add the slider in the panel: JPanel panel = new JPanel(); panel.add(slider);The following is an example to create horizontal slider −Examplepackage my; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new ... Read More

How to create an invisible fixed height component between two components in Java?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

106 Views

Use the createVerticalStrut() method to create an invisible fixed height component between two components. Let’s say we have some button and we are creating a fixed height between them −box.add(button4); box.add(Box.createVerticalStrut(50)); box.add(button5); box.add(Box.createVerticalStrut(30)); box.add(button6);The following is an example to create an invisible fixed height component between two components in Java −Examplepackage my; import java.awt.BorderLayout; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Groups");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JButton button1 = new JButton("CSK");       JButton button2 ... Read More

How to create Glue to fill the space between neighbouring components in Java?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

390 Views

Let’s say we have 6 components and we need to fill the space between some of them −JButton button1 = new JButton("CSK"); JButton button2 = new JButton("DC"); JButton button3 = new JButton("MI"); JButton button4 = new JButton("SRH"); JButton button5 = new JButton("RR"); JButton button6 = new JButton("KKR");To fill the space and separate components, create a Glue using the createGlue() method −Box box = new Box(BoxLayout.X_AXIS); box.add(button1); box.add(button2); box.add(Box.createGlue()); box.add(button3); box.add(button4); box.add(Box.createGlue()); box.add(button5); box.add(button6);The following is an example to fill the space between neighbouring components −Examplepackage my; import java.awt.BorderLayout; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class ... Read More

Java Program to get the count of child nodes of any node in JTree

Anvi Jain
Updated on 30-Jul-2019 22:30:26

656 Views

Use the getChildCount() method in Java to get the count of child nosed of any node i.e. even if it’s a root node or not.Let’s say we want the count of child nodes for node 1, which is not a root node, therefore −node1.getChildCount()The following is an example to get the count of child nodes of any node −Examplepackage my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Website");       DefaultMutableTreeNode node1 ... Read More

How to create a Box Layout in Java?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

2K+ Views

To create a Box Layout in Java Swing, use the BoxLayout class. Here, we have also set that the components should be laid our left to right or top to bottom −Box box = new Box(BoxLayout.X_AXIS); box.add(button1); box.add(button2); box.add(button3); box.add(button4); box.add(Box.createGlue()); box.add(button5); box.add(button6); box.add(button7); box.add(button8);Above, we have 8 buttons in our Box Layout. We have separated 4 buttons each using the createGlue() method.The following is an example to create a BoxLayout in Java −Examplepackage my; import java.awt.BorderLayout; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String args[]) {     ... Read More

How to separate Components in a Row or Column with Box in Java

George John
Updated on 30-Jul-2019 22:30:26

243 Views

To separate components in a row or column, use the createGlue() method. This creates an invisible "glue" component that separates components.The following is an example to separate components in a row or column with Box −Examplepackage my; import java.awt.BorderLayout; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Matches");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JButton button1 = new JButton("CSK");       JButton button2 = new JButton("DC");       JButton button3 = new JButton("MI");       JButton ... Read More

Java Program to determine when a Frame or Window is closing in Java

Chandu yadav
Updated on 30-Jul-2019 22:30:26

1K+ Views

To determine when a Window is closing in Java, use the WindowListener, which is the listener interface for receiving window events..WindowListener listener = new WindowAdapter() {    public void windowClosing(WindowEvent evt) {       Frame frame = (Frame) evt.getSource();       System.out.println("Closing = "+frame.getTitle());    } };Above, we have used the windowClosing() method, which is invoked when a window is in the process of being closed −public void windowClosing(WindowEvent evt) {    Frame frame = (Frame) evt.getSource();    System.out.println("Closing = "+frame.getTitle()); }The following is an example to determine when a Frame or Window is closing in Java −Examplepackage ... Read More

Determine when a Frame or Window is Opened in Java

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

721 Views

To determine when a Window is opened in Java, use the WindowListener, which is the listener interface for receiving window events..WindowListener listener = new WindowAdapter() {    public void windowOpened(WindowEvent evt) {       Frame frame = (Frame) evt.getSource();       System.out.println("Opened "+frame.getTitle());    } };Above, we have used the windowOpened() method, which is invoked when a window has been opened −public void windowOpened(WindowEvent evt) {    Frame frame = (Frame) evt.getSource();    System.out.println("Opened "+frame.getTitle()); }The following is an example to determine when a frame or window is opened in Java −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import ... Read More

Advertisements