Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I've already seen some examples with inserting of JComboBox and JCheckBox, but when I insert my own component there is a problem: I can click on the checkbox and button on my component but the component has the same hash-code for all cells of the table. In this case the state of every JCheckBox (selected property) is not saved because all links point to the same object (the same component). How to solve this problem ?

Here how I add a renderer and editor:

MIDL
table.getColumn(0).setCellRenderer(new MyCellComponentRenderer());

table.getColumn(0).setCellEditor(new MyCellComponentEditor(new JCheckBox()));


Here is my renderer class:

C#
public class MyCellComponentRenderer extends MyComponent implements TableCellRenderer {

      public MyCellComponentRenderer() {
       // setOpaque(true);
      }

      public Component getTableCellRendererComponent(JTable table, Object value,
                       boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
          this.setForeground(table.getSelectionForeground());
          setBackground(table.getSelectionBackground());
        } else{
          setForeground(table.getForeground());
          setBackground(Color.white);
        }


        return this;
      }
    }


Editor:

MSIL
public class MyCellComponentEditor extends DefaultCellEditor {

    private MyComponent myComponent;

    private boolean isChecked;
    private boolean isPushed;

    public MyCellComponentEditor(JCheckBox checkBox) {
        super(checkBox);

        myComponent = new MyComponent();
        myComponent.getCheckBox().setOpaque(true);
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column)
    {
        if (isSelected) {
            myComponent.setForeground(table.getSelectionForeground());
            myComponent.setBackground(table.getSelectionBackground());
        } else {
            myComponent.setForeground(table.getForeground());
            myComponent.setBackground(Color.orange);
        }

        return myComponent;
    }

    @Override
    public Object getCellEditorValue() {
        if (isPushed) {
        }
        isPushed = false;
        return new String("");
    }
    @Override
    public boolean stopCellEditing() {
        isPushed = false;
        return super.stopCellEditing();
    }
    @Override
    protected void fireEditingStopped() {
        super.fireEditingStopped();
    }
}


Component:

MSIL
public class MyComponent extends JPanel
{
    private JButton button;
    private JCheckBox checkBox;
    private JLabel label1;
    private JLabel label2;

    private boolean state;

    public MyComponent() {

        button = new JButton("A");
        checkBox = new JCheckBox("B");
        label1 = new JLabel("1");
        label2 = new JLabel("2");

        this.add(button);
        this.add(checkBox);
        this.add(label1);
        this.add(label2);

        button.setOpaque(true);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                JOptionPane.showMessageDialog(button,button.hashCode());
            }
        });
    }

    public JCheckBox getCheckBox()
    {
        return checkBox;
    }

    public void setCheckBox(JCheckBox checkBox)
    {
        this.checkBox = checkBox;
    }

    public void setState(boolean state)
    {
        this.state = state;
        checkBox.setSelected(state);
    }

}
Posted
Updated 15-Jun-23 12:16pm
v3

You should use a custom JCheckBox. That JCheckBox can have an ID, on which you can identify it:

public class CustomCheckBox extends JCheckBox {
    private final String strID;

    public CustomCheckBox(final String strID){
        super();
        this.strID = strID;
    }

    public String getID(){
        return strID;
    }
}


The ID can be whatever you want it - perhaps something defining the row or the content of the row...
Also can you extend the custom component with custom styles, naming (internationalization!) and much more. So the Component is more what you need it to be than what comes with the basic implementation.

insert as seen here in your TableCellEditor:

C#
public Component getTableCellEditorComponent(JTable table, Object value,
              boolean isSelected, int row, int column) {
        switch (column) {
            case 0:
                return new CustomCheckBox("Row1");

            //....
            default: return null;
        }
    }



regards
Torsten
 
Share this answer
 
v4
Comments
Shendor 8-Jun-11 5:37am    
And how I am supposed to set this ID ? Just look at that code where I add renderer and editor.
This is not a solution, or perhaps I just didn't understand you.
TorstenH. 8-Jun-11 5:47am    
the TableCellEditor is the interesting one - the other one is just formatting the table.

see the improvment above.
Shendor 8-Jun-11 5:55am    
now it's clearer. I'll try it a little bit later
Shendor 8-Jun-11 7:55am    
in this case it always will return the new CustomCheckBox object, but I need to save its state (property isSelected() ), so it's not what I need
TorstenH. 8-Jun-11 11:17am    
*lol* add a boolean to the dataset (each row) representing the state of the checkbox. Come on, we are not delivering complete solutions for free!
The problem was resolved. I just need to put a code which sets a new value into the database (in my case it's a boolean value) in the method: public boolean stopCellEditing() in the editor class
 
Share this answer
 
v2
how to get a boolean value is true on check box in java netbean code
 
Share this answer
 
Comments
Richard Deeming 16-Jun-23 3:56am    
Your question is not a "solution" to someone else's question.

If you want to ask a question, then ASK A QUESTION[^]. But you're going to have to provide a lot more information than you have here if you want anyone to be able to help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900