• Post Reply Bookmark Topic Watch Topic
  • New Topic

JLabel words move?  RSS feed

 
Janeice DelVecchio
Bartender
Posts: 1810
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty new at this.... I'm working on this project for school..... and starting by setting up the GUI it needs.

When I type in the odometer, cost and gallons fields, the label moves with the cursor...... what the heck??



I'd ultimately like the labels to be to the LEFT of the text areas.... and the text areas to be only one character high.... I can look that stuff up. I'm really mostly confused at the behavior of the labels when I type.

/Janeice
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the section from the How to Use Box Layout. It does some strange things because components can have different default horizontal alignments. So the simple answer is make sure the alignment is the same for all components.

Also, you should not be using a JTextArea for those fields. You should be using a JTextField. You will still get some wierd sizing of the components because of the way a BoxLayout handles sizing of components. The easy solution is to use:

textField.setMaximumSize( textField.getPreferredSize() );
 
Janeice DelVecchio
Bartender
Posts: 1810
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply and the link....

I was using the JTextField for those three components but it did (as you said) show up weird. I thought by changing it to JTextArea and setting the height parameter to "1" it would show up one character high.... it didn't and I didn't get around to changing it back before I realized the labels were moving and freaked out.
 
Janeice DelVecchio
Bartender
Posts: 1810
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used the textField.setMaximumSize as suggested.

Worked really well for the sizing and also for the moving JLabels.... my original thought was that it would work for the sizing but not the justification.....

Not like I'm complaining, but WHY did it work for both?

/Janeice/
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know for sure why it does what it does.

As I suggested in my first reply did you check the alignment of each of the components to see what the difference might be?
 
Janeice DelVecchio
Bartender
Posts: 1810
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The site says:

By default, most components have center X and Y alignment. However, buttons, combo boxes, labels, and menu items have a different default X alignment value: LEFT_ALIGNMENT.


I think it's weird that when I set the maximum size it stopped the labels from moving. Everything showed up left justified when I made that change.

--Janeice
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!