import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.tree.*;
class JTreeTest extends JFrame {
JTree xTree;
JLabel xStatus;
public JTreeTest() {
super("JTree Demo");
DefaultMutableTreeNode root, node;
root = new DefaultMutableTreeNode("각국의 인사말");
node = new DefaultMutableTreeNode("한국");
node.add(new DefaultMutableTreeNode("안녕하슈~"));
node.add(new DefaultMutableTreeNode("반갑심데이~~"));
node.add(new DefaultMutableTreeNode("반갑당께요~"));
root.add(node);
node = new DefaultMutableTreeNode("미국");
node.add(new DefaultMutableTreeNode("Hi~"));
node.add(new DefaultMutableTreeNode("Hello~"));
node.add(new DefaultMutableTreeNode("Nice to meet you~"));
root.add(node);
xTree = new JTree(root);
xTree.addTreeSelectionListener( new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode n = (DefaultMutableTreeNode)
(e.getPath().getLastPathComponent());
xStatus.setText((String)n.getUserObject());
}
}
);
xTree.expandRow(2);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(xTree), "Center");
xStatus = new JLabel("없슴");
xStatus.setBorder(new TitledBorder("선택된항목"));
getContentPane().add(xStatus, "South");
}
public static void main(String[] args) {
JTreeTest f = new JTreeTest();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
f.pack();
f.setSize(300, 200);
f.setVisible(true);
}
}
'IT_Programming > Java' 카테고리의 다른 글
패키지와 임포트 (0) | 2007.02.06 |
---|---|
static의 사용법과 개념 예 (0) | 2007.02.05 |
스윙(Swing)이란? (0) | 2007.02.05 |
Vector vs. LinkedList (0) | 2007.02.05 |
Vector, ArrayList, Object[], HashMap, TreeMap 중에서 어떤 것을 선택할 것인가? (0) | 2007.02.05 |