簡體   English   中英

通過自定義表模型突出顯示JTable中的單元格

[英]Highlight a cell in JTable via custom table model

我有一個JTable和一個JTextField,我想突出顯示與JTextField中的文本對應的單元格。 我在代碼中添加了Todo,但我不知道該怎么做。

如何在表模型中完成它? 任何人都可以建議一個代碼段嗎?

TableModel的:

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);
    }
}

關於Highlights subString ....的問題詳細描述了兩種可能的方式(包括SSCCE ),

TableModel當然不是這個功能的地方。 這屬於渲染器。 有關更多詳細信息,請參閱有關渲染器Swing教程

我投票決定將這個問題作為JTable (或特定的一個表頭顏色java swing中單元格更改重復,這解釋了如何實現此功能。 與文本字段的耦合是唯一的新事物,而且相當簡單。 在這些主題中查閱kleopatra的答案,並給她更多的功勞

如前所述, TableModel不是正確的選擇。

而是覆蓋JTable.preparedRenderer(TableCellRenderer renderer, int row, int column) 如果rowcolumn號相同,則可以更改作為顯示返回的Component的背景顏色(通常是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是由MouseMotionListener更新的int字段:

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) {/***/}
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM