Example analysis of Tree and scrollbar usage in swing

  • 2020-04-01 04:07:47
  • OfStack

This article illustrates the use of Tree and scrollbars in swing. Share with you for your reference. The details are as follows:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class test extends JApplet{
  JTree tree;
  JTextField jtf;
  public void init(){
    Container contentPane = getContentPane();
    contentPane.setLayout(new BorderLayout());
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");
    DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
    top.add(a);
    DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
    a.add(a1);
    DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
    a.add(a2);
    DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
    top.add(b);
    DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
    b.add(b1);
    DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
    b.add(b2);
    tree = new JTree(top);
    int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
    int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
    JScrollPane jsp = new JScrollPane(tree,v,h);
    contentPane.add(jsp, BorderLayout.CENTER);
    jtf = new JTextField("", 20);
    contentPane.add(jtf, BorderLayout.SOUTH);
    tree.addMouseListener(new MouseAdapter(){
      public void mouseClicked(MouseEvent me){
        doMouseclicked(me);
      }
    });
  }
  void doMouseclicked(MouseEvent me){
    TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
    if(tp !=null)
      jtf.setText(tp.toString());
    else
      jtf.setText("");
  }
}

I hope this article has been helpful to your Java programming.


Related articles: