so i was trying to change the color of a row to red in a jtable in depending on the outcome of a cell put this code down below and should work but noting change
her is the code and if any one can do some modification on it that would be great and thank you
public class Test1 extends JFrame {
private JPanel contentPane;
private JTable table;
private static JTable getNewRenderedTable(final JTable table) {
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int col) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
int status = (int)table.getModel().getValueAt(row, 4);
if (status<=0) {
setBackground(Color.RED);
setForeground(Color.BLACK);
} else {
setBackground(table.getBackground());
setForeground(table.getForeground());
}
return this;
}
});
return table;
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test1 frame = new Test1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
try
{ Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
String query="select * from employes";
PreparedStatement pst=con.prepareStatement(query);
ResultSet rs=pst.executeQuery();
ResultSetMetaData rsm=rs.getMetaData();
int c=rsm.getColumnCount();
Vector column=new Vector(3);
column.add(rsm.getColumnName(1));
column.add(rsm.getColumnName(2));
column.add(rsm.getColumnName(3));
column.add("date");
Vector row=new Vector();
Vector data=new Vector();
while(rs.next())
{
row=new Vector();
row.add(rs.getString("id"));
row.add(rs.getString("nom"));
row.add(rs.getString("prenom"));
Timer time=new Timer(rs.getString("date_de_embauche").toString());
int j=(int) time.getResulte();
row.add(j);
data.add(row);
}
table = new JTable(data,column);
}
catch(Exception e)
{
System.out.println(e);
}
table.setBounds(33, 28, 335, 203);
contentPane.add(table);
}}