문제

할 수있는 방법이 있습니까?

도움이 되었습니까?

해결책

그만큼 베스트 가장 쉬운 방법은 모델에서 해당 요소를 제거하는 것입니다.

다른 팁

거기에 있습니다 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);

    ...
}

체크 아웃 JTables에 대한 Sun의 튜토리얼 그리고 정렬 및 필터링 섹션을보십시오.

필터링 된 값으로 채워진 각 열에 대한 배열리스트를 설정하고 사용자 정의 렌더러에서이를 구현할 수 있습니다. 셀 전체 행 값이 충족되지 않으면 렌더러는 Row+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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top