问题
I am trying to make:
A JTree where ALL nodes have the possibility of a JCheckBox.
After having selected the different checkboxes I would like to be able to extract strings stating the node path to the different checked nodes.
The leader node must be able to show if a child node has been selected.
I have included a small SSCCE
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.Color;
import java.awt.EventQueue;
public class ptf_viewer extends JFrame {
private static final long serialVersionUID = 1L;
static JTree tree = new JTree();
public static void main(String[] args) throws Exception {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ptf_viewer frame = new ptf_viewer();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ptf_viewer() throws Exception
{
getContentPane().setBackground(Color.WHITE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
getContentPane().setLayout(null);
tree.setVisibleRowCount(50);
tree.setRootVisible(false);
tree.setModel(new DefaultTreeModel(
new DefaultMutableTreeNode("test") {
private static final long serialVersionUID = 1L;
{
DefaultMutableTreeNode node_1;
DefaultMutableTreeNode node_2;
DefaultMutableTreeNode node_3;
DefaultMutableTreeNode node_4;
DefaultMutableTreeNode node_5;
DefaultMutableTreeNode node_6;
node_1 = new DefaultMutableTreeNode("Aarhus");
node_2 = new DefaultMutableTreeNode("protection");
node_3 = new DefaultMutableTreeNode("grid");
node_4 = new DefaultMutableTreeNode("plant");
node_5 = new DefaultMutableTreeNode("disconnection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("current"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("connection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("reconnection");
node_6 = new DefaultMutableTreeNode("longInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_6 = new DefaultMutableTreeNode("shortInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_4.add(node_5);
node_3.add(node_4);
node_4 = new DefaultMutableTreeNode("unit");
node_5 = new DefaultMutableTreeNode("disconnection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("LOM"));
node_5.add(new DefaultMutableTreeNode("current"));
node_5.add(new DefaultMutableTreeNode("dcImmunity"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("stability");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("current"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("connection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("reconnection");
node_6 = new DefaultMutableTreeNode("longInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_6 = new DefaultMutableTreeNode("shortInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_4.add(node_5);
node_3.add(node_4);
node_2.add(node_3);
node_3 = new DefaultMutableTreeNode("generator");
node_3.add(new DefaultMutableTreeNode("photovoltaic"));
node_2.add(node_3);
node_3 = new DefaultMutableTreeNode("human");
node_3.add(new DefaultMutableTreeNode("lossOfMains"));
node_3.add(new DefaultMutableTreeNode("residualCurrent"));
node_3.add(new DefaultMutableTreeNode("voltage"));
node_3.add(new DefaultMutableTreeNode("frequency"));
node_2.add(node_3);
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("quality");
node_2.add(new DefaultMutableTreeNode("harmonics "));
node_2.add(new DefaultMutableTreeNode("flicker"));
node_2.add(new DefaultMutableTreeNode("powerFactor"));
node_2.add(new DefaultMutableTreeNode("dcEmission"));
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("support");
node_3 = new DefaultMutableTreeNode("voltage");
node_3.add(new DefaultMutableTreeNode("faultRideTrough"));
node_2.add(node_3);
node_2.add(new DefaultMutableTreeNode("frequency"));
node_2.add(new DefaultMutableTreeNode("reactivePower"));
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("fault");
node_2.add(new DefaultMutableTreeNode("shortCircuit"));
node_1.add(node_2);
node_1.add(new DefaultMutableTreeNode("all"));
add(node_1);
node_1 = new DefaultMutableTreeNode("Billund");
node_2 = new DefaultMutableTreeNode("protection");
node_3 = new DefaultMutableTreeNode("grid");
node_4 = new DefaultMutableTreeNode("plant");
node_5 = new DefaultMutableTreeNode("disconnection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("current"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("connection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("reconnection");
node_6 = new DefaultMutableTreeNode("longInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_6 = new DefaultMutableTreeNode("shortInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_4.add(node_5);
node_3.add(node_4);
node_4 = new DefaultMutableTreeNode("unit");
node_5 = new DefaultMutableTreeNode("disconnection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("LOM"));
node_5.add(new DefaultMutableTreeNode("current"));
node_5.add(new DefaultMutableTreeNode("dcImmunity"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("stability");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("current"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("connection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("reconnection");
node_6 = new DefaultMutableTreeNode("longInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_6 = new DefaultMutableTreeNode("shortInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_4.add(node_5);
node_3.add(node_4);
node_2.add(node_3);
node_3 = new DefaultMutableTreeNode("generator");
node_3.add(new DefaultMutableTreeNode("photovoltaic"));
node_2.add(node_3);
node_3 = new DefaultMutableTreeNode("human");
node_3.add(new DefaultMutableTreeNode("lossOfMains"));
node_3.add(new DefaultMutableTreeNode("residualCurrent"));
node_3.add(new DefaultMutableTreeNode("voltage"));
node_3.add(new DefaultMutableTreeNode("frequency"));
node_2.add(node_3);
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("quality");
node_2.add(new DefaultMutableTreeNode("harmonics "));
node_2.add(new DefaultMutableTreeNode("flicker"));
node_2.add(new DefaultMutableTreeNode("powerFactor"));
node_2.add(new DefaultMutableTreeNode("dcEmission"));
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("support");
node_3 = new DefaultMutableTreeNode("voltage");
node_3.add(new DefaultMutableTreeNode("faultRideTrough"));
node_2.add(node_3);
node_2.add(new DefaultMutableTreeNode("frequency"));
node_2.add(new DefaultMutableTreeNode("reactivePower"));
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("fault");
node_2.add(new DefaultMutableTreeNode("shortCircuit"));
node_1.add(node_2);
node_1.add(new DefaultMutableTreeNode("all"));
add(node_1);
node_1 = new DefaultMutableTreeNode("Christiansfeld");
node_2 = new DefaultMutableTreeNode("protection");
node_3 = new DefaultMutableTreeNode("grid");
node_4 = new DefaultMutableTreeNode("plant");
node_5 = new DefaultMutableTreeNode("disconnection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("current"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("connection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("reconnection");
node_6 = new DefaultMutableTreeNode("longInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_6 = new DefaultMutableTreeNode("shortInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_4.add(node_5);
node_3.add(node_4);
node_4 = new DefaultMutableTreeNode("unit");
node_5 = new DefaultMutableTreeNode("disconnection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("LOM"));
node_5.add(new DefaultMutableTreeNode("current"));
node_5.add(new DefaultMutableTreeNode("dcImmunity"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("stability");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("current"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("connection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("reconnection");
node_6 = new DefaultMutableTreeNode("longInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_6 = new DefaultMutableTreeNode("shortInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_4.add(node_5);
node_3.add(node_4);
node_2.add(node_3);
node_3 = new DefaultMutableTreeNode("generator");
node_3.add(new DefaultMutableTreeNode("photovoltaic"));
node_2.add(node_3);
node_3 = new DefaultMutableTreeNode("human");
node_3.add(new DefaultMutableTreeNode("lossOfMains"));
node_3.add(new DefaultMutableTreeNode("residualCurrent"));
node_3.add(new DefaultMutableTreeNode("voltage"));
node_3.add(new DefaultMutableTreeNode("frequency"));
node_2.add(node_3);
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("quality");
node_2.add(new DefaultMutableTreeNode("harmonics "));
node_2.add(new DefaultMutableTreeNode("flicker"));
node_2.add(new DefaultMutableTreeNode("powerFactor"));
node_2.add(new DefaultMutableTreeNode("dcEmission"));
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("support");
node_3 = new DefaultMutableTreeNode("voltage");
node_3.add(new DefaultMutableTreeNode("faultRideTrough"));
node_2.add(node_3);
node_2.add(new DefaultMutableTreeNode("frequency"));
node_2.add(new DefaultMutableTreeNode("reactivePower"));
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("fault");
node_2.add(new DefaultMutableTreeNode("shortCircuit"));
node_1.add(node_2);
node_1.add(new DefaultMutableTreeNode("all"));
add(node_1);
node_1 = new DefaultMutableTreeNode("Daugaard");
node_2 = new DefaultMutableTreeNode("protection");
node_3 = new DefaultMutableTreeNode("grid");
node_4 = new DefaultMutableTreeNode("plant");
node_5 = new DefaultMutableTreeNode("disconnection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("current"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("connection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("reconnection");
node_6 = new DefaultMutableTreeNode("longInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_6 = new DefaultMutableTreeNode("shortInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_4.add(node_5);
node_3.add(node_4);
node_4 = new DefaultMutableTreeNode("unit");
node_5 = new DefaultMutableTreeNode("disconnection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("LOM"));
node_5.add(new DefaultMutableTreeNode("current"));
node_5.add(new DefaultMutableTreeNode("dcImmunity"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("stability");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("current"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("connection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("reconnection");
node_6 = new DefaultMutableTreeNode("longInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_6 = new DefaultMutableTreeNode("shortInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_4.add(node_5);
node_3.add(node_4);
node_2.add(node_3);
node_3 = new DefaultMutableTreeNode("generator");
node_3.add(new DefaultMutableTreeNode("photovoltaic"));
node_2.add(node_3);
node_3 = new DefaultMutableTreeNode("human");
node_3.add(new DefaultMutableTreeNode("lossOfMains"));
node_3.add(new DefaultMutableTreeNode("residualCurrent"));
node_3.add(new DefaultMutableTreeNode("voltage"));
node_3.add(new DefaultMutableTreeNode("frequency"));
node_2.add(node_3);
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("quality");
node_2.add(new DefaultMutableTreeNode("harmonics "));
node_2.add(new DefaultMutableTreeNode("flicker"));
node_2.add(new DefaultMutableTreeNode("powerFactor"));
node_2.add(new DefaultMutableTreeNode("dcEmission"));
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("support");
node_3 = new DefaultMutableTreeNode("voltage");
node_3.add(new DefaultMutableTreeNode("faultRideTrough"));
node_2.add(node_3);
node_2.add(new DefaultMutableTreeNode("frequency"));
node_2.add(new DefaultMutableTreeNode("reactivePower"));
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("fault");
node_2.add(new DefaultMutableTreeNode("shortCircuit"));
node_1.add(node_2);
node_1.add(new DefaultMutableTreeNode("all"));
add(node_1);
node_1 = new DefaultMutableTreeNode("Esbjerg");
node_2 = new DefaultMutableTreeNode("protection");
node_3 = new DefaultMutableTreeNode("grid");
node_4 = new DefaultMutableTreeNode("plant");
node_5 = new DefaultMutableTreeNode("disconnection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("current"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("connection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("reconnection");
node_6 = new DefaultMutableTreeNode("longInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_6 = new DefaultMutableTreeNode("shortInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_4.add(node_5);
node_3.add(node_4);
node_4 = new DefaultMutableTreeNode("unit");
node_5 = new DefaultMutableTreeNode("disconnection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("LOM"));
node_5.add(new DefaultMutableTreeNode("current"));
node_5.add(new DefaultMutableTreeNode("dcImmunity"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("stability");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("current"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("connection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("reconnection");
node_6 = new DefaultMutableTreeNode("longInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_6 = new DefaultMutableTreeNode("shortInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_4.add(node_5);
node_3.add(node_4);
node_2.add(node_3);
node_3 = new DefaultMutableTreeNode("generator");
node_3.add(new DefaultMutableTreeNode("photovoltaic"));
node_2.add(node_3);
node_3 = new DefaultMutableTreeNode("human");
node_3.add(new DefaultMutableTreeNode("lossOfMains"));
node_3.add(new DefaultMutableTreeNode("residualCurrent"));
node_3.add(new DefaultMutableTreeNode("voltage"));
node_3.add(new DefaultMutableTreeNode("frequency"));
node_2.add(node_3);
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("quality");
node_2.add(new DefaultMutableTreeNode("harmonics "));
node_2.add(new DefaultMutableTreeNode("flicker"));
node_2.add(new DefaultMutableTreeNode("powerFactor"));
node_2.add(new DefaultMutableTreeNode("dcEmission"));
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("support");
node_3 = new DefaultMutableTreeNode("voltage");
node_3.add(new DefaultMutableTreeNode("faultRideTrough"));
node_2.add(node_3);
node_2.add(new DefaultMutableTreeNode("frequency"));
node_2.add(new DefaultMutableTreeNode("reactivePower"));
node_1.add(node_2);
node_2 = new DefaultMutableTreeNode("fault");
node_2.add(new DefaultMutableTreeNode("shortCircuit"));
node_1.add(node_2);
node_1.add(new DefaultMutableTreeNode("all"));
add(node_1);
}
}
));
tree.setShowsRootHandles(true);
tree.setBounds(174, 32, 301, 420);
getContentPane().add(tree);
}
}
回答1:
your code
.
.
with JCheckBox (uncoment)
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.tree.*;
public class CheckBoxNodeTreeSample {
public CheckBoxNodeTreeSample() {
JFrame frame = new JFrame("CheckBox Tree");
//setLookAndFeel();
JTree tree = new JTree();
tree.setVisibleRowCount(50);
tree.setRootVisible(false);
tree.setModel(new DefaultTreeModel(
new DefaultMutableTreeNode("test") {
private static final long serialVersionUID = 1L;
{
DefaultMutableTreeNode node_1;
DefaultMutableTreeNode node_2;
DefaultMutableTreeNode node_3;
DefaultMutableTreeNode node_4;
DefaultMutableTreeNode node_5;
DefaultMutableTreeNode node_6;
node_1 = new DefaultMutableTreeNode("Aarhus");
node_2 = new DefaultMutableTreeNode("protection");
node_3 = new DefaultMutableTreeNode("grid");
node_4 = new DefaultMutableTreeNode("plant");
node_5 = new DefaultMutableTreeNode("disconnection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("current"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("connection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("reconnection");
node_6 = new DefaultMutableTreeNode("longInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_6 = new DefaultMutableTreeNode("shortInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_4.add(node_5);
node_3.add(node_4);
node_4 = new DefaultMutableTreeNode("unit");
node_5 = new DefaultMutableTreeNode("disconnection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("LOM"));
node_5.add(new DefaultMutableTreeNode("current"));
node_5.add(new DefaultMutableTreeNode("dcImmunity"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("stability");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_5.add(new DefaultMutableTreeNode("current"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("connection");
node_5.add(new DefaultMutableTreeNode("voltage"));
node_5.add(new DefaultMutableTreeNode("frequency"));
node_4.add(node_5);
node_5 = new DefaultMutableTreeNode("reconnection");
node_6 = new DefaultMutableTreeNode("longInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_6 = new DefaultMutableTreeNode("shortInterrupt");
node_6.add(new DefaultMutableTreeNode("voltage"));
node_6.add(new DefaultMutableTreeNode("frequency"));
node_5.add(node_6);
node_4.add(node_5);
node_3.add(node_4);
//removed overloaded limit 30k chars
}
}
));
tree.setShowsRootHandles(true);
// original code
/*CheckBoxNode accessibilityOptions[] = {new CheckBoxNode(
"Move system caret with focus/selection changes", false),
new CheckBoxNode("Always expand alt text for images", true)};
CheckBoxNode browsingOptions[] = {
new CheckBoxNode("Notify when downloads complete", true),
new CheckBoxNode("Disable script debugging", true),
new CheckBoxNode("Use AutoComplete", true),
new CheckBoxNode("Browse in a new process", false)};
Vector<Object> accessVector = new NamedVector("Accessibility", accessibilityOptions);
Vector<Object> browseVector = new NamedVector("Browsing", browsingOptions);
Object rootNodes[] = {accessVector, browseVector};
Vector<Object> rootVector = new NamedVector("Root", rootNodes);
tree = new JTree(rootVector);
CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
tree.setCellRenderer(renderer);
tree.setCellEditor(new CheckBoxNodeEditor(tree));
tree.setEditable(true);*/
//
JScrollPane scrollPane = new JScrollPane(tree);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new CheckBoxNodeTreeSample();
}
});
}
}
class CheckBoxNodeRenderer implements TreeCellRenderer {
private JCheckBox leafRenderer = new JCheckBox();
private DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();
private Color selectionBorderColor, selectionForeground, selectionBackground,
textForeground, textBackground;
protected JCheckBox getLeafRenderer() {
return leafRenderer;
}
public CheckBoxNodeRenderer() {
Font fontValue;
fontValue = UIManager.getFont("Tree.font");
if (fontValue != null) {
leafRenderer.setFont(fontValue);
}
Boolean booleanValue = (Boolean) UIManager.get("Tree.drawsFocusBorderAroundIcon");
leafRenderer.setFocusPainted((booleanValue != null) && (booleanValue.booleanValue()));
selectionBorderColor = UIManager.getColor("Tree.selectionBorderColor");
selectionForeground = UIManager.getColor("Tree.selectionForeground");
selectionBackground = UIManager.getColor("Tree.selectionBackground");
textForeground = UIManager.getColor("Tree.textForeground");
textBackground = UIManager.getColor("Tree.textBackground");
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
Component returnValue;
if (leaf) {
String stringValue = tree.convertValueToText(value, selected, expanded, leaf, row, false);
leafRenderer.setText(stringValue);
leafRenderer.setSelected(false);
leafRenderer.setEnabled(tree.isEnabled());
if (selected) {
leafRenderer.setForeground(selectionForeground);
leafRenderer.setBackground(selectionBackground);
} else {
leafRenderer.setForeground(textForeground);
leafRenderer.setBackground(textBackground);
}
if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
if (userObject instanceof CheckBoxNode) {
CheckBoxNode node = (CheckBoxNode) userObject;
leafRenderer.setText(node.getText());
leafRenderer.setSelected(node.isSelected());
}
}
returnValue = leafRenderer;
} else {
returnValue = nonLeafRenderer.getTreeCellRendererComponent(tree,
value, selected, expanded, leaf, row, hasFocus);
}
return returnValue;
}
}
class CheckBoxNodeEditor extends AbstractCellEditor implements TreeCellEditor {
private static final long serialVersionUID = 1L;
private CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
private ChangeEvent changeEvent1 = null;
private JTree tree;
public CheckBoxNodeEditor(JTree tree) {
this.tree = tree;
}
@Override
public Object getCellEditorValue() {
JCheckBox checkbox = renderer.getLeafRenderer();
CheckBoxNode checkBoxNode = new CheckBoxNode(checkbox.getText(),
checkbox.isSelected());
return checkBoxNode;
}
@Override
public boolean isCellEditable(EventObject event) {
boolean returnValue = false;
if (event instanceof MouseEvent) {
MouseEvent mouseEvent = (MouseEvent) event;
TreePath path = tree.getPathForLocation(mouseEvent.getX(),
mouseEvent.getY());
if (path != null) {
Object node = path.getLastPathComponent();
if ((node != null) && (node instanceof DefaultMutableTreeNode)) {
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
Object userObject = treeNode.getUserObject();
returnValue = ((treeNode.isLeaf()) && (userObject instanceof CheckBoxNode));
}
}
}
return returnValue;
}
@Override
public Component getTreeCellEditorComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row) {
Component editor = renderer.getTreeCellRendererComponent(tree, value, true, expanded, leaf, row, true);
ItemListener itemListener = new ItemListener() {// editor always selected / focused
@Override
public void itemStateChanged(ItemEvent itemEvent) {
if (stopCellEditing()) {
fireEditingStopped();
}
}
};
if (editor instanceof JCheckBox) {
((JCheckBox) editor).addItemListener(itemListener);
}
return editor;
}
}
class CheckBoxNode {
private String text;
private boolean selected;
public CheckBoxNode(String text, boolean selected) {
this.text = text;
this.selected = selected;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean newValue) {
selected = newValue;
}
public String getText() {
return text;
}
public void setText(String newValue) {
text = newValue;
}
@Override
public String toString() {
return getClass().getName() + "[" + text + "/" + selected + "]";
}
}
class NamedVector extends Vector<Object> {
private static final long serialVersionUID = 1L;
private String name;
public NamedVector(String name) {
this.name = name;
}
public NamedVector(String name, Object elements[]) {
this.name = name;
for (int i = 0, n = elements.length; i < n; i++) {
add(elements[i]);
}
}
@Override
public String toString() {
return "[" + name + "]";
}
}
来源:https://stackoverflow.com/questions/18201654/jcheckbox-in-all-nodes-of-a-jtree