-
Bug
-
Resolution: Not an Issue
-
P3
-
None
-
1.2.0
-
x86
-
windows_95, windows_nt
I created MyTreeCellRenderer that is a JPanel as below. The JPanel has one JLabel. When I click a node one or more label's text was disappeared. After minimize and restore, some node's text was appeared but some still not be seen.
To reproduce:
1. Save the following code and name it as PaintTest.java
2. Compile and run it.
3. Expand all children nodes.
4. Click any node.
import java.awt.swing.*;
import java.awt.swing.tree.*;
import java.awt.*;
class MyTreeCellRenderer extends JPanel implements TreeCellRenderer
{
JLabel alabel = new JLabel("a label");
public MyTreeCellRenderer(){
super(false);
setLayout(new FlowLayout());
setBackground(Color.white);
add(alabel);
}
/*
public void paint(Graphics g){
super.paint(g);
alabel.paint(g);
}*/
public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus){
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
alabel.setText((String)(node.getUserObject()));
return this;
}
}
public class PaintTest extends JFrame
{
JTree tree = null;
MyTreeCellRenderer renderer = null;
public PaintTest(){
DefaultMutableTreeNode top = new DefaultMutableTreeNode("TOP");
DefaultMutableTreeNode catagory;
DefaultMutableTreeNode composer;
DefaultMutableTreeNode style;
// Classical
catagory = new DefaultMutableTreeNode("Classical");
top.add(catagory);
// Beethoven
catagory.add(composer = new DefaultMutableTreeNode("Beethoven"));
composer.add(style = new DefaultMutableTreeNode("Concertos"));
style.add(new DefaultMutableTreeNode("No. 1 - C Major"));
style.add(new DefaultMutableTreeNode("No. 2 - B-Flat Major"));
composer.add(style = new DefaultMutableTreeNode("Quartets"));
style.add(new DefaultMutableTreeNode("Six String Quartets"));
style.add(new DefaultMutableTreeNode("Three String Quartets"));
style.add(new DefaultMutableTreeNode("Grosse Fugue for String Quartets"));
// Mozart
catagory.add(composer = new DefaultMutableTreeNode("Mozart"));
composer.add(style = new DefaultMutableTreeNode("Concertos"));
style.add(new DefaultMutableTreeNode("Piano Concerto No. 12"));
style.add(new DefaultMutableTreeNode("Piano Concerto No. 17"));
renderer = new MyTreeCellRenderer();
tree = new JTree(top);
tree.setCellRenderer(renderer);
getContentPane().add(tree);
setSize(300,300);
show();
}
public static void main(String[] args){
new PaintTest();
}
}
To reproduce:
1. Save the following code and name it as PaintTest.java
2. Compile and run it.
3. Expand all children nodes.
4. Click any node.
import java.awt.swing.*;
import java.awt.swing.tree.*;
import java.awt.*;
class MyTreeCellRenderer extends JPanel implements TreeCellRenderer
{
JLabel alabel = new JLabel("a label");
public MyTreeCellRenderer(){
super(false);
setLayout(new FlowLayout());
setBackground(Color.white);
add(alabel);
}
/*
public void paint(Graphics g){
super.paint(g);
alabel.paint(g);
}*/
public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus){
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
alabel.setText((String)(node.getUserObject()));
return this;
}
}
public class PaintTest extends JFrame
{
JTree tree = null;
MyTreeCellRenderer renderer = null;
public PaintTest(){
DefaultMutableTreeNode top = new DefaultMutableTreeNode("TOP");
DefaultMutableTreeNode catagory;
DefaultMutableTreeNode composer;
DefaultMutableTreeNode style;
// Classical
catagory = new DefaultMutableTreeNode("Classical");
top.add(catagory);
// Beethoven
catagory.add(composer = new DefaultMutableTreeNode("Beethoven"));
composer.add(style = new DefaultMutableTreeNode("Concertos"));
style.add(new DefaultMutableTreeNode("No. 1 - C Major"));
style.add(new DefaultMutableTreeNode("No. 2 - B-Flat Major"));
composer.add(style = new DefaultMutableTreeNode("Quartets"));
style.add(new DefaultMutableTreeNode("Six String Quartets"));
style.add(new DefaultMutableTreeNode("Three String Quartets"));
style.add(new DefaultMutableTreeNode("Grosse Fugue for String Quartets"));
// Mozart
catagory.add(composer = new DefaultMutableTreeNode("Mozart"));
composer.add(style = new DefaultMutableTreeNode("Concertos"));
style.add(new DefaultMutableTreeNode("Piano Concerto No. 12"));
style.add(new DefaultMutableTreeNode("Piano Concerto No. 17"));
renderer = new MyTreeCellRenderer();
tree = new JTree(top);
tree.setCellRenderer(renderer);
getContentPane().add(tree);
setSize(300,300);
show();
}
public static void main(String[] args){
new PaintTest();
}
}
- duplicates
-
JDK-4106137 Painting problem in my TreeCellRenderer
- Closed
-
JDK-4102506 Duplicated label text on the attached test suite
- Closed
-
JDK-4102497 When expand JLabel children nodes it does not look good
- Closed
- relates to
-
JDK-4102486 A JComponent is not redrawing correctly when dragging other window over the tree
- Closed