Java JTable Column selectByTyping(JTable table, javax.swing.JTextField textfield, int column)

Here you can find the source of selectByTyping(JTable table, javax.swing.JTextField textfield, int column)

Description

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

License

Open Source License

Parameter

Parameter Description
table the jTable where the item should be selected
textfield the related filtertextfield that contains the user-input
column the column where the filtering-comparison should be applied to. in most cases, the relevant information (i.e. the string/text) is in column 0, but sometimes also in column 1

Declaration

public static void selectByTyping(JTable table, javax.swing.JTextField textfield, int column) 

Method Source Code

//package com.java2s;
/*//from w ww.j a v a2s  .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.JTable;

public class Main {
    /**
     * This method selects the first entry in the JTable {@code table} that start with the
     * text that is entered in the filter-textfield {@code textfield}.
     *
     * @param table the jTable where the item should be selected
     * @param textfield the related filtertextfield that contains the user-input
     * @param column the column where the filtering-comparison should be applied to. in most cases, the relevant
     * information (i.e. the string/text) is in column 0, but sometimes also in column 1
     */
    public static void selectByTyping(JTable table, javax.swing.JTextField textfield, int column) {
        String text = textfield.getText().toLowerCase();
        for (int cnt = 0; cnt < table.getRowCount(); cnt++) {
            String val = table.getValueAt(cnt, column).toString();
            if (val.toLowerCase().startsWith(text)) {
                table.getSelectionModel().setSelectionInterval(cnt, cnt);
                table.scrollRectToVisible(table.getCellRect(cnt, column, false));
                // and leave method
                return;
            }
        }
    }
}

Related

  1. makeTablePanel(int rows, int cols, int mainColumn, JComponent components[])
  2. newTable(String name, Object[][] rowData, Object[] columnNames)
  3. pack(JTable table, boolean packColumns, boolean packRows, int padding)
  4. ponerAncho(TableColumn loColumn, int plAncho)
  5. removeColumn(int index, JTable table)
  6. setColumnOrder(final JTable table, final String positions)
  7. setearPrimerRegistro(JTable pTabla, JTextField pTextoDeBusqueda, int pColumna)
  8. setearTextoDeBusqueda(JTable pTabla, JTextField pTextoDeBusqueda, int pColumna)
  9. setSelectedRow(JTable table, int rowIndex, int columnIndex)