开发者

java jTable : how to know the row number?

I wrote a class that implements TableCellRenderer, since I want to put checkboxes inside the jTable. In the getTableCellRendererComponent method of this class, i have the condition :

if(isSelected){ do ..... }

inside this if condition, how can I know the row number ?

code :

 private static class ValueRenderer extends JCheckBox
    implements TableCellRenderer {

    private static final Color hilite = new Color(0xE8E8E8);

    public ValueRenderer() {
        this.setOpaque(true);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean hasFocus, int row, int col) {
        Value v = (Value) value;
        this.setSelected(v.selected);

        if(isSelected)
        {
            if(v.selected==true)
            System.out.println("checked"); 
            else System.out.println("unchecked");
        }
        return this;
    }
}

  private开发者_运维知识库 static class Value {
    private Boolean selected;

    public Value(Boolean selected) {
        this.selected = selected;

    }



}


Why are you creating a Value class and a custom renderer? From the code it is just a Boolean. Just add the Boolean directly to the TableModel and then override the getColumnClass() method of the JTable or TableModel to return Boolean and the table will use the default Boolean renderer.

Read the JTable API and follow the link to the Swing tutorial on How to Use Tables for an example that uses Booleans.


Use the row parameter passed to your renderer. It's zero based just like arrays in Java.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜