0

I have a form to fill and 3 chekbox(yellow-orange-red), when i fill the form and select a colour I click a button which will add a row whit selected colour to the Jtable . I used a TableCellRenderer It works but when I do the operation three or four times I have all th rows coloured whith the selected checkbox. How to make for every row his colour? Ther's my code:

JTable  table = new JTable(
            new DefaultTableModel(new Object[][] {}, columnNames));

    TableColumn column1 = table.getColumnModel().getColumn(
            0);
    column1.setCellRenderer(getRenderer());
    TableColumn column3 = table.getColumnModel().getColumn(
            1);
    column3.setCellRenderer(getRenderer());
    TableColumn column2 = table.getColumnModel().getColumn(
            2);
    column2.setCellRenderer(getRenderer());
 String desc = display.getSelectedItem().toString();
 DefaultTableModel tm = (DefaultTableModel) table.getModel();

                    String numcam = num_cam;
                    String timeStamp = new SimpleDateFormat(
                            "yyyy-MM-dd--HH:mm:ss").format(Calendar
                            .getInstance().getTime());

                    tm.addRow(new Object[] { new String(timeStamp), new String(numcam),
                            new String(desc) });

                    table.setModel(tm);

                    // reinitialiser les boutons apres le click
                    buttonGroup.clearSelection();
                    buttonGroup2.clearSelection();

And there's my cell renderer code

  private static TableCellRenderer getRenderer() {
    return new DefaultTableCellRenderer() {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Override
        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus,
                int row, int column) {
            Component tableCellRendererComponent = super
                    .getTableCellRendererComponent(table, value,
                            isSelected, hasFocus, row, column);
            tableCellRendererComponent.setBackground(Color.black);
            setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
            // verification de l couleur du bouton clické
            if (yellow.isSelected()) {
                tableCellRendererComponent.setForeground(new Color(255,
                        255, 51));
            } else if (orange.isSelected()) {
                tableCellRendererComponent.setForeground(new Color(255,
                        153, 0));
            } else if (red.isSelected()) {
                tableCellRendererComponent.setForeground(Color.RED);
            }

            return tableCellRendererComponent;
        }
    };
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
hamza-don
  • 455
  • 5
  • 26

1 Answers1

1

How to make for every row his colour?

The question is not clear to me:

  1. Are you trying to make every row the same color? If yes, then you need to invoke table.repaint() whenever you select a new color. The all the rows will be re-renderer with the new color

  2. Or, are you trying to make every row a different Color depending on the color that was selected at the time the row was added? If yes, then you need to add another column to the TableModel to store the selected Color for the row. Then in your renderer you need to get the Color from the TableModel.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Yes, when I click the submit Button It will add a new row with values from my form and this row is colored depending on the selected color. Sorry I didn't understand your suggestion because i'm really new on swing java – hamza-don Nov 25 '13 at 16:27
  • @user2928578, I am suggestion that the color of the row needs to be based on data in the TableModel. See [Table Row Rendering](http://tips4java.wordpress.com/2010/01/24/table-row-rendering/) for an example of this approach. In you case you will want data in the TableModel to store the Color, but you don't want a column displayed in the table. So you will need to remove the `TableColumn` from the table. See the `getColumn()` and `removeColumn()` methods for JTable to help you do this. – camickr Nov 25 '13 at 18:01
  • @user2928578 That is complete working code that you can download and compile and execute. It doesn't get any simpler than that. I have no idea what you don't understand about the example. – camickr Nov 25 '13 at 20:01