When I click on the JButton nothing happens. This applies even for a JScrollPane that I put in (it shows, but will not scroll). Why is the JPanel not at the front? I get the sense that something has to be overridden. Should it be the expansion of the part that is not the arrow? If so, how is that done?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.tree.*;
public class test {
public test() {
JTree tree = createTree();
tree.setToggleClickCount(0);
tree.setRowHeight(50);
tree.setCellRenderer(new PanelRenderer());
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(tree));
f.setSize(400, 400);
f.setLocation(200, 200);
f.setVisible(true);
}
private JTree createTree() {
int children = 4;
int grandChildren = 3;
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
DefaultMutableTreeNode node;
for (int j = 0; j < children; j++) {
node = new DefaultMutableTreeNode();
root.add(node);
for (int k = 0; k < grandChildren; k++)
node.add(new DefaultMutableTreeNode());
}
DefaultTreeModel model = new DefaultTreeModel(root);
return new JTree(model);
}
public static void main(String[] args) {
new test();
}
}
class PanelRenderer implements TreeCellRenderer {
JPanel panel;
public PanelRenderer() {
panel = new JPanel(new BorderLayout());
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
panel.add(new JLabel("aloha"), BorderLayout.WEST);
JButton fg = new JButton("fg");
panel.add(fg, BorderLayout.EAST);
fg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent d) {
System.out.println("hello");
}
});
panel.setPreferredSize(new Dimension(200, 500));
return panel;
}
}
This is for MadProgrammer who says that Renderers and Editors make an image of what you put down. In this example, the JButtons are clickable.
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
import java.util.*;
public class test extends JFrame {
public test() {
super("TreeDemo");
setSize(500, 500);
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
JButton theParent = new JButton("Feature Set #1");
JButton thefeature = new JButton("sdfdsfds");
JButton feature2 = new JButton("Second Feature");
JButton feature3 = new JButton("Third Feature");
DefaultMutableTreeNode top = new DefaultMutableTreeNode(theParent, true);
DefaultMutableTreeNode n1 = new DefaultMutableTreeNode(thefeature, true);
DefaultMutableTreeNode n2 = new DefaultMutableTreeNode(feature2, true);
DefaultMutableTreeNode n3 = new DefaultMutableTreeNode(feature3, false);
n1.add(n3);
top.add(n1);
top.add(n2);
JTree tree = new JTree(top);
p.add(tree, BorderLayout.NORTH);
getContentPane().add(p);
TestRenderer tr = new TestRenderer();
TestEditor1 te = new TestEditor1();
tree.setEditable(true);
tree.setCellRenderer(tr);
tree.setCellEditor(te);
}
public class TestEditor1 implements TreeCellEditor {
public TestEditor1() {
}
public void addCellEditorListener(CellEditorListener l) {
}
public void cancelCellEditing() {
}
public Object getCellEditorValue() {
return this;
}
public boolean isCellEditable(EventObject evt) {
if (evt instanceof MouseEvent) {
MouseEvent mevt = (MouseEvent) evt;
if (mevt.getClickCount() == 1) {
return true;
}
}
return false;
}
public void removeCellEditorListener(CellEditorListener l) {
}
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
public boolean stopCellEditing() {
return false;
}
public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded,
boolean leaf, int row) {
DefaultMutableTreeNode temp = (DefaultMutableTreeNode) value;
JComponent temp2 = (JComponent) temp.getUserObject();
return temp2;
}
}
public class TestRenderer implements TreeCellRenderer {
transient protected Icon closedIcon;
transient protected Icon openIcon;
public TestRenderer() {
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
DefaultMutableTreeNode temp = (DefaultMutableTreeNode) value;
JComponent temp2 = (JComponent) temp.getUserObject();
temp2.setBackground(UIManager.getColor("Tree.textBackground"));
return temp2;
}
public void setClosedIcon(Icon newIcon) {
closedIcon = newIcon;
}
public void setOpenIcon(Icon newIcon) {
openIcon = newIcon;
}
}
public static void main(String args[]){
JFrame frame = new test();
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…