如何在jtable中获得所有具有一条共享信息的不同行?

cvxl0en2  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(257)

我是新来的,对编程也不太熟悉,所以如果我不直呼事物的真名,我很抱歉。
所以我正在使用一个jtable,我试图让这个表显示所有住在同一个地方的人(在我的数据库中,我有两个住在“champa”的人),而不是只显示第一个在他们的“location”atribute中说地点的人。这是我正在使用的代码,它只向我显示第一个符合要求的人,而不是向我显示所有符合要求的人:

private void cargarLocalidad() {
    dtm.setRowCount(0);
    HabitanteADO h_ado = new HabitanteADO();
    Habitante habi = null;
    String [] habitantes = new String[7];
    String localidad = txtBuscar.getText().trim().toLowerCase();
    if(localidad.length() == 0) {
        ArrayList<Habitante> arregloHabitante = h_ado.readByAll();
        for(Habitante h : arregloHabitante) {
            habitantes[0] = h.getRut();
            habitantes[1] = h.getNombre();
            habitantes[2] = h.getGenero();
            habitantes[3] = h.getFecha().toString();
            habitantes[4] = h.getDireccion();
            habitantes[5] = h.getLocalidad().toLowerCase();
            habitantes[6] = String.valueOf(h.getVulnerabilidad());
            dtm.addRow(habitantes);
            txtBuscar.setText("");
            txtBuscar.requestFocus();
 }

    } else {
        habi = h_ado.readByLocalidad(localidad);
        if(habi != null) {
            habitantes[0] = habi.getRut();
            habitantes[1] = habi.getNombre();
            habitantes[2] = habi.getGenero();
            habitantes[3] = habi.getFecha().toString();
            habitantes[4] = habi.getDireccion();
            habitantes[5] = habi.getLocalidad();
            habitantes[6] = String.valueOf(habi.getVulnerabilidad());
            dtm.addRow(habitantes);
            txtBuscar.setText("");
            txtBuscar.requestFocus();
        } else
            mensaje("La localidad que busca no existe!");
            txtBuscar.setText("");
            txtBuscar.requestFocus();
}
    }

代码中位于else后面的部分只向我显示在文本字段中输入位置的第一个人。我需要它向我显示共享上述位置的所有人,因此如果我键入“champa”,我希望jtable能够加载我数据库中居住在那里的所有人,而不仅仅是第一个这样做的人。提前谢谢,这是我在这里的第一个职位,所以我很抱歉有任何错误!

dgenwo3n

dgenwo3n1#

通过正确查询数据库中的信息,您应该能够在jtable中显示此信息,例如:

String sql = "SELECT * FROM MyTableName WHERE city='champa';"

您还可以使用jtable的tablerowsorter类以及rowfilter.regexfilter()方法来过滤jtable的全部内容,以便只显示所需的记录,例如,下面有一个方法可以为您执行此操作(请阅读javadoc):

/**
 * Make sure the supplied JTable's DefaultTableModel already contains data
 * before calling this method.<br><br>
 * <p>
 * The JTable always retains the data it is currently filled with within its
 * Table Model. <b>Knowing this we can theoretically fill the Table with all
 * database table records then use the Filter to display only the specific
 * table records we want.</b> This way we don't have to pole the database
 * for different records all the time and records acquisition is greatly
 * increased.<br><br>
 * <p>
 * This method will pass the supplied Filter text across the supplied JTable
 * and will force that JTable to only display records (contained within that
 * JTable at the time) which contains that specific text. It reacts very
 * much like a search engine for the JTable.<br><br>
 * <p>
 * If you want to display all records again which were originally contained
 * within the supplied JTable then just pass a Null String ("") within the
 * filterText parameter of this method.<br><br>
 *
 * @param table          (JTable) The JTable to run filter on.<br>
 *
 * @param searchCriteria (String) The text to filter JTable with. Passing a 
 * Null String ("") will force the table to display all records. Regular 
 * Expressions (RegEx) can also be supplied within the criteria string. If 
 * the wildcard characters <b>?</b> or <b>*</b> are supplied within the filter 
 * criteria String without any RegEx meta characters then the functional purpose 
 * of these two wildcard characters are converted to RegEx when encountered. If 
 * actual Regular Expressions are going to be used to make up the search criteria 
 * string then be sure to set the <b>endorseWildcards</b> optional parameter to 
 * boolean false since the <b>?</b> and <b>*</b> wildcard characters have 
 * different meaning within a Regular Expression and must be handled differently.<br>
 *
 * @param options (optional - Integer/Boolean):<pre>
 * 
 *     byColumnNumber - (Optional - Integer - Default is -1) By default
 *                       this method filters across all table columns but
 *                       you can be column specific if you pass a column
 *                       number to this optional parameter. This parameter
 *                       accepts only a <b>Literal Column Number</b> which
 *                       means that although the first column within a
 *                       JTable is considered column 0, to this method it is
 *                       considered as column 1.
 * 
 *    endorseWildcards - (boolean) Default is true (allow wildcards). If true 
 *                       then when a wildcard character is encountered within
 *                       a search criteria string it is automatically converted
 *                       to its RegEx equivalent. The two wildcard characters 
 *                       are almost always more than enough to carry out any 
 *                       search required and is usually much easier to use than 
 *                       some complex regular expressions. If endorseWildcards
 *                       is true then upper or lower letter case is ignored as
 *                       well.
 * 
 *                       If you provide a true of false to this parameter then
 *                       you must provide a value (or null) to the option 
 *                       <b>byColumnNumber</b> parameter.</pre>
 */
public static void filterTable(JTable table, String searchCriteria, Object... options) {
    int column = -1;
    boolean endorseWildcards = true;
    if (options.length > 0) {
        if (options[0] != null) {
            column = (int)options[0] - 1;
        }
        if (options.length >= 2) {
            if (options[1] != null) {
                endorseWildcards = (boolean)options[1];
            }
        }
    }

    String criteria = searchCriteria;
    if (endorseWildcards) {
        criteria = "(?i)" + searchCriteria.replace("?", ".?").replace("*", ".*?");
    }

    try {
        TableRowSorter<TableModel> sorter = new TableRowSorter<>(((DefaultTableModel) table.getModel()));
        sorter.setRowFilter(column < 0 ? RowFilter.regexFilter(criteria) : 
                            RowFilter.regexFilter(criteria, column));
        table.setRowSorter(sorter);
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
}

相关问题