Results 1 to 6 of 6
- 12-19-2011, 10:44 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
how can i arrange the component to the left side in Boxlayout?
Hi Everybody,
I am confused with BoxLayout...
I have 2 components on my frame. (a button , and a table) And I used BoxLayout to arrange these components.
When I run my application, I noticed, my button was misaligned. It seems it was randomly placed.
I would like to put that button to the left side of the panel.
How can I achive this? Might I omit anything from my settings ?
May I use nested panels?
Thanks in advance for your help.
here is my simplified code
Java Code:import javax.swing.*; import javax.swing.table.*; import java.awt.event.*; import java.awt.*; public class TableData extends JFrame implements ActionListener{ String[] header = {"name", "gift", "piece"}; DefaultTableModel model = new DefaultTableModel(header, 3); JTable table = new JTable(model); JPanel panel = new JPanel(); JButton buttonOK = new JButton("OK"); public TableData() { panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS)); panel.add(buttonOK); buttonOK.addActionListener(this); buttonOK.setAlignmentX(Component.LEFT_ALIGNMENT); panel.add(new JScrollPane(table)); getContentPane().add(panel); setSize(400,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Christmas gifts"); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("OK")) { System.out.println("ok"); } } public static void main(String[] args) { new TableData(); } }
- 12-19-2011, 11:23 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: how can i arrange the component to the left side in Boxlayout?
The following left aligns the button with the table:I would like to put that button to the left side of the panel.
Basically you nest box containers and add glue and struts to taste. I've found this a good recipe.Java Code:panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS)); Box box = Box.createHorizontalBox(); /* suggested in http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#alignment buttonOK.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(Color.red), buttonOK.getBorder())); */ box.add(buttonOK); box.add(Box.createHorizontalGlue()); panel.add(box); buttonOK.addActionListener(this); //buttonOK.setAlignmentX(Component.LEFT_ALIGNMENT);
This sort of question ought to be posed in the Swing forum to better catch the eye of one of the Swing experts who can explain why setAlignmentX() failed to. (In the past I've been caught out by not appreciating the impact of the different sizes of a component: actual, min, max and preferred.)
- 12-20-2011, 01:55 AM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: how can i arrange the component to the left side in Boxlayout?
See: How to Use BoxLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container) to better understand how alignment works.
- 12-20-2011, 02:34 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: how can i arrange the component to the left side in Boxlayout?
As shown in the code I posted, that's where I went! But when am I going to learn how to read...? I managed to convince myself it was a size problem.
@OP: there is a picture there -
The numbers are the alignments: LEFT on top of CENTRE on top of RIGHT. The image is called wacky.png. Indeed. But the text at the start of the page explains how it gets to be that way. And in the bit camickr linked to they offer the advice "In general, all the components controlled by a top-to-bottom BoxLayout object should have the same X alignment".
That whole page is worth reading. Twice.Java Code:panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS)); panel.add(buttonOK); buttonOK.addActionListener(this); buttonOK.setAlignmentX(Component.LEFT_ALIGNMENT); JScrollPane scroll = new JScrollPane(table); scroll.setAlignmentX(Component.LEFT_ALIGNMENT); // <----- panel.add(scroll); getContentPane().add(panel);
- 12-25-2011, 08:36 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
Re: how can i arrange the component to the left side in Boxlayout?
Very thanks your reply, it solved my problem. I tried out and tested the your mentioned Box class and its methods, and it really makes better the visualization.
Furthermore I tested the createRigidArea(Dimension d) method to let space between components.
Thanks again.
- 12-25-2011, 09:03 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Left side bar is not just irritating, its frustrating.
By Addez in forum Suggestions & FeedbackReplies: 7Last Post: 05-26-2011, 09:01 AM -
Reversing dojo slider to make it slide from right to left rather left to right as giv
By gurpreet.singh in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 05-05-2011, 01:49 PM -
square moves left and down but not up or left
By natdizzle in forum AWT / SwingReplies: 3Last Post: 02-04-2011, 05:20 PM -
arrange(java.lang.String[][]) in Test cannot be applied to (java.lang.String) arrange
By prizzly in forum New To JavaReplies: 4Last Post: 01-01-2011, 10:52 AM -
Right-side for left curly braces
By zweibieren in forum AWT / SwingReplies: 5Last Post: 11-02-2009, 08:08 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks