选择新的jTable单元格后如何使其保持彩色? [英] How do i keep a colored jTable cell colored after selecting a new one?

查看:102
本文介绍了选择新的jTable单元格后如何使其保持彩色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解cellrenderer,并无法找到特定问题的解决方案. 我想为选定的单元格特别着色,然后单击一个按钮,然后让程序提醒哪些单元格已经着色而哪些单元格没有着色.因此,如果我为某个游戏上色,则在剩下的时间内,它应该保持不变,直到开始新游戏为止. 据我所知,我可以为单元格着色,但是我不知道如何使jTable保持着色的颜色.

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.

我在带GUI的Netbeans中工作.这是我的第一次,也是一个初学者,所以请对我和我的代码保持谨慎.我确实已经阅读了渲染教程等,但是找不到可行的方法,或者无法在我的小程序中使它正常工作.

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.

这是在initcomponents()之后;

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"
        }
    ));

这是我的渲染器:

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;
}
}

}

推荐答案

我不知道如何使jTable保持彩色的颜色.

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

一种方法是将信息保留在TableModel中.因此,也许您只是将Boolean.TRUE或Boolean.FALSE存储在模型中以指示选择了哪些单元格. FALSE然后,当您单击一个单元格时,使用setValueAt(Boolean.TRUE, row, column)方法更新TableModel来更改选择.

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) {
   ...

这篇关于选择新的jTable单元格后如何使其保持彩色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆