私は作ろうとしています:
すべてのノードが JCheckBox の可能性を持つ JTree。
さまざまなチェックボックスを選択した後、さまざまなチェックされたノードへのノードパスを示す文字列を抽出できるようにしたいと考えています。
リーダー ノードは、子ノードが選択されているかどうかを表示できる必要があります。
私は小さな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);
}
}