JTree放入JSplitPane范例

范例1
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

public class JTreeTest extends JFrame {

    private JSplitPane split = new JSplitPane();

    private JTree tree = new JTree();

    private JLabel label = new JLabel();

    public JTreeTest() {
        this.getContentPane().add(this.split);
        this.split.setLeftComponent(this.tree);
        this.tree.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                label.setText(tree.getSelectionPath().getLastPathComponent().toString());
            }
        });
        this.split.setRightComponent(this.label);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        JTreeTest jt = new JTreeTest();
        jt.setVisible(true);
        jt.setSize(300, 200);
        jt.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

}
 
范例2:
package com.boomlink.report.gui.chart.treetest.jsplitpane;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
public class TestJTree {
 /**
  * @param args
  */
 public static void main(String[] args) {
  JFrame fr = new JFrame();
  fr.setSize(300, 500);
  fr.setDefaultCloseOperation(3);
  JSplitPane jp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
  fr.getContentPane().add(jp);
  JTree jt = new JTree();
  jp.add(jt, JSplitPane.LEFT, 0);
  JTextArea ja = new JTextArea();
  jp.add(ja, JSplitPane.RIGHT, 1);
  // fr.getContentPane().add(jt);
  fr.setVisible(true);
  jt.addTreeSelectionListener(new MyTreeSelectionListener(ja, jt));
 }
}
class MyTreeSelectionListener implements TreeSelectionListener {
 JTextArea ja;
 JTree jt;
 public MyTreeSelectionListener(JTextArea ja, JTree jt) {
  this.ja = ja;
  this.jt = jt;
 }
 public void valueChanged(TreeSelectionEvent e) {
  ja.setText(jt.getSelectionPath().getLastPathComponent().toString());
 }
}

你可能感兴趣的:(split)