1

I have a JTable and a JTextField, I want to highlight the cell which corresponds to the text in the JTextField. I added Todo in the code but I don't know how to do it.

How is it possible to do it within table model? Can anybody suggest a code snippet?

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

    }
}
}

Custom render

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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
itro
  • 7,006
  • 27
  • 78
  • 121
  • *"I added Todo in the code but I don't know how to do it."* Maybe they need a `// WhatToPutHere?` tag. ;) – Andrew Thompson Mar 28 '12 at 12:45
  • possible duplicate of [Changing color of cell in JTable](http://stackoverflow.com/questions/5821724/changing-color-of-cell-in-jtable) – Robin Mar 28 '12 at 12:49

3 Answers3

2

Two possible ways (including SSCCE) are detailed descibed in my question about Highlights subString ....,

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

The TableModel is certainly not the place for this functionality. This belongs in the renderer. See the Swing tutorial about renderers for more details.

I voted to close this question as a duplicate of Changing color of cell in JTable (or particular one table header color java swing), which explains how to achieve this functionality. The coupling with your text field is the only new thing and that is rather trivial. Consult the answers of kleopatra in those threads and give her some more credit

Community
  • 1
  • 1
Robin
  • 36,233
  • 5
  • 47
  • 99
1

As has been mentioned, the TableModel is not the right place for this.

Instead override JTable.preparedRenderer(TableCellRenderer renderer, int row, int column). If the row and column numbers are the same, you can change the background color of the Component returned as the display (usually a JLabel);

Here's an example that highlights the row the mouse is over:

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

where this.itsRow is an int field updated by a 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) {/***/}
    });
BenCole
  • 2,092
  • 3
  • 17
  • 26