Testando uma classe, eu não consigo fazer a célula da JTable ficar com cores diferentes!Eu quero digitar 40 e deixar azul, e ao digitar 50, deixar vermelho(após pressionar o JButton).Mudei um bando de coisa, mas hora fica a linha toda vermelha, ou a linha toda azul.
Todo o código para quem quiser testar:
[code]
import java.awt.;
import java.awt.event.;
import javax.swing.;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import java.util.;
public class JTableDemo extends JDialog implements Runnable {
JTable table = new JTable();
public static void main(String… arg) {
SwingUtilities.invokeLater(new JTableDemo());
}
private final MyColorModel tableModel;
private JTableDemo() {
setModal(false);
setTitle("JTable demo");
setLayout(new BorderLayout(10, 10));
JToolBar bar = new JToolBar();
bar.setFloatable(false);
bar.setRollover(true);
ButtonGroup g = new ButtonGroup();
g.add((AbstractButton) bar.add(new AnalyzeButton("Analyze")));
add(bar, BorderLayout.NORTH);
java.util.List<MyColorRow> rows = new ArrayList<MyColorRow>();
rows.add(new MyColorRow(10,30, null));
rows.add(new MyColorRow(20,80, null));
rows.add(new MyColorRow(30,70, null));
tableModel = new MyColorModel(rows);
table.setModel(tableModel);
table.setAutoCreateRowSorter(true);
table.setShowGrid(true);
table.getColumnModel().getColumn(0).setCellRenderer((TableCellRenderer) new MyColorRenderer());
table.getColumnModel().getColumn(1).setCellRenderer((TableCellRenderer) new MyColorRenderer());
add(new JScrollPane(table), BorderLayout.CENTER);
pack();
}
//Analyze button
private class AnalyzeButton extends JButton {
private AnalyzeButton(String text) {
super(text);
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < tableModel.getRowCount(); i++){
Object obj= table.getValueAt(i,0);
int value=(int)obj;
if(value==40){
tableModel.setColorAt(i, 0, Color.BLUE);
}else if(value==50){
tableModel.setColorAt(i, 0, Color.RED);
}else
tableModel.setColorAt(i, 0, null);
}
}
});
}
}
// The model.
private static class MyColorModel extends AbstractTableModel {
private final java.util.List<MyColorRow> rows;
private MyColorModel(java.util.List<MyColorRow> rows) {
this.rows = rows;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return Integer.class;
case 1:
return Integer.class;
default:
throw new IndexOutOfBoundsException("columnIndex out of bounds");
}
}
@Override
public int getRowCount() {
return rows.size();
}
@Override
public int getColumnCount() {
return 2;
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
MyColorRow cr=rows.get(rowIndex);
switch(columnIndex){
case 0:
return cr.getValue();
case 1:
return cr.getValue2();
default: // default
throw new IndexOutOfBoundsException("columnIndex out of bounds");
}
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex){
MyColorRow cr=rows.get(rowIndex);
if(columnIndex==0)
cr.setValue((int)aValue);
else if(columnIndex==1)
cr.setValue2((int)aValue);
fireTableCellUpdated(rowIndex,columnIndex);
}
public Color getColorAt(int rowIndex, int columnIndex) {
MyColorRow cr=rows.get(rowIndex);
switch(columnIndex){
case 0:
return cr.getColor();
case 1:
return cr.getColor();
default: // default
throw new IndexOutOfBoundsException("columnIndex out of bounds");
}
}
public void setColorAt(int rowIndex, int columnIndex, Color color) {
MyColorRow cr=rows.get(rowIndex);
if(columnIndex==0)
cr.setColor(color);
else if(columnIndex==1)
cr.setColor(color);
fireTableRowsUpdated(rowIndex, rowIndex);
}
}
// Contains data of one row. Note that it keeps the text and the color.
private static class MyColorRow {
int value;
int value2;
private Color color;
private MyColorRow(int value,int value2,Color color) {
this.value = value;
this.value2= value2;
this.color = color;
}
public int getValue() {
return value;
}
public int getValue2() {
return value2;
}
public Color getColor() {
return color;
}
public void setValue(int v) {
this.value = v;
}
public void setValue2(int v) {
this.value2 = v;
}
public void setColor(Color color) {
this.color = color;
}
}
private static class MyColorRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean hasFocus, int row, int column){
MyColorModel model = (MyColorModel) table.getModel();
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value,isSelected, hasFocus, row, column);
Color color = model.getColorAt(row, column);
label.setText(value.toString());
label.setForeground(color != null ? color : table.getForeground());
return label;
}
}
public void run() {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
} [/code]
Onde está o erro? :roll: