Results 1 to 13 of 13
  1. #1
    whiteMath is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default BoxLayout Alignment problem

    Hi!

    I used the setAlignmentX() method in a component with BoxLayout-Y-AXIS in my program, but it works definitely bad. It puts the JButtons hardly ever to the right place. What did i wrong? Please check my code!

    Java Code:
    public class BAT  extends JFrame{
    
    	public BAT() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setPreferredSize(new Dimension(180,120));
    		Container pane = getContentPane();
    		pane.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS));
    		
    		JButton button1 = new JButton("left");
    		button1.setAlignmentX(Component.LEFT_ALIGNMENT);
    		add(button1);
    		JButton button2 = new JButton("center");
    		button2.setAlignmentX(Component.CENTER_ALIGNMENT);
    		add(button2);
    		JButton button3 = new JButton("right");
    		button3.setAlignmentX(Component.RIGHT_ALIGNMENT);
    		add(button3);
    		
    		pack();
    	}
    	
    	public static void main(String[] args) {
    		BAT bat = new BAT();
    		bat.setVisible(true);
    	}
    
    }
    Last edited by whiteMath; 03-24-2010 at 11:08 AM.

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Personally, I was never EVER able to get the alignmentX thing to work, and not for the lack of trying... What I do in these situations is add JPanels instead of Buttons, and have these JPanels use either BorderLayout (the only one in Java that REALLY works) or BoxLayout with Box.createHorizontalGlue () kind of thing (also doesn't ALWAYS work)...

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

    Default

    It puts the JButtons hardly ever to the right place.
    We are not mind readers. Define what "right place" means to you.

  4. #4
    whiteMath is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    We are not mind readers. Define what "right place" means to you.
    I mean, if i use LEFT_ALIGNMENT, RIGHT_ALIGNMENT, CENTER_ALIGNMENT in calling the setAlignmentX() function, i would like the Component (JButton in my example) to appear respectively at the left, at the right part or at the centre of the given container. In the above exapmle, only the CENTRE_ALIGNMENT works fine for me.

    Sorry for unclear composition, and possible bad grammar. I try to do my best.

  5. #5
    whiteMath is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks! These issues can really be fixed that way.

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

    Default

    Read the Box Layout Features section from the Swing tutorial for a better explanation on how the alignment parameters work.

  7. #7
    whiteMath is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Ok. I have read it again, more carefully than I did at the first time. The setAlignmentX() functions purpose really differs from my previous expectations.

  8. #8
    schmoe is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    Read the Box Layout Features section from the Swing tutorial for a better explanation on how the alignment parameters work.


    Ok. I have read it again, more carefully than I did at the first time. The setAlignmentX() functions purpose really differs from my previous expectations.

    Like iluxa, I have never gotten this to work. I have a Y_AXIS BoxLayout, and I want to layout two check boxes so that they align to the left. So, I call setAlignmentX(Component.LEFT_ALIGNMENT) on each checkbox, yet the checkboxes appear centered when run.

    From How to Use BoxLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    In the first figure, all three components have an X alignment of 0.0 (Component.LEFT_ALIGNMENT). This means that the components' left sides should be aligned. Furthermore, it means that all three components are positioned as far left in their container as possible.

    So why aren't my checkboxes aligned as far left as possible?

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    1. Get your own thread, don't hijack an old one.
    2. To get better help sooner, post a SSCCE : Java Glossary and don't forget to use the code tags.

    db

  10. #10
    schmoe is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    a) This is not a hijack. This is the exact topic I want.
    b) The forum rules state:
    - To avoid multiple posts on the same topic, please do a search on your topic. Your question might have been answered before.
    - Please do not start more than one topic (in the same OR different forums) with the same question or message. Read the forum descriptions and pick the most appropriate forum, and start your topic there ONLY. Starting multiple topics with the same question or message is confusing, splits up the discussion / answers and makes a moderator's job harder. Please resist all temptation to cross post.

    d) The resolved answer was never clearly expressed - "purpose really differs from my previous expectations" - for the rest of us, and there were clearly several who were curious.

    e) A short Short Self-Contained Compilable Example will be forthcoming.

  11. #11
    whiteMath is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Hi!

    I dont really remember things, because i use c# nowadays so I cant give you an exact answer, but i started the thread... The boxlayout on the bases of its contents' alignment somehow (its not really clear for me how) caluculates a baseline, and the other alignments are set on the bases of this baseline. If I put every content to the left (I checked the checkboxes as well), or to the same alignment it works for me. Check the working of the BoxLayoutDemo.
    You can use inner containers for mixed alignment.

    whiteMath

  12. #12
    schmoe is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    I wanted a simple BoxLayout control that aligned all the components to the left. When I run the sample code below, the checkboxs were indented and not aligned to the left with the list box.

    I've seen this problem reported several times on this forum and others, so I am ready to conclude that it can't be done without a work around. You either need to use a different layout manager or wrap each control in its own JPanel. That ruins the simplicity of the BoxLayout. I ended up using TableLayout.


    Java Code:
    public class Test extends JFrame {
    	
    	public Test() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		Container pane = getContentPane();
    		pane.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS));
    
    		JCheckBox chkButton1 = new JCheckBox("checkbox 1");
    		chkButton1.setAlignmentX(Component.LEFT_ALIGNMENT);
    		pane.add(chkButton1);
    
    		JCheckBox chkButton2 = new JCheckBox("checkbox 2");
    		chkButton2.setAlignmentX(Component.LEFT_ALIGNMENT);
    		pane.add(chkButton2);
    		
    		JCheckBox chkButton3 = new JCheckBox("checkbox 3");
    		chkButton3.setAlignmentX(Component.LEFT_ALIGNMENT);
    		pane.add(chkButton3);
    
    		JList list = new JList(new Object [] { "List box that is far wider than the check boxes.",
    						   "Be good to your car so your car will be good to you.",
    						   "By this point, you get this gist of the list box"});
    		list.setAlignmentX(Component.LEFT_ALIGNMENT);
    		pane.add(new JScrollPane(list));
    
    		pack();
    	}
    
    	public static void main(String[] args) {
    		Test test = new Test();
    		test.setVisible(true);
    	}
    
    }

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

    Default

    When I run the sample code below, the checkboxs were indented and not aligned to the left with the list box.
    Well the problem is that you didn't add the JList to the content pane. You added a JScrollPane and you didn't set the alignment of the scrollpane so it defaulted to center.

    Java Code:
    //		list.setAlignmentX(Component.LEFT_ALIGNMENT);
    //		pane.add(new JScrollPane(list));
    		JScrollPane scrollPane = new JScrollPane(list);
    		scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
    		pane.add(scrollPane);

Similar Threads

  1. GridLayout alignment problem
    By q8inq8 in forum New To Java
    Replies: 9
    Last Post: 03-16-2010, 07:54 PM
  2. Alignment using BoxLayout
    By tiptoe in forum AWT / Swing
    Replies: 4
    Last Post: 01-23-2010, 07:15 PM
  3. BoxLayout problems
    By paulb in forum New To Java
    Replies: 2
    Last Post: 11-04-2009, 10:04 PM
  4. BoxLayout Behaviour
    By PetalumaBoy in forum AWT / Swing
    Replies: 4
    Last Post: 06-10-2009, 01:27 PM
  5. [SOLVED] alignment problem
    By nanimtech in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-10-2008, 01:23 PM

Posting Permissions

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