Found 9150 Articles for Object Oriented Programming

Customize the JOptionPane layout with updated color and image in Java

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

2K+ Views

Customize the layout by changing the look and feel of the panel in which you added the component −ImageIcon icon = new ImageIcon(new URL("http −//www.tutorialspoint.com/images/C-PLUS.png")); JLabel label = new JLabel(icon); JPanel panel = new JPanel(new GridBagLayout()); panel.add(label); panel.setOpaque(true); panel.setBackground(Color.ORANGE);Above, we have added an image and even updated the background color of the panel.Now, set it for the text panel −JPanel textPanel = new JPanel(new GridLayout(10, 5)); textPanel.setBackground(Color.Magenta);The following is an example to customize the JOptionPane layout −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.net.URL; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; public class SwingDemo { ... Read More

How to apply adjustments to the next column of a JTable only, when the width of a column is changed in Java Swing?

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

189 Views

To apply adjustments to the next column only, use the setAutoResizeMode and set the mode. The mode here would be AUTO_RESIZE_NEXT_COLUMN. This will allow you to adjust only the next column even if any of the column header is dragged to resize.Let us first see an example to create a table −Examplepackage my; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo {    public static void main(String[] argv) throws Exception {       DefaultTableModel tableModel = new DefaultTableModel();       JTable table = new JTable(tableModel);       tableModel.addColumn("Technology");       tableModel.addColumn("BCA"); ... Read More

Set the location of component in a GridBagLayout with Java

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

499 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

889 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

511 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

184 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

583 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 10-Oct-2024 11:38:52

855 Views

In this program, we will create a tree structure using JTree and DefaultMutableTreeNode in Java to represent a hierarchical structure, such as a website. We will then use the getChildCount() method to retrieve and display the number of child nodes for specific nodes in the tree. The example focuses on nodes that are not root nodes and prints the count of child nodes for each. Let’s say we want the count of child nodes for node 1, which is not a root node − node1.getChildCount() Steps to get the count of child nodes of any node in JTree Following are ... Read More

How to create a Box Layout in Java?

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

3K+ 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

302 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

Advertisements