Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: how can i arrange the component to the left side in Boxlayout?

  1. #1
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default 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();
    	}
    
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: how can i arrange the component to the left side in Boxlayout?

    I would like to put that button to the left side of the panel.
    The following left aligns the button with the table:

    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);
    Basically you nest box containers and add glue and struts to taste. I've found this a good recipe.

    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.)

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: how can i arrange the component to the left side in Boxlayout?


  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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".

    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);
    That whole page is worth reading. Twice.
    DarrylBurke likes this.

  5. #5
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default 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.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: how can i arrange the component to the left side in Boxlayout?

    You're welcome.

Similar Threads

  1. Left side bar is not just irritating, its frustrating.
    By Addez in forum Suggestions & Feedback
    Replies: 7
    Last Post: 05-26-2011, 10:01 AM
  2. 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 JSTL
    Replies: 1
    Last Post: 05-05-2011, 02:49 PM
  3. square moves left and down but not up or left
    By natdizzle in forum AWT / Swing
    Replies: 3
    Last Post: 02-04-2011, 06:20 PM
  4. Replies: 4
    Last Post: 01-01-2011, 11:52 AM
  5. Right-side for left curly braces
    By zweibieren in forum AWT / Swing
    Replies: 5
    Last Post: 11-02-2009, 09:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •