meu codigo esta abaixo, ele pega o resultado de uma consulta ao banco de dados e mostra os valores, se eu mando comparar o valor de value com uma constante, da certo, dependendo do numero a cor fica como eu quero. Mas se eu comparo value com uma variavel, ele pega apenas o ultimo numero da lista em q esta a variavel para comparar.
Se alguem puder me ajudar eu agradeço.
package asj;
import DAO.DAOvendedor;
import static asj.ControleEstoque.QTD;
import java.awt.Color;
import java.awt.Component;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import logica.listarprodutos;
public class ControleEstoque extends javax.swing.JFrame {
public int cor;
public static int qtdminima, QTD;
// Vermelho ver = new Vermelho();
public ControleEstoque() throws SQLException {
initComponents();
CarregaVendedor();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
formatarPreco1 = new Util.FormatarPreco();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jCvendedor = new javax.swing.JComboBox<>();
jScrollPane1 = new javax.swing.JScrollPane();
jTtabela = new javax.swing.JTable();
jBBuscar = new javax.swing.JButton();
formatarPreco1.setText("formatarPreco1");
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Controle de Estoque");
jLabel1.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
jLabel1.setText("Controle de Estoque");
jLabel2.setText("Selecione Empresa");
jTtabela.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"ID", "Produto", "Preço Compra", "Qtd.", "Qtd. Minima"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTtabela.getTableHeader().setReorderingAllowed(false);
jScrollPane1.setViewportView(jTtabela);
if (jTtabela.getColumnModel().getColumnCount() > 0) {
jTtabela.getColumnModel().getColumn(0).setResizable(false);
jTtabela.getColumnModel().getColumn(0).setPreferredWidth(5);
jTtabela.getColumnModel().getColumn(1).setResizable(false);
jTtabela.getColumnModel().getColumn(1).setPreferredWidth(150);
jTtabela.getColumnModel().getColumn(2).setResizable(false);
jTtabela.getColumnModel().getColumn(2).setPreferredWidth(15);
jTtabela.getColumnModel().getColumn(2).setCellRenderer(formatarPreco1);
jTtabela.getColumnModel().getColumn(3).setResizable(false);
jTtabela.getColumnModel().getColumn(3).setPreferredWidth(5);
jTtabela.getColumnModel().getColumn(4).setResizable(false);
jTtabela.getColumnModel().getColumn(4).setPreferredWidth(5);
}
jBBuscar.setBackground(new java.awt.Color(0, 255, 0));
jBBuscar.setText("Buscar");
jBBuscar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBBuscarActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jCvendedor, javax.swing.GroupLayout.PREFERRED_SIZE, 501, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jBBuscar))
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 761, javax.swing.GroupLayout.PREFERRED_SIZE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jCvendedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBBuscar))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 11, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 60, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jBBuscarActionPerformed(java.awt.event.ActionEvent evt) {
try {
DAOvendedor dao = new DAOvendedor();
DefaultTableModel model = (DefaultTableModel) jTtabela.getModel();
model.setRowCount(0);
for (listarprodutos p : dao.EstoquexVendedor((String) jCvendedor.getSelectedItem())) {
model.addRow(new Object[]{p.getId(), p.getProduto(), p.getPrecocompra(), p.getQtd(), p.getQtdmin()});
qtdminima = p.getQtdmin();
QTD = p.getQtd();
jTtabela.getColumnModel().getColumn(4).setCellRenderer(new Vermelho());
}
} catch (SQLException ex) {
Logger.getLogger(ControleEstoque.class.getName()).log(Level.SEVERE, null, ex);
}
}
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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ControleEstoque.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(() -> {
try {
new ControleEstoque().setVisible(true);
} catch (SQLException ex) {
Logger.getLogger(ControleEstoque.class.getName()).log(Level.SEVERE, null, ex);
}
});
}
// Variables declaration - do not modify
private Util.FormatarPreco formatarPreco1;
private javax.swing.JButton jBBuscar;
private javax.swing.JComboBox<String> jCvendedor;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTtabela;
// End of variables declaration
//carrega vendedores
public void CarregaVendedor() throws SQLException {
DAOvendedor dao = new DAOvendedor();
DefaultComboBoxModel defaultComboModel = new DefaultComboBoxModel(dao.Carregar("vendedor", "empresa").toArray());
jCvendedor.setModel(defaultComboModel);
}
}
class Vermelho extends DefaultTableCellRenderer {
Color defaultBackground, defaultForeground;
public Vermelho() {
this.defaultBackground = getBackground();
this.defaultForeground = getForeground();
setHorizontalAlignment(javax.swing.JLabel.RIGHT);
}
@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);
final java.awt.Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
System.out.println("QTD " +QTD);
System.out.println("Min " + (int) value);
System.out.println("---------------------");
if ((int) value > QTD){
cellComponent.setForeground(Color.white);
cellComponent.setBackground(Color.red);
}else{
cellComponent.setForeground(Color.white);
cellComponent.setBackground(Color.blue);
}
return c;
}
}