我要做的是将单元格颜色编辑为红色(调用TableModelListener),按下按钮SEND后,所有红色单元格变为绿色。我怎样才能做到这一点?
final JTable Misc_Table = new JTable(model){
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
Component comp = super.prepareRenderer(renderer, row, col);
Object value = getModel().getValueAt(row, col);
/////what to do?///
return comp;
}
};
这是我的阅读按钮。选定的变量只是一个选定的ComboBox索引号。
Read.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
try {
int register = Integer.parseInt(Register_Text_Field.getText());
int count = Integer.parseInt(Amount_Text_Field.getText());
int position = Integer.parseInt(BitPos_Text_Field.getText());
String[] result = new String[count];
int selected = (int) Functions_ComboBox.getSelectedIndex();
if(selected == 0){
result = BackEnd.ReadMultiple_16Bit(251, register, count).get16BitString();
//BackEnd.closeSerial();
}
else if(selected == 1){
result = BackEnd.ReadMultiple_32Bit(251, register, count).get32BitString();
//BackEnd.closeSerial();
}
else if(selected == 2){
result = BackEnd.ReadMultiple_Float(251, register, count).getFloatString();
//BackEnd.closeSerial();
}
else if(selected == 3){
result = BackEnd.ReadCoilStatus(251, register, count).getCoilString();
//BackEnd.closeSerial();
}
else if(selected == 4){
result = BackEnd.ReadDiscreteInputStatus(251, register, count).getStringDiscreteInputResult();
//BackEnd.closeSerial();
}
else if(selected == 5){
result = BackEnd.ReadAnalogInputRegisters(251, register, count).getStringAnalogInputRegistersResult();
//BackEnd.closeSerial();
}
else if(selected == 6){
result = BackEnd.ReadBit(251, register, position, count).getReadBitString();
//BackEnd.closeSerial();
}
if (model.getRowCount() > 0)
{
model.setNumRows(0);
}
for(int i=0; i<result.length; i++)
{
model.addRow(new Object[]{register++,
result[i]});
}
} catch (NumberFormatException e) {
}
}
});
这是发送已编辑的值。
Misc_Table.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int col = e.getColumn();
Send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
int selected = (int) Functions_ComboBox.getSelectedIndex();
String val = (String) model.getValueAt(row, col);
if(selected == 0){
int int_Val = Integer.parseInt(val);
BackEnd.WriteMultiple_16Bit(unitID, row, int_Val , 1);
}
else if(selected == 1){
int int_Val = Integer.parseInt(val);
BackEnd.WriteMultiple_32Bit(unitID, row, int_Val , 1);
}
else if(selected == 2){
float float_Val = Float.parseFloat(val);
BackEnd.WriteMultiple_Float(unitID, row, float_Val, 1);
}
else if(selected == 6){
int position = Integer.parseInt(BitPos_Text_Field.getText());
BackEnd.WriteBit(unitID, row, position, true, 1);
}
} catch (NumberFormatException ea) {
JOptionPane.showMessageDialog(MISC,"Wrong number format.");
}
}
});
}
});