1
I need the line color to change whenever the cell value is 0. However, all lines are being painted.
View with a simple Jtable
import javax.swing.JTable;
public class View extends javax.swing.JFrame {
private EstoqueActionListener listener;
public View() {
initComponents();
this.listener = new EstoqueActionListener(this);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
tableEstoque = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tableEstoque.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null},
{null, null},
{null, null}
},
new String [] {
"PRODUTO", "STATUS"
}
));
jScrollPane1.setViewportView(tableEstoque);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 752, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new View().setVisible(true);
}
});
}
public JTable getTableEstoque() {
return tableEstoque;
}
public void setTableEstoque(JTable tableEstoque) {
this.tableEstoque = tableEstoque;
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tableEstoque;
// End of variables declaration
}
Class Estoquetablemodel extending Abstracttablemodel
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.table.AbstractTableModel;
public class EstoqueTableModel extends AbstractTableModel {
List<Estoque> estoques;
List<String> colunas = Arrays.asList("PRODUTO", "SALDO");
public EstoqueTableModel() {
Estoque e1 = new Estoque("ProdutoA", 10);
Estoque e2 = new Estoque("ProdutoA", 10000);
Estoque e3 = new Estoque("ProdutoA", 0);
estoques = new ArrayList<>();
estoques.add(e1);
estoques.add(e2);
estoques.add(e3);
}
@Override
public int getRowCount() {
return estoques.size();
}
@Override
public int getColumnCount() {
return colunas.size();
}
public int getColumnIndex(String coluna) {
return colunas.indexOf(coluna);
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Estoque e = estoques.get(rowIndex);
String columnName = getColumnName(columnIndex);
switch(columnName) {
case "PRODUTO": return e.getNomeProduto();
case "SALDO": return e.getSaldo();
}
return null;
}
@Override
public String getColumnName(int column) {
return colunas.get(column);
}
public List<Estoque> getEstoques() {
return estoques;
}
public static class Estoque {
private String nomeProduto;
private int saldo;
public Estoque(String nomeProduto, int saldo) {
this.nomeProduto = nomeProduto;
this.saldo = saldo;
}
public String getNomeProduto() {
return nomeProduto;
}
public void setNomeProduto(String nomeProduto) {
this.nomeProduto = nomeProduto;
}
public int getSaldo() {
return saldo;
}
public void setSaldo(int saldo) {
this.saldo = saldo;
}
}
}
Class that manages view events and starts Jtable data
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class EstoqueActionListener {
private View viewEstoque;
private EstoqueTableModel tableEstoque;
public EstoqueActionListener(View viewEstoque) {
this.viewEstoque = viewEstoque;
startTable();
}
public void startTable() {
tableEstoque = new EstoqueTableModel();
if (tableEstoque.getEstoques() != null) {
viewEstoque.getTableEstoque().setModel(tableEstoque);
for (int i = 0; i < tableEstoque.getEstoques().size(); i++) {
int cellValue = (int) tableEstoque.getValueAt(i, tableEstoque.getColumnIndex("SALDO"));
if (cellValue == 0) { //SE 0, MUDE A COR
viewEstoque.getTableEstoque().setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
c.setBackground(Color.red);
c.setForeground(Color.white);
return c;
}
});
}
}
}
}
I even found a solution, but without a [mcve], there’s no way I can even test your code to see if it’ll work or vote reopening. Add this to the question to facilitate reopening.
– user28595
I already have a solution, just waiting to reopen the question.
– user28595
@diegofm found out why in that previous example nothing was being painted. Within the method I was passing my object to another class to align the cells. As a new instance of Defaulttablecellrenderer was created, I lost the characteristics of the previous one. This I have already fixed, however, all cells are painted instead of those specific ones. I await your solution.
– Marcelo Augusto