Setting the Color on the row of the JTable using JColorChooser

后端 未结 2 1354
时光说笑
时光说笑 2021-01-26 08:56

I am novice to Swing. I just started Swing couple of weeks ago and I stuck at some point..

I have designed a JTable having some rows on clicking (right) it

2条回答
  •  粉色の甜心
    2021-01-26 09:28

    If you want the ability to color each row separately then one way is to store the Color as part of the data in the TableModel. So you will need to add the Color as a column in the model.

    But you will not want to display this column in the view of the table so you will need to remove it from the view:

    table.removeColumn( table.getColumn(...) );
    

    Next you will need to add custom rendering for the table. One way to do this is to add rendering for the entire row. Check out Table Row Rendering for an example of this approach.

    So the basic code for the rendering would be something like:

    Color background = table.getTableModel.getValueAt(row, ???);
    
    if (background != null)
        c.setBackground( background );
    

    And when you display the color choose you would need to save the Color to the TableModel:

    table.getTableModel().setValueAt(color, table.getSelectedRow(), ???);
    

提交回复
热议问题