Selasa, 14 Agustus 2012
Program Tree
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
class DemoTree implements TreeSelectionListener {
public JTree tree;
public JScrollPane scroller;
public JTextField tfInfo;
public DemoTree() {
DefaultMutableTreeNode root = null;
DefaultMutableTreeNode nodeLevel1 = null;
DefaultMutableTreeNode nodeLevel2 = null;
root = new DefaultMutableTreeNode("SDDN");
nodeLevel1 = new DefaultMutableTreeNode("Betha Sidik");
root.add(nodeLevel1);
nodeLevel2 = new DefaultMutableTreeNode("Pemrograman HTML");
nodeLevel1.add(nodeLevel2);
nodeLevel2 = new DefaultMutableTreeNode("Pemrograman PHP");
nodeLevel1.add(nodeLevel2);
nodeLevel2 = new DefaultMutableTreeNode("Linux/Unix");
nodeLevel1.add(nodeLevel2);
nodeLevel1 = new DefaultMutableTreeNode("KS Bahri");
root.add(nodeLevel1);
nodeLevel2 = new
DefaultMutableTreeNode("Pemrograman Delphi");
nodeLevel1.add(nodeLevel2);
nodeLevel1 = new DefaultMutableTreeNode("Ruslan Nuryadin");
root.add(nodeLevel1);
nodeLevel2 = new DefaultMutableTreeNode("Co-Linux");
nodeLevel1.add(nodeLevel2);
nodeLevel2 = new DefaultMutableTreeNode("MapServer");
nodeLevel1.add(nodeLevel2);
nodeLevel1 = new DefaultMutableTreeNode("Budi Raharjo");
root.add(nodeLevel1);
nodeLevel2 = new DefaultMutableTreeNode("Oracle");
nodeLevel1.add(nodeLevel2);
nodeLevel2 = new DefaultMutableTreeNode("C++");
nodeLevel1.add(nodeLevel2);
nodeLevel2 = new DefaultMutableTreeNode("Pascal");
nodeLevel1.add(nodeLevel2);
nodeLevel2 = new DefaultMutableTreeNode("Java");
nodeLevel1.add(nodeLevel2);
tree = new JTree(root);
tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(this);
scroller = new JScrollPane(tree,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroller.setLocation(0,0);
scroller.setSize(new Dimension(440, 300));
tfInfo = new JTextField(100);
tfInfo.setLocation(0, 300);
tfInfo.setSize(tfInfo.getPreferredSize());
}
public void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Demo JTree");
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(scroller);
frame.getContentPane().add(tfInfo);
frame.setBounds(0, 0, 450, 350);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void valueChanged(TreeSelectionEvent event) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
if (node == null) {
return;
}
//if (node.isLeaf()) { // memeriksa node level akhir
tfInfo.setText(node.toString());
//}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
DemoTree app = new DemoTree();
app.createAndShowGUI();
}
});
}
}
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar