如何在JTable中隐藏(隐藏)行?

时间:2009-06-03 10:18:49

标签: java jtable row

有什么办法吗?

4 个答案:

答案 0 :(得分:13)

您可以使用RowFilter<DefaultTableModel, Object>类过滤掉行。 DefaultTableModel可以由您自己的模型替换。要过滤,请实施方法

@Override
public boolean include(Entry entry) {
    // All rows are included if no filter is set
    if (filterText.isEmpty())
        return true;

    // If any of the column values contains the filter text,
    // the row can be shown
    for (int i = 0; i < entry.getValueCount(); i++) {
        String value = entry.getStringValue(i);
        if (value.toLowerCase().indexOf(filterText) != -1)
            return true;
    }

    return false;
}

访问行时,例如侦听ListSelectionEvents,不要忘记将可见行索引转换为模型中的完整行索引。 Java也为此提供了一个函数:

public void valueChanged(ListSelectionEvent e) {
    ListSelectionModel lsm = (ListSelectionModel) e.getSource();

    int visibleRowIndex = ... // Get the index of the selected row

    // Convert from the selection index based on the visible items to the
    // internal index for all elements.
    int internalRowIndex = tableTexts
            .convertRowIndexToModel(visibleRowIndex);

    ...
}

答案 1 :(得分:7)

查看Sun's Tutorial for JTables并查看“排序和过滤”部分。

答案 2 :(得分:1)

您可以为每个列设置一个arraylists,这些列由过滤的值填充,并在自定义渲染器中实现这些。如果不满足单元格的整行值,则渲染器以行+ 1递归调用自身。

如果单元格行符合条件,它会被渲染,另一个arraylist存储已经渲染的行号,最好通过示例解释此方法是在客户渲染器中扩展JLabel

public Component getTableCellRendererComponent(JTable table, Object color,
        boolean isSelected, boolean hasFocus, int row, int column) {

    Object value;
    String s;

    try {
        if (row == 0) {
            drawn[column].clear();
        }// drawn is arraylist which stores cols rend
        if (row > table.getModel().getDataVector.size()) {
            return null;
        }// if we go too far
        if (drawn[column].contains(Integer.toString(row)) == true) {
            // already rendered?
            return getTableCellRendererComponent(table, color, isSelected,
                    hasFocus, (row + 1), column);
        }// render row+1

        for (int i = 0; i < filters.length; i++) {
            value = table.getModel().getValueAt(row, i);
            s = (i == 1) ? df.format(value) : value.toString();
            if (filters[i].contains(s) != true) {
                //try and put in next row, if succeeds when it reaches column 8 it adds row to
                return getTableCellRendererComponent(table, color,
                        isSelected, hasFocus, (row + 1), column);
            }
        }

        value = table.getModel().getValueAt(row, column);

        setFont(getFont().deriveFont(Font.BOLD));

        if ((isSelected == false)) {

            if ((column == 1)) {
                setForeground(Color.magenta);
            }// just formatting
            else {
                setForeground(Color.black);
                setBackground(Color.white);
            }

        } else {
            setBackground(Color.blue);
            setForeground(Color.white);
        }

        if ((column == 1))// col 1 is a date, other columns strings
        {
            setText((value == null) ? "" : df.format(value));
        } else {
            setText((value == null) ? "" : value.toString());
        }

        todayStr = df.format(new java.util.Date());
        dateval = table.getModel().getValueAt(row, 1);
        String datevalStr = df.format(dateval);
        if (datevalStr.equals(todayStr)) {
            setForeground(Color.red);
        }
        drawn[column].add(Integer.toString(row));// mark row as rendered

    } catch (Exception e) {
        e.getMessage();
        return null;
    }
    return this;
}

答案 3 :(得分:-3)

最佳最简单的方法是从模型中删除相应的元素。