Results 1 to 10 of 10
  1. #1
    fletcher is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default 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.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    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. ^_^

  4. #4
    devunion is offline Member
    Join Date
    Jun 2008
    Posts
    56
    Rep Power
    0

    Default

    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.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by devunion View Post
    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.
    This is a good option, I'm using it in most cases. But sometime it didn't wrap within the boarder of the control. So I use a repaint simply.

  6. #6
    fletcher is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    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.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  9. #9
    fletcher is offline Member
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    I ended up doing this:

    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;
        }
    }
    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.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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

  1. Bit Wise operation: shifting
    By rueter in forum New To Java
    Replies: 1
    Last Post: 06-27-2009, 06:51 AM
  2. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 04:37 PM
  3. How rmi preferable than http for Performance wise??
    By Kishore Kumar in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 03-23-2009, 12:18 PM
  4. Make a text in Jlabel down to next Line
    By hungleon88 in forum AWT / Swing
    Replies: 2
    Last Post: 12-01-2008, 11:10 PM
  5. jLabel
    By Matty in forum AWT / Swing
    Replies: 3
    Last Post: 09-22-2008, 10:22 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
  •