Results 1 to 13 of 13
Thread: BoxLayout Alignment problem
- 03-23-2010, 05:43 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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.
- 03-23-2010, 07:30 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
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)...
- 03-23-2010, 08:17 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
We are not mind readers. Define what "right place" means to you.It puts the JButtons hardly ever to the right place.
- 03-23-2010, 10:51 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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.
- 03-24-2010, 12:23 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
Thanks! These issues can really be fixed that way.
- 03-24-2010, 02:45 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Read the Box Layout Features section from the Swing tutorial for a better explanation on how the alignment parameters work.
- 03-24-2010, 11:04 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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.
- 07-16-2010, 11:20 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
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?
- 07-17-2010, 01:41 PM #9
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
- 07-17-2010, 03:55 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
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.
- 07-19-2010, 07:25 PM #11
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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
- 07-19-2010, 09:04 PM #12
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
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); } }
- 07-19-2010, 10:04 PM #13
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.When I run the sample code below, the checkboxs were indented and not aligned to the left with the list box.
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
-
GridLayout alignment problem
By q8inq8 in forum New To JavaReplies: 9Last Post: 03-16-2010, 07:54 PM -
Alignment using BoxLayout
By tiptoe in forum AWT / SwingReplies: 4Last Post: 01-23-2010, 07:15 PM -
BoxLayout problems
By paulb in forum New To JavaReplies: 2Last Post: 11-04-2009, 10:04 PM -
BoxLayout Behaviour
By PetalumaBoy in forum AWT / SwingReplies: 4Last Post: 06-10-2009, 01:27 PM -
[SOLVED] alignment problem
By nanimtech in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 04-10-2008, 01:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks