How do i keep a colored jTable cell colored after selecting a new one?

吃可爱长大的小学妹 提交于 2020-01-06 08:05:50

问题


I have a difficult time understanding the cellrenderer and find a solution to a specified problem. I want to color cells specifically when selected and then clicking a button and then have the program remind which cells are colored already and which ones are not. So if I color one, it should stay colored the rest of the time, until a new game is started. I do have it as far that I can color a cell but I can't figure out how to make the jTable keep the colored ones colored.

I work in Netbeans with GUI. This is my first time and I am a starter, so please be gentle with me and my code. I do have read the rendering tutorials and so on but can't find a working way, or I can't make it work in my little program.

This is right after the initcomponents();

jTableScoreFormulier.setDefaultRenderer(Object.class,new MyRenderer());

Then you get this part to make the table:

    jTableScoreFormulier.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {"Rood", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
            {"Geel", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
            {"Groen", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2"},
            {"Blauw", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2"}
        },
        new String [] {
            "Kleur", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "Sluit"
        }
    ));

And here is my renderer:

class MyRenderer implements TableCellRenderer {

public final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Component renderer = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    Color foreground, background;
    if (isSelected) {
        foreground = Color.WHITE;
        background = Color.BLACK;
    }  else {
        foreground = Color.BLACK;
        background = Color.WHITE;
    }
    renderer.setForeground(foreground);
    renderer.setBackground(background);
    return renderer;
}
}

}


回答1:


I can't figure out how to make the jTable keep the colored ones colored.

One way is to keep the information in the TableModel. So maybe you simply store Boolean.TRUE or Boolean.FALSE in the model to indicate which cells are selected. The default for all the cells would be Boolean.FALSE Then when you click on a cell you update the TableModel with the setValueAt(Boolean.TRUE, row, column) method to change the selection.

Then your renderer code becomes:

//if (isSelected) {

Boolean colored = (Boolean)value;

if (colored) {
   ...



回答2:


jTableScoreFormulier.setRowSelectionAllowed(true);
jTableScoreFormulier.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

This enables multi-selection



来源:https://stackoverflow.com/questions/45635529/how-do-i-keep-a-colored-jtable-cell-colored-after-selecting-a-new-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!