import javax.swing.*; import javax.swing.tree.*; import javax.swing.event.*; public class Puu1 extends JPanel implements TreeSelectionListener{ JTree puu; JTextField tf=new JTextField(10); public Puu1(){ //puu=new JTree(new Object[]{"Juku", "Kati", "Mati"}); DefaultMutableTreeNode juur=new DefaultMutableTreeNode("klass"); DefaultMutableTreeNode laps1=new DefaultMutableTreeNode("Juku"); DefaultMutableTreeNode laps2=new DefaultMutableTreeNode("Kati"); DefaultMutableTreeNode laps3=new DefaultMutableTreeNode("Mati"); DefaultMutableTreeNode koer1=new DefaultMutableTreeNode("Muki"); DefaultMutableTreeNode koer2=new DefaultMutableTreeNode("Pontu"); juur.add(laps1); juur.add(laps2); juur.add(laps3); laps1.add(koer2); laps2.add(koer1); puu=new JTree(juur); add(puu); puu.addTreeSelectionListener(this); add(tf); } public void valueChanged(TreeSelectionEvent e){ //tf.setText(puu.getLastSelectedPathComponent().toString()); Object[] jada=puu.getSelectionPath().getPath(); StringBuffer sb=new StringBuffer(jada[0].toString()); for(int i=1; i "+jada[i]); } tf.setText(sb.toString()); } public static void main(String[] arg){ JFrame f=new JFrame("Puu"); f.add(new Puu1()); f.setSize(400, 400); f.setVisible(true); } }