Java JTextField Select selectByTyping(javax.swing.JTree tree, javax.swing.JTextField textfield)

Here you can find the source of selectByTyping(javax.swing.JTree tree, javax.swing.JTextField textfield)

Description

This method selects the first entry in a jTree that start with the text that is entered in the filter-textfield.

License

Open Source License

Parameter

Parameter Description
tree the jTree where the item should be selected
textfield the related filtertextfield that contains the user-input

Declaration

public static void selectByTyping(javax.swing.JTree tree, javax.swing.JTextField textfield) 

Method Source Code

//package com.java2s;
/*//from   w ww. j  a v a 2 s  . c  o m
 * Zettelkasten - nach Luhmann
 ** Copyright (C) 2001-2014 by Daniel L?decke (http://www.danielluedecke.de)
 * 
 * Homepage: http://zettelkasten.danielluedecke.de
 * 
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of 
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 * Dieses Programm ist freie Software. Sie k?nnen es unter den Bedingungen der GNU
 * General Public License, wie von der Free Software Foundation ver?ffentlicht, weitergeben
 * und/oder modifizieren, entweder gem?? Version 3 der Lizenz oder (wenn Sie m?chten)
 * jeder sp?teren Version.
 * 
 * Die Ver?ffentlichung dieses Programms erfolgt in der Hoffnung, da? es Ihnen von Nutzen sein 
 * wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite Garantie der MARKTREIFE oder 
 * der VERWENDBARKEIT F?R EINEN BESTIMMTEN ZWECK. Details finden Sie in der 
 * GNU General Public License.
 * 
 * Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm 
 * erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>.
 */

import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;

import javax.swing.tree.TreePath;

public class Main {
    /**
     * This method selects the first entry in a jTree that start with the
     * text that is entered in the filter-textfield.
     *
     * @param tree the jTree where the item should be selected
     * @param textfield the related filtertextfield that contains the user-input
     */
    public static void selectByTyping(javax.swing.JTree tree, javax.swing.JTextField textfield) {
        DefaultTreeModel dtm = (DefaultTreeModel) tree.getModel();
        MutableTreeNode root = (MutableTreeNode) dtm.getRoot();
        String text = textfield.getText().toLowerCase();
        if (root != null && !text.isEmpty()) {
            for (int cnt = 0; cnt < dtm.getChildCount(root); cnt++) {
                MutableTreeNode child = (MutableTreeNode) dtm.getChild(root, cnt);
                String childtext = child.toString().toLowerCase();
                if (childtext.startsWith(text)) {
                    TreePath tp = new TreePath(root);
                    tp = tp.pathByAddingChild(child);
                    tree.setSelectionPath(tp);
                    tree.scrollPathToVisible(tp);
                    return;
                }
            }
        }
    }
}

Related

  1. createSelected(final JTextField textField, final JButton... btns)
  2. focusAndSelectTextInTextField(JTextField textField)
  3. newFileChooser(String dialogTitle, int fileSelectionMode, JTextField textField, String globalLocation)
  4. recipientHintSelected(String hintString, JTextField toList, boolean shiftKeyPressed)
  5. selectAllOnFocus(final JTextField jtf, final String recoverText, final Color forTip, final Color forContent)
  6. wrapWithFileChooser(final Component aParent, final JTextField aTextField, final int aFileSelectionMode)