Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Refresh bounds of jLabel

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Refresh bounds of jLabel

    Hi there.

    I have a custom JTree with a custom TreeCellRenderer. My TreeCellRenderer extends the DefaultTreeCellRenderer from swing.
    Everything works fine except for one thing, when an Item in my TreeModel changes its name the bounding box of the labels that are used to render the cells in the tree dont change.

    To give you an example, when my Item's toString() method returns "This is a long item name" after creation the label with have a rather large bounding box. When I then change the name to "short" the text in the tree correctly updates to the new name, but the bounding box is still the same size.
    So when the item is selected in the tree the highlight will be huge.

    Even worse is when the name was short at the beginning and became bigger later on. In this case the full name is not shown at all, instead it shows just "...".

    Here is the code I use for the TreeCellRenderer:
    	public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
    		Component result = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
     
    		if (result instanceof JLabel) {
    			JLabel label = (JLabel) result;
     
    			if (value instanceof SourceBrowserNode) {
    				SourceBrowserNode node = (SourceBrowserNode) value;
    				boolean error = node.getSource().getErrors().exists();
    				boolean changed = node.getSource().hasChanged();
     
    				Color textColor = error ? leafColor_error : ( changed ? leafColor_changed : leafColor_normal );
    				label.setForeground(textColor);
     
    				if (node.isLeaf()) {
    					Icon icon = error ? leafIcon_error : leafIcon_noError;
    					label.setIcon(icon);
    				}
    			}
    		}
     
    		return result;
    	}

    Heres pictures of the problem in action:

    After the item was created:
    image1.png

    When the name is made smaller afterwards:
    image2.png

    When the name is made longer afterwards:
    image3.png


    Thank you very much for the help.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Refresh bounds of jLabel

    Try calling nodeChanged(TreeNode node) on the TreeModel backing the JTree (nodeChanged is actually defined in the DefaultTreeModel class), passing the node you wish to refresh as the parameter.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Refresh bounds of jLabel

    Ah, got it. You were right, that was the problem.

    Actually I had been using a custom tree model and I had implemented a method to notify the listeners but I had an error in that method and the listeners were not correctly informed about the change.

    Thank you very much.

Similar Threads

  1. jsf page refresh
    By omeja in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 7th, 2013, 11:35 AM
  2. Loop through JLabel and change JLabel
    By JoeBrown in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:52 PM
  3. Adding JLabel on other Jlabel
    By mike416 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 29th, 2012, 11:50 AM
  4. JTable refresh with Hibernate
    By Scott.Anthony in forum JDBC & Databases
    Replies: 2
    Last Post: October 12th, 2010, 04:19 AM
  5. GUI - refresh problem
    By Shnkc in forum AWT / Java Swing
    Replies: 5
    Last Post: April 2nd, 2010, 06:11 AM