Results 1 to 10 of 10
- 10-12-2009, 10:21 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
I can't make JLabel do what I want (size-wise)
I've tried a variety of different layout managers.
Here's the idea. I want my JLabel to have a fixed maximum horizontal width and a variable height (depending on the textual content).
The text of the JLabel changes in response to user action, and it can theoretically be huge, but in practice probably 1 or 2 lines.
- 10-13-2009, 04:46 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If you want to change the height at runtime you may repaint the container. Did you try that?
- 10-13-2009, 11:17 AM #3
My idea is JLabel has no decorations and it does not react to user input also. It's used only as an identification for other components like JButton or JTextField. ^_^
- 10-13-2009, 12:44 PM #4
Member
- Join Date
- Jun 2008
- Posts
- 56
- Rep Power
- 0
You can calculate number of lines that will be necessary for displaying user input. Then you can use HTML for text rendering and split it with <BR> tags.
Try Controls4J - Advanced Swing Components.
- 10-14-2009, 04:33 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 10-14-2009, 06:28 AM #6
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
My other reply didn't seem to happen.
I thought perhaps there would be an easy method, but if inspecting the text and inserting line breaks is necessary then it's not that big of a deal. I just didn't want to something like that and find out there was a really simple solution.
- 10-14-2009, 01:53 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you try-out that use of line break in your application?
-
Have you considered using a JTextArea here instead of a JLabel? Just a thought.
- 10-14-2009, 09:56 PM #9
Member
- Join Date
- Sep 2009
- Posts
- 8
- Rep Power
- 0
I ended up doing this:
I like my solution for now. I think it works better with the current GUI. However, I am deliberating changing the whole GUI completely at this point so I am not thinking that hard about it anymore.Java Code:public class ConstrainedLabel extends JLabel { public ConstrainedLabel(String text) { setText(text); } @Override public void setText(String text) { super.setText(text); super.setToolTipText(text); } @Override public String getText() { String text = super.getText(); if (text.length() > 40) { text = text.substring(0,40); text = text + "..."; } return text; } }
- 10-15-2009, 04:41 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Hmmm, that's not a bad thing. You just display part of the string in the label and display the complete content as a tool-tip. Changing the complete UI at this point is a mess for you. ;)
Similar Threads
-
Bit Wise operation: shifting
By rueter in forum New To JavaReplies: 1Last Post: 06-27-2009, 06:51 AM -
Setting frame size to the size of an image
By Yoruichi in forum AWT / SwingReplies: 5Last Post: 04-22-2009, 04:37 PM -
How rmi preferable than http for Performance wise??
By Kishore Kumar in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 03-23-2009, 12:18 PM -
Make a text in Jlabel down to next Line
By hungleon88 in forum AWT / SwingReplies: 2Last Post: 12-01-2008, 11:10 PM -
jLabel
By Matty in forum AWT / SwingReplies: 3Last Post: 09-22-2008, 10:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks