1

我有一个 JTable 和一个 JTextField,我想突出显示与 JTextField 中的文本对应的单元格。我在代码中添加了 Todo,但我不知道该怎么做。

如何在表模型中做到这一点?任何人都可以建议一个代码片段吗?

表模型:

public class ArtikelTableModel extends AbstractTableModel {
private List<Object[]> data;
private String[] headers;
private String wordToBeFind = "";

public ArtikelTableModel(List<Object[]> data, String[] headers) {
    this.data = data;
    this.headers = headers;
}
public List<Object[]> getData() {
    return data;
}

public void setData(List<Object[]> data) {
    this.data = data;
}

public String getWordToBeFind() {
    return wordToBeFind;
}

public void setWordToBeFind(String wordToBeFind) {
    this.wordToBeFind = wordToBeFind;
}



public int getRowCount() {
    return data.size();
}

public int getColumnCount() {
    return headers.length;
}

public Object getValueAt(int rowIndex, int columnIndex) {
    String celValue = (String) data.get(rowIndex)[columnIndex];
    System.out.println(celValue);
    return celValue;
}

@Override
public void fireTableDataChanged() {
    super.fireTableDataChanged();
}

public void findWordInTableAndHighlightIt(String word){
    for(int i = 0; i<data.size();i++){
        for(int j=0;j<headers.length;j++){
            if(word.equals(data.get(i)[j])){
                //Todo: highlight the content of the cell and set the cell border color to red
            }
        }

    }
}
}

自定义渲染

public class ArtikelCellRenderer extends DefaultWebTableCellRenderer{
    protected static  int row;
    protected static int col;
    protected boolean isSelected;
    protected boolean isFocused;
    public Component getTableCellRendererComponent(WebTable tbl, Object v, boolean isSelected, boolean isFocused, int row, int col)
    {
            //Store this info for later use
            this.row = row;
            this.col = col;
            this.isSelected = isSelected;
            this.isFocused = isFocused;

            super.setValue(v); //Set the value as requested

            //Set colors dependant upon if the row is selected or not
            if (!this.isSelected) this.setBackground(new Color((float)0.87, (float)0.91, (float)1.0));
            else this.setBackground(new Color((float)0.75, (float)0.78, (float)0.85));

            //Set a special highlight color if this actual cell is focused
            if (this.isFocused) this.setBackground(new Color((float)0.5, (float)0.80, (float)0.6));

             //Set a special highlight color if this actual cell matches to the JTtextField text
              Todo: ?set background color to green and border color to red

            //and then allow the usual component to be returned
            return super.getTableCellRendererComponent(tbl, v, isSelected, isFocused, row, col);
    }
}
4

3 回答 3

2

在我关于Highlights subString ....的问题中详细描述了两种可能的方式(包括SSCCE ) ,

于 2012-03-28T12:55:58.503 回答
1

正如已经提到的,这TableModel不是正确的地方。

而是覆盖JTable.preparedRenderer(TableCellRenderer renderer, int row, int column). 如果rowcolumn数字相同,则可以更改Component返回的背景颜色作为显示(通常是 a JLabel);

这是一个突出显示鼠标所在行的示例:

@Override
public Component prepareRenderer(final TableCellRenderer renderer, final int row, final int column) {
    final Component c = super.prepareRenderer(renderer, row, column);
    if (row == this.itsRow) {
        c.setBackground(Color.RED);
    }
    return c;
}

this.itsRow由 a 更新的 int 字段在哪里MouseMotionListener

this.addMouseMotionListener(new MouseMotionListener() {
        public void mouseMoved(MouseEvent e) {
            SubclassedJTable.this.itsRow = SubclassedJTable.this.rowAtPoint(e.getPoint());
            SubclassedJTable.this.repaint();
        }
        public void mouseDragged(MouseEvent e) {/***/}
    });
于 2012-03-28T15:24:30.633 回答
1

TableModel当然不是这个功能的地方。这属于渲染器。有关更多详细信息,请参阅有关渲染器的 Swing 教程

我投票结束这个问题,作为改变 JTable 中单元格颜色的副本(或特定的表头颜色 java swing),它解释了如何实现此功能。与您的文本字段的耦合是唯一的新事物,而且相当微不足道。在这些线程中查阅 kleopatra 的答案,并给她更多的信任

于 2012-03-28T12:51:27.263 回答