我在這裏有兩列(JTable
)。右欄是可編輯的,而另一欄不是。JTable valuechanged然後更改單元格顏色
所以,我的問題是,無論何時用戶更改單元格的值,該特定單元格將更改其單元格顏色。
我想這樣做是因爲我想讓用戶知道他/她在表格中進行了一些更改。
我發現這個地方,它以某種方式解決了我的問題,但有一件事沒有出現我的期望是更改值並單擊另一個單元格後,顏色變回原來的顏色。我想讓它保持直到它被保存。
@Override
public Component prepareEditor(TableCellEditor editor, int data, int columns) {
Component c = super.prepareEditor(editor, data, columns);
c.setBackground(Color.RED);
return c;
}
這可能嗎?如果是,請舉例說明。
UPDATE:
String[] columnname = {"Student Name", "Grade"};
Object[][] data = {};
gradetable = new JTable(data, columnname){
private Object[][] rowData;
public boolean isCellEditable(int data, int columns){
return columns == 1;
}
public Component prepareRenderer(TableCellRenderer r, int data, int columns){
final Component c = super.prepareRenderer(r, data, columns);
if (data % 2 == 0){
c.setBackground(Color.LIGHT_GRAY);
}
else{
c.setBackground(Color.WHITE);
}
if (isCellSelected(data, columns)){
c.setBackground(Color.ORANGE);
}
return c;
}
@Override
public Component prepareEditor(TableCellEditor editor, int data, int columns) {
Component c = super.prepareEditor(editor, data, columns);
c.setBackground(Color.RED);
return c;
}
};
gradetable.setModel(new DefaultTableModel(data, columnname));
gradetable.setPreferredScrollableViewportSize(new Dimension (350, 130));
gradetable.setFillsViewportHeight(true);
gradetable.getTableHeader().setReorderingAllowed(false);
gradetable.setGridColor(new Color(128,128,128,128));
JScrollPane jsp = new JScrollPane(gradetable);
panel3.add(jsp);
嗨MadProgrammer,感謝您的答覆。你能告訴我一個解決我的問題的示例程序嗎? – 2013-03-08 06:19:25
當然,其鏈接的答案 – MadProgrammer 2013-03-08 06:27:02
謝謝。我會研究你給的答案。 – 2013-03-08 07:59:35