- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a Box to display components from left to right in Java
Box is lightweight container that uses a BoxLayout object as its layout manager. To display components from left to right, use Box createHorizontalBox() method.
Let us first create some button components −
JButton button1 = new JButton("One"); JButton button2 = new JButton("Two"); JButton button3 = new JButton("Three"); JButton button4 = new JButton("Four"); JButton button5 = new JButton("Five"); JButton button6 = new JButton("Six");
Now, crate a Box and align all the buttons from left to right −
Box box = Box.createHorizontalBox(); box.add(button1); box.add(button2); box.add(button3); box.add(button4); box.add(button5); box.add(button6);
The following is an example to create a Box to display components from left to right −
Example
package my; import java.awt.BorderLayout; import javax.swing.Box; 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("Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button1 = new JButton("One"); JButton button2 = new JButton("Two"); JButton button3 = new JButton("Three"); JButton button4 = new JButton("Four"); JButton button5 = new JButton("Five"); JButton button6 = new JButton("Six"); Box box = Box.createHorizontalBox(); box.add(button1); box.add(button2); box.add(button3); box.add(button4); box.add(button5); box.add(button6); JScrollPane jScrollPane = new JScrollPane(); jScrollPane.setViewportView(box); frame.add(jScrollPane, BorderLayout.CENTER); frame.setSize(550, 250); frame.setVisible(true); } }
This will produce the following output −
- Related Articles
- How to create a left-right split pane in Java?
- How to display the JList items from top to bottom and left to right in Java?
- How to display text Right-to-Left Using HTML?
- How to display text Right-to-Left Using CSS?
- How to display only a left and bottom box border in Matplotlib?
- How to create a Box Layout in Java?
- How to arrange components in a FlowLayout to be left-justified in Java?
- How to create JSplitPane to divide components in Java?
- How to arrange components in a Flow to be right-justified in Java?
- How to separate Components in a Row or Column with Box in Java
- How to create a Confirmation Dialog Box in Java?
- How to left align components vertically using BoxLayout with Java?
- Create Left to Right slide animation in Android?
- How can we set the orientation of a JTextArea from right to left in Java?
- How to move the horizontal slider right-to-left in Java?

Advertisements