2012-03-28 5 views
1

JTableとJTextFieldがあり、JTextFieldのテキストに対応するセルを強調表示します。コードにTodoを追加しましたが、どうやってそれを行うのか分かりません。カスタム表モデルを使用してJTable内のセルを強調表示します

テーブルモデル内でどのように行うことができますか?誰かがコードスニペットを提案できますか?

する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); 
    } 
} 
+0

「私はコードで藤堂を追加しましたが、私どうやってやるかわからない "* WhatToPutHere?タグが必要かもしれない。 ;) –

+0

[JTableのセルの色を変更する]の可能な複製(http://stackoverflow.com/questions/5821724/changing-color-of-cell-in-jtable) – Robin

答えて

1

TableModelをレンダリングするには、確かに、この機能のための場所ではありません。これはレンダラーに属します。詳細はSwing tutorial about renderersを参照してください。

私はChanging color of cell in JTable(またはparticular one table header color java swing)の複製としてこの問題を解決することにしました。これはこの機能を実現する方法を説明しています。あなたのテキストフィールドとの結合は唯一の新しいものであり、それはやや些細なことです。それらのスレッドにクレオパトラの答えを相談し、彼女のいくつかのより多くの信用を与える述べたように(SSCCE含む)

1

で詳細なdescibedあり、TableModelは右の場所ではありませんこのため。

代わりにJTable.preparedRenderer(TableCellRenderer renderer, int row, int column)を上書きしてください。 rowcolumnの数字が同じ場合は、ディスプレイとして返されたComponentの背景色を変更できます(通常JLabel)。ここで

は、マウスが上である行をハイライトする例を示します

this.itsRowMouseMotionListenerによって更新int型のフィールドである
@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.addMouseMotionListener(new MouseMotionListener() { 
     public void mouseMoved(MouseEvent e) { 
      SubclassedJTable.this.itsRow = SubclassedJTable.this.rowAtPoint(e.getPoint()); 
      SubclassedJTable.this.repaint(); 
     } 
     public void mouseDragged(MouseEvent e) {/***/} 
    }); 
関連する問題