1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreeSelectionModel;
public class Exemple2 {
public static void main(String[] args) {
JFrame frame = new JFrame("Exemple");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTree tree = new JTree(new DefaultTreeModel(createTree()));
tree.setRootVisible(false);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
for (int i = 0; i < tree.getRowCount(); i++) {
tree.expandRow(i);
}
tree.setVisibleRowCount(10);
JPanel buttonPanel = new JPanel(new GridBagLayout());
JButton buttonPlus = new JButton("+");
buttonPlus.setActionCommand("+");
JButton buttonMinus = new JButton("-");
buttonMinus.setActionCommand("-");
List<JButton> buttonList = Arrays.asList(buttonPlus, buttonMinus);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx=0;
gbc.gridy=GridBagConstraints.RELATIVE;
gbc.fill=GridBagConstraints.HORIZONTAL;
buttonList.forEach(button-> buttonPanel.add(button,gbc));
JPanel panel = new JPanel(new BorderLayout());
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setPreferredSize(new Dimension(300, 150));
JPanel treePanel = new JPanel(new BorderLayout());
treePanel.add(new JScrollPane(tree));
CardLayout cardLayout = new CardLayout();
JPanel mainPanel = new JPanel(cardLayout);
mainPanel.add(treePanel,"tree");
mainPanel.add(buttonPanel,"operators");
cardLayout.show(mainPanel, "tree");
panel.add(mainPanel);
panel.add(new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),BorderLayout.SOUTH);
frame.add(panel);
ActionListener buttonAction = e-> {
if ( textArea.getText().length()>0 ) {
textArea.append(" ");
}
textArea.append(e.getActionCommand());
cardLayout.show(mainPanel, "tree");
};
buttonList.forEach(button-> button.addActionListener(buttonAction));
TreeSelectionListener treeSelectionListener = e-> {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
if ( e.isAddedPath() && node.isLeaf() ) {
if ( textArea.getText().length()>0 ) {
textArea.append(" ");
}
textArea.append(node.getUserObject().toString());
cardLayout.show(mainPanel, "operators");
tree.setSelectionPaths(null);
}
};
tree.addTreeSelectionListener(treeSelectionListener);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static TreeNode createTree() {
String[] values = {
"a.b.A",
"a.b.B",
"c.C",
"c.d.D",
"c.d.E"
};
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
Map<String, DefaultMutableTreeNode> map = new HashMap<>();
StringBuilder path = new StringBuilder();
map.put("", root);
for(String value : values) {
String[] treePath = value.split("\\.");
add(root, path, map, treePath, 0);
}
return root;
}
private static void add(DefaultMutableTreeNode parentNode, StringBuilder path, Map<String, DefaultMutableTreeNode> map,
String[] treePath, int depth) {
if ( depth<treePath.length ) {
int length = path.length();
if ( depth!=0 ) {
path.append('.');
}
path.append(treePath[depth]);
DefaultMutableTreeNode currentNode = map.get(path.toString());
if ( currentNode==null ) {
currentNode = new DefaultMutableTreeNode(treePath[depth]);
parentNode.add(currentNode);
map.put(path.toString(), currentNode);
}
add(currentNode, path, map, treePath, depth+1);
path.setLength(length); // pop
}
}
} |
Partager