Found 9150 Articles for Object Oriented Programming

How to set vertical alignment for a component in Java?

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

4K+ Views

For vertical alignment, create a frame and set the layout using the BoxLayout manager −JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));Above, we have set the BoxLayout to set the alignment since it is a layout manager that allows multiple components to be laid out either vertically or horizontally. We have set vertical alignment here −BoxLayout.X_AXISThe following is an example to set vertical alignment for a component −Examplepackage my; import java.awt.Component; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame ... Read More

How to create a Borderless Window in Java?

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

632 Views

To create a borderless window in Java, do not decorate the window. The following is an example to create a BorderLess Window −Examplepackage my; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; import java.awt.Point; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.JWindow; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) throws Exception {       JWindow frame = new JWindow();       JLabel label1, label2, label3;       frame.setLayout(new GridLayout(2, 2));       label1 = new JLabel("Id", SwingConstants.CENTER);       label2 = new JLabel("Age", SwingConstants.CENTER);       label3 = new JLabel("Password", SwingConstants.CENTER);     ... Read More

Set a component and place it next to the previously added component with GridBagLayout in Java

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

150 Views

We have set a component first −GridBagConstraints constraints = new GridBagConstraints(); constraints.gridy = 0; panel.add(new JButton("First row"), constraints);Now, we will place it next to the previously added component −constraints.gridx = GridBagConstraints.RELATIVE; constraints.gridy = 1; panel.add(new JButton("2nd row 1st column"), constraints); panel.add(new JButton("2nd row 2nd column"), constraints);Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JPanel panel = new JPanel();       panel.setLayout(new GridBagLayout());       GridBagConstraints constraints = new ... Read More

How to add components with a Relative X Position in Java

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

623 Views

To add components with a relative X position, use the GridBagConstraints.RELATIVE constant. Set this to gridx field −GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = GridBagConstraints.RELATIVE;The following is an example to add components with a relative X position in Java −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JPanel panel = new JPanel();       panel.setLayout(new GridBagLayout());       GridBagConstraints constraints = new GridBagConstraints();       constraints.gridy = ... Read More

How to disallow resizing component with GridBagLayout in Java

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

817 Views

To disallow resizing component with GridBagLayout, use the GridBagConstraints NONE constant −GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.NONE;The following is an example to disallow resizing component with 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");       JPanel panel = new JPanel();       GridBagLayout layout = new GridBagLayout();       panel.setLayout(layout);       GridBagConstraints gbc = new GridBagConstraints();       gbc.fill = GridBagConstraints.NONE;   ... Read More

Distribute extra horizontal and vertical space in a GridBagLayout with Java

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

773 Views

To distribute the extra horizontal and vertical space, use the fields weightx and weighty. The following is an example to distribute extra horizontal and vertical space −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");       JPanel panel = new JPanel();       GridBagLayout layout = new GridBagLayout();       panel.setLayout(layout);       GridBagConstraints gbc = new GridBagConstraints();       JLabel label = new JLabel("Rank: ");   ... Read More

How to set columnWeights and rowWeights in a GridBagLayout?

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

211 Views

Create a panel and set the layout −JPanel panel = new JPanel(); GridBagLayout layout = new GridBagLayout(); panel.setLayout(layout);Now, set the constrainst and with that the columnWeights and rowWeights −GridBagConstraints gbc = new GridBagConstraints(); JLabel label = new JLabel("Rank: "); JTextArea text = new JTextArea(); text.setText("Add rank here..."); layout.columnWeights = new double[]{0.0f, 0.0f, 2.0f}; layout.rowWeights = new double[]{0.0f, 1.0f};Now, set the constraints for the label and add it to the panel −gbc.gridx = 0; gbc.gridy = 0; layout.setConstraints(label, gbc); panel.add(label);The following is an example to set columnWeights and rowWeights in a GridBagLayout −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JFrame; import ... Read More

How to center a Window in Java?

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

1K+ Views

To center a Window in Java, use the getCenterPoint() method. Set the width and height and use the below formulae to set bounds −Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); int width = 500; int height = 200; frame.setBounds(center.x - width / 2, center.y - height / 2, width, height);The following is an example to center a window −Examplepackage my; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; import java.awt.Point; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame.setDefaultLookAndFeelDecorated(true);       JFrame frame = new JFrame("Register!");     ... Read More

How to set preferred size for BoxLayout Manager in Java?

Nishtha Thakur
Updated on 29-Oct-2024 00:37:55

2K+ Views

In this article, we will learn to set the preferred size for a panel within a JFrame using the BoxLayout manager in Java Swing. By setting the preferred and maximum size for the panel, we can control the layout’s appearance, ensuring that the panel maintains a consistent size as other components are added. This approach is useful when arranging elements vertically or horizontally in a structured format while having specific size requirements. Steps to set preferred size for BoxLayout Manager The following are the steps for setting the preferred size for BoxLayout Manager in Java − ... Read More

How to combine FlowLayout and BoxLayout in Java?

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

620 Views

To combine both the layouts, here we have created two panels −Frame f = new JFrame("Combining Layouts"); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel();Now, set the layouts accordingly −panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS)); panel2.setLayout(new FlowLayout());Then after adding components to both the panels, add them to the frame −f.add(panel1, BorderLayout.WEST); f.add(panel2, BorderLayout.CENTER);Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; public class SwingDemo {    public static void main(String[] args) {       JFrame f = new JFrame("Combining Layouts");       JPanel panel1 = new JPanel();       ... Read More

Advertisements