-1

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);
}}
ayoub
  • 41
  • 3

2 Answers2

3
table = new JTable(data,column);

The problem with your current code is that you create a new JTable. Whenever you create a new table you lose the custom renderers. So you need to add the renderer to the table AFTER you create the table. I don't see where you invoke your getNewRenderedTable(...) method to add the renderer.

Another problem is that you should use a JScrollPane to display the table and then add the scrollpane to the frame.

Finally don't use a null layout. Swing was designed to be used with layout managers.

i was trying to change the color of a row to red in a jtable in depending on the outcome of a cell

A JTable may use different renderers for each type of data in the columns. Instead of creating custom renderers for each data type you can do row level renderering by override the prepareRenderer(...) method of JTable.

The basic structure of the code would be:

JTable table = new JTable(...)
{
    public Component prepareRenderer(
        TableCellRenderer renderer, int row, int column)
    {
        Component c = super.prepareRenderer(renderer, row, column);

        //  add custom rendering here

        return c;
    }
};

Check out Table Row Rendering for an example that shows how to color rows based on data in the row.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • still pretty new to java i started a week ago so pleas can you edit my code for me – ayoub Nov 29 '15 at 18:51
  • @ayoub, You learn by trying. I also gave you a link to code that you can download and test. I also suggest you first learn how to do this without the database. The is create a JTable with hard coded data. Learn one concept at a time. – camickr Nov 29 '15 at 19:00
  • @ayoub, glad the suggestion helped. Don't forget to "accept" the answer so people know the problem has been solved. – camickr Nov 30 '15 at 16:04
0
try this code:

  String status = (String)table.getModel().getValueAt(row, STATUS_COL);
        if ("active".equals(status)) {
            setBackground(Color.BLACK);
            setForeground(Color.WHITE);
        } else {
            setBackground(table.getBackground());
            setForeground(table.getForeground());
        }  
Kumaresan Perumal
  • 1,926
  • 2
  • 29
  • 35