This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JLabel size  RSS feed

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have created 4 JLabels, and I want them to remain the same size when they are initialized and when data is put into them. My Labels start out with no text. I have tried both setPreferredSize() and setSize() to get the Labels to the size I want, but it doesn't work. When I put a long empty string in the text of the label, they are the size I want. However, once I set the text of them, if the text is not the same size of the label, it shortens the label.
Could someone please help me understand what I am doing wrong?
Thanks in advance!
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might try setMinimumSize() and setMaximumSize() with setPreferredSize().
 
Lon Allen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could always put the current JLabel inside a JPanel and put the new JPanel wherever the current JLabel resides. Set the layout of the new JPanel to BorderLayout and add the JLabel component to the JPanel with Center position. The JLabel should completely fill the JPanel due to this layout. The size should remain whatever size you make the JPanel as long as you don't add any other components to the JPanel (to North, South, East or West).
Lon Allen
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys! I tried setting the maximum size to the preferred size and that worked!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!