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
|
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JSplitPane;
import java.awt.BorderLayout;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
public class MenuTreeDemo extends JFrame implements TreeSelectionListener {
private JTabbedPane tabbedPane;
private JTree tree;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new MenuTreeDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
public MenuTreeDemo() {
DefaultMutableTreeNode top = new DefaultMutableTreeNode("MENU");
createMenu(top);
JSplitPane splitPane = new JSplitPane();
getContentPane().add(splitPane, BorderLayout.CENTER);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
splitPane.setRightComponent(tabbedPane);
JScrollPane scrollPane = new JScrollPane();
splitPane.setLeftComponent(scrollPane);
tree = new JTree(top);
scrollPane.setViewportView(tree);
tree.addTreeSelectionListener(this);
ToolTipManager.sharedInstance().registerComponent(tree);
tree.setCellRenderer(new MenuTreeCellRenderer());
}
/**
* @param top
*/
private void createMenu(DefaultMutableTreeNode top) {
DefaultMutableTreeNode newChild;
MenuItem menuItem;
// Sous-menu Statistique
DefaultMutableTreeNode statistique = new DefaultMutableTreeNode("Statistiques");
top.add(statistique);
ImageIcon ico1 = new ImageIcon(MenuTreeDemo.class.getResource("/1_32.png"));
menuItem = new MenuItem("Ventes",PanelStatVente.class,ico1,"Statistiques des ventes");
newChild = new DefaultMutableTreeNode(menuItem);
statistique.add(newChild);
ImageIcon ico2 = new ImageIcon(MenuTreeDemo.class.getResource("/2_32.png"));
menuItem = new MenuItem("Retour",PanelStatRetour.class,ico2,"Statistiques des retours");
newChild = new DefaultMutableTreeNode(menuItem);
statistique.add(newChild);
menuItem = new MenuItem("Divers",PanelStatDivers.class,ico2,"Statistiques diverses");
newChild = new DefaultMutableTreeNode(menuItem);
statistique.add(newChild);
// Sous-menu Sécurité
DefaultMutableTreeNode Security = new DefaultMutableTreeNode("Securité");
top.add(Security);
menuItem = new MenuItem("Utilisateur",PanelUser.class,null,"");
newChild = new DefaultMutableTreeNode(menuItem);
Security.add(newChild);
menuItem = new MenuItem("Permmissions",PanelPermission.class,null,"");
newChild = new DefaultMutableTreeNode(menuItem);
Security.add(newChild);
menuItem = new MenuItem("Profile",PanelProfile.class,null,"");
newChild = new DefaultMutableTreeNode(menuItem);
Security.add(newChild);
}
public void valueChanged(TreeSelectionEvent arg0) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
Object nodeInfo = node.getUserObject();
if (node == null)
return;
if (node.isLeaf()) {
MenuItem menuItem = (MenuItem) nodeInfo;
JComponent panel = menuItem.getPanelInstance();
int index = tabbedPane.indexOfComponent(panel);
if (index == -1) {
tabbedPane.add(menuItem.getTitle(), panel);
index = tabbedPane.indexOfComponent(panel);
}
tabbedPane.setSelectedIndex(index);
}
}
} |
Partager