Found 9150 Articles for Object Oriented Programming

How to left align components vertically using BoxLayout with Java?

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

3K+ Views

To align components vertically, use the BoxLayout −JFrame frame = new JFrame(); frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));Now, create a Panel and add some buttons to it. After that set left alignment of components which are already arranged vertically using Component.LEFT_ALIGNMENT constant −JPanel panel = new JPanel(); JButton btn1 = new JButton("One"); JButton btn2 = new JButton("Two"); JButton btn3 = new JButton("Three"); JButton btn4 = new JButton("Four"); JButton btn5 = new JButton("Five"); panel.add(btn1); panel.add(btn2); panel.add(btn3); panel.add(btn4); panel.add(btn5); panel.setAlignmentX(Component.LEFT_ALIGNMENT);The following is an example to left align components vertically with BoxLayout −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; ... Read More

How to center align component using BoxLayout with Java?

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

7K+ Views

Let us first create a panel and set some buttons −JPanel panel = new JPanel(); JButton btn1 = new JButton("One"); JButton btn2 = new JButton("Two"); JButton btn3 = new JButton("Three"); JButton btn4 = new JButton("Four"); JButton btn5 = new JButton("Five"); panel.add(btn1); panel.add(btn2); panel.add(btn3); panel.add(btn4); panel.add(btn5);Now, use the setAlignmentX() and within that specify alignment to the center of the component −panel.setAlignmentX(Component.CENTER_ALIGNMENT);The following is an example to center align component using BoxLayout −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) {       ... Read More

How to display JTextArea in the form of a table with GridLayout in Java?

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

760 Views

Display a component in the form of rows and columns using the GridLayout. Here, we have set a panel, within which we will create a layout with 3 rows and 5 columns −JPanel panel = new JPanel(new GridLayout(3, 5, 5, 5));Now, loop through and display JTextArea from 1 to 15 i.e. 3 rows and 5 columns −for (int i = 1; i

How to display labels in the form of a 4 column table with GridLayout in Java?

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

919 Views

We will displays labels in a label with 5 rows and 4 columns using GridLayout −JPanel panel = new JPanel(new GridLayout(5, 4, 10, 10));Use a for loop and loop through 1 to 20 to display 20 labels −for (int i = 1; i

Java program to create three vertical columns with equal number of buttons in GridLayout

Nishtha Thakur
Updated on 18-Nov-2024 22:29:08

362 Views

In this article, we will learn how to create a Java program that arranges buttons in three vertical columns with an equal number of buttons using GridLayout. The GridLayout class allows us to arrange components in a grid format, which makes it ideal for creating a uniform layout of buttons. Problem StatementGiven a set of 12 buttons, we need to create a Java Swing application that organizes these buttons into three vertical columns. Each column should contain an equal number of buttons.Input No direct input is required from the user. The program will automatically generate 12 buttons labeled as ... Read More

How to create vertical button column with GridLayout in Java?

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

2K+ Views

To create a vertical button column, let us first create some buttons and set the layout as well −JPanel btnPanel = new JPanel(new GridLayout(10, 1, 10, 5)); btnPanel.add(new JButton("First Button")); btnPanel.add(new JButton("Second Button")); btnPanel.add(new JButton("Third Button")); btnPanel.add(new JButton("Fourth Button")); btnPanel.add(new JButton("Fifth Button")); btnPanel.add(new JButton("Sixth Button")); btnPanel.add(new JButton("Seventh Button")); btnPanel.add(new JButton("Eighth Button"));Above, we have set the GridLayout to create rows and columns with vertical and horizontal gap.The following is an example to create vertical button column with GridLayout −Examplepackage my; import java.awt.BorderLayout; import java.awt.GridBagLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class SwingDemo {    public static ... Read More

Java Program to create a layout using GridBagLayout

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

247 Views

The GridBagLayout creates a grid bag layout manager. It arranges the components in a horizontal and vertical manner.Here, we have a frame and panel. The panel has two components arranged using GridBagLayout −JFrame frame = new JFrame("Demo Frame"); JPanel panel = new JPanel(); JLabel label = new JLabel("Email-Id: "); JTextArea text = new JTextArea(); text.setText("Add id here..."); panel.setLayout(new GridBagLayout());Now, set the components to the panel −panel.add(label); panel.add(text);The following is an example to create a layout using GridBagLayout −Examplepackage my; import java.awt.GridBagLayout; import javax.swing.BorderFactory; 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 ... Read More

How to work with border layout position options in Java?

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

312 Views

Let us see some examples to work with different border layout position options such as PAGE_START, PAGE_END, etc.The following is an example of BorderLayout.PAGE_START option −Examplepackage my; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JToolBar; public class SwingDemo {    public static void main(String[] args) {       JPanel panel = new JPanel(new BorderLayout());       JToolBar toolbar = new JToolBar();       panel.add(toolbar, BorderLayout.PAGE_START);       toolbar.add(new JTextArea(" Add name here"));       toolbar.add(new JButton("Submit Name"));       toolbar.addSeparator();       toolbar.add(new JTextArea(" Add age here"));     ... Read More

How to align multiple buttons with different height in Java?

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

559 Views

To align multiple buttons with different height in Java, try the following example, Here, we have set 5 buttons with GridBagConstraints −GridBagConstraints constraints = new GridBagConstraints(); constraints.insets = new Insets(5, 5, 5, 5); constraints.anchor = GridBagConstraints.WEST;In addition, to set different height for different buttons, we have used −component. getPreferredSize().heightThe following is an example to align multiple buttons with different height −Examplepackage my; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       final JFrame frame = new JFrame(SwingDemo.class.getSimpleName());       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JPanel panel = new JPanel(new GridBagLayout());       GridBagConstraints constraints = new GridBagConstraints();       constraints.insets = new Insets(5,  5,  5,  5);       constraints.anchor = GridBagConstraints.WEST;     ... Read More

How to set all the Arrow Buttons in a frame with Java?

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

315 Views

To set arrow buttons in a frame, let us first create a frame −JFrame frame = new JFrame();Now, set the layout for the frame wherein all the arrow buttons would be displayed −frame.setLayout(new GridLayout(0, 5));Set the arrow buttons for all the locations −frame.add(new BasicArrowButton(BasicArrowButton.EAST)); frame.add(new BasicArrowButton(BasicArrowButton.NORTH)); frame.add(new BasicArrowButton(BasicArrowButton.SOUTH)); frame.add(new BasicArrowButton(BasicArrowButton.WEST));The following is an example to set all the arrow buttons in a frame −Examplepackage my; import java.awt.GridLayout; import javax.swing.Box; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.plaf.basic.BasicArrowButton; public class SwingDemo {    public static void main(String[] args) {       JButton button1 = new JButton("One");     ... Read More

Advertisements