The Source for Java Technology Collaboration


-- Main.aberrant - 02 Jun 2008

Demonstrates how to expand the searching functionality of JTree. Also has RegEx support.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.text.Position;
import javax.swing.text.Position.Bias;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 * Demonstrates how to change expand the searching functionality of JTree. 
 * 
 * @author Collin Fagan
 */
public class FlexibleSearchTreeDemo extends JPanel {

   private FlexibleSearchTree tree = new FlexibleSearchTree();
   private DefaultTreeModel treeModel;
   private JTextField searchField = new JTextField();
   private JButton searchButton = new JButton("Search");
   private JComboBox matchTypes = new JComboBox(Matchers.values());

   /**
    * Constructor builds the demo creating the table and associated renderer.
    */
   public FlexibleSearchTreeDemo() {
      setLayout(new BorderLayout());
      JScrollPane scrollPane = new JScrollPane(tree);
      add(scrollPane, BorderLayout.CENTER);

      JToolBar buttonBar = new JToolBar();
      buttonBar.setFloatable(false);
      buttonBar.add(searchField);
      buttonBar.add(searchButton);

      searchButton.addActionListener(searchAction);
      searchField.addActionListener(searchAction);

      add(buttonBar, BorderLayout.SOUTH);
      
      JToolBar matcherBar = new JToolBar();
      matcherBar.setFloatable(false);
      matcherBar.add(new JLabel("Match Type: "));
      matcherBar.add(matchTypes);
      add(matcherBar, BorderLayout.NORTH);
      
      matchTypes.addActionListener(changeMatcher);

      treeModel = createModel();
      tree.setModel(treeModel);
      tree.setMatcher(Matchers.Contains);
   }

   /**
    * changes the matcher in response to the user selecting an item out the combobox. 
    */
   private ActionListener changeMatcher = new ActionListener(){

      @Override
      public void actionPerformed(ActionEvent e) {
         tree.setMatcher((Matchers)matchTypes.getSelectedItem());
      }
   };
   
   /**
    * finds the next match for the text in the search box
    */
   private ActionListener searchAction = new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
         int start;
         if (tree.getSelectionCount() == 0) {
            start = -1;
         } else {
            start = tree.getSelectionRows()[0];
         }
         TreePath path = tree.getNextMatch(searchField.getText(), start,
               Position.Bias.Forward);
         if (path != null) {
            tree.setSelectionPath(path);
         }
      }

   };

   /**
    * Start the demo on the Event Dispatch Thread.
    * 
    * @param args
    *            the command line arguments
    */
   public static void main(String[] args) {

      // turn bold fonts off in metal
      UIManager.put("swing.boldMetal", Boolean.FALSE);

      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            JFrame demoFrame = new JFrame();
            demoFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            demoFrame.setContentPane(new FlexibleSearchTreeDemo());
            demoFrame.pack();
            demoFrame.setVisible(true);
         }
      });
   }

   /**
    * Creates the demos tree model which contains java books.
    * 
    * @return the demos tree model
    */
   private DefaultTreeModel createModel() {
      DefaultMutableTreeNode root = new DefaultMutableTreeNode("Books");
      DefaultMutableTreeNode category = null;
      DefaultMutableTreeNode book = null;

      category = new DefaultMutableTreeNode("Books for Java Programmers");
      root.add(category);

      // original Tutorial
      book = new DefaultMutableTreeNode(
            "The Java Tutorial: A Short Course on the Basics");
      category.add(book);

      // Tutorial Continued
      book = new DefaultMutableTreeNode(
            "The Java Tutorial Continued: The Rest of the JDK");
      category.add(book);

      // JFC Swing Tutorial
      book = new DefaultMutableTreeNode(
            "The JFC Swing Tutorial: A Guide to Constructing GUIs");
      category.add(book);

      // Bloch
      book = new DefaultMutableTreeNode(
            "Effective Java Programming Language Guide");
      category.add(book);

      // Arnold/Gosling
      book = new DefaultMutableTreeNode("The Java Programming Language");
      category.add(book);

      // Chan
      book = new DefaultMutableTreeNode("The Java Developers Almanac");
      category.add(book);

      category = new DefaultMutableTreeNode("Books for Java Implementers");
      root.add(category);

      // VM
      book = new DefaultMutableTreeNode(
            "The Java Virtual Machine Specification");
      category.add(book);

      // Language Spec
      book = new DefaultMutableTreeNode("The Java Language Specification");
      category.add(book);
      return new DefaultTreeModel(root);
   }

}

Topic FlexibleSearchTreeDemo . { Edit | Ref-By | Printable | Diffs r2 < r1 | More }
 XML java.net RSS

Revision r2 - 03 Jun 2008 - 02:04:26 - Main.aberrant
Parents: WebHome > Javatutorials