-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
1.3.1
-
x86
-
windows_2000
Name: gm110360 Date: 09/24/2001
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
When a custom TreeCellRenderer is created the Tree does not correctly adjust to
chnages in the cell width. For example if the cell initially does not have an
icon and an icon is set when the cell is selected, the text gets cut off rather
than the cell width being adjusted to fit the new text. This is demonstrated by
the following sample...
Create one class called MyTreeCellRenderer that conatains the following...
//==================================================================
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean isSelected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
String stringValue = tree.convertValueToText(value, isSelected, expanded,
leaf, row, hasFocus);
this.hasFocus = hasFocus;
setText(stringValue);
if(isSelected)
setForeground(getTextSelectionColor());
else
setForeground(getTextNonSelectionColor());
// There needs to be a way to specify disabled icons.
if (!tree.isEnabled()) {
setEnabled(false);
if (leaf) {
if (isSelected)
setDisabledIcon( UIManager.getIcon("Tree.leafIcon") );
else
setDisabledIcon(null);
} else if (expanded) {
setDisabledIcon( UIManager.getIcon("Tree.openIcon") );
} else {
setDisabledIcon( UIManager.getIcon("Tree.closedIcon") );
}
}
else {
setEnabled(true);
if (leaf) {
if (isSelected)
setIcon( UIManager.getIcon("Tree.leafIcon") );
else
setIcon(null);
} else if (expanded) {
setIcon( UIManager.getIcon("Tree.openIcon") );
} else {
setIcon( UIManager.getIcon("Tree.closedIcon") );
}
}
setComponentOrientation(tree.getComponentOrientation());
this.selected = isSelected;
return this;
}
}
//==================================================================
Next create the following sample program....
//==================================================================
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
public final class TreeCellRendererSample extends JFrame
{
/**
* Constructs a TreeCellRendererSample object
*/
TreeCellRendererSample()
{
frameSetup();
validate();
setVisible( true );
}
/**
* Creates each element of the user interface and adds it to the the
* main frame.
*/
private void frameSetup()
{
// Get the content pane to work with
JPanel contentPane = (JPanel)getContentPane();
// Set the layout of the content pane
contentPane.setLayout( new GridLayout() );
// Set the size that the UI should start up at
setSize( 400, 400 );
// Create soem sample nodes to display
DefaultMutableTreeNode top = new DefaultMutableTreeNode(
"The Java Series");
createNodes(top);
// Add a tree with the MyTreeCellRenderer to the UI
JTree tree = new JTree(top);
tree.setCellRenderer( new MyTreeCellRenderer() );
contentPane.add( new JScrollPane(tree) );
// Expand the rows of the tree
tree.expandRow(0);
tree.expandRow(2);
tree.expandRow(1);
// Create an anonymous inner class that listens to the window
addWindowListener(
new WindowAdapter()
{
// When the window closes, end the program
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
/**
* Adds some sample nodes to the provided node - taken from the JavaTutorial
*/
private void createNodes(DefaultMutableTreeNode top)
{
DefaultMutableTreeNode category = null;
DefaultMutableTreeNode book = null;
category = new DefaultMutableTreeNode("Books for Java Programmers");
top.add(category);
book = new DefaultMutableTreeNode("The Java Tutorial: Object-Oriented "
+ "Programming for the Internet");
category.add(book);
book = new DefaultMutableTreeNode(
"The Java Tutorial Continued: The Rest of the JDK");
category.add(book);
book = new DefaultMutableTreeNode(
"The JFC Swing Tutorial: A Guide to Constructing GUIs");
category.add(book);
category = new DefaultMutableTreeNode("Books for Java Implementers");
top.add(category);
book = new DefaultMutableTreeNode(
"The Java Virtual Machine Specification");
category.add(book);
book = new DefaultMutableTreeNode("The Java Language Specification");
category.add(book);
}
/**
* Runs the test application
*
* @param args no arguements are used for this application
*/
public static void main (String args[])
{
new TreeCellRendererSample();
}
}
//==================================================================
The above sample will start up with a populated tree with the nodes expanded.
When one of the 'leaf' nodes of the tree is clicked on an icon is added to the
cell; thereby making the cell need to be wider to accomidate the icon. However
since the room that the cell is provided by the tree is not altered, the text
appears with a '..' on the end. The expected behavior would be that the Tree
would recognize that the cell width has changed and adjust the tree accordingly
to make room for the longer cell.
I have also tried to override the getPreferredSize() method and made numerous
other attempts at getting the Tree to realize that the size of the cell has
changed, but have not been able to do so. This problem is also experienced if
the size of the icon used changes dramatically. This problem prevents design
of a TreeCellRenderer that dymanically changes icons based on the state of the
tree contents or other variables when the icons are of varrying sizes or are
sometimes not shown.
(Review ID: 132118)
======================================================================
- relates to
-
JDK-8264803 JTree support for selection-sensitive rendering is incomplete
-
- Open
-