Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9182087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:28:40+00:00 2026-06-17T18:28:40+00:00

I am fairly new to java, so please excuse me for any foolish mistakes

  • 0

I am fairly new to java, so please excuse me for any foolish mistakes i make …

I have a JTable of 2 columns in which the 2nd column can have 4 types :

  • a column in which values cannot be changed
  • a column which can contain any text string
  • a column which contains JComboBoxes
  • a column which contains JCheckBoxes

The column types are dynamically since they depend on the data being received.

All types work fine, except the JCheckBox.
When I replace the JCheckBox with a JComboBox with 2 choices, then it works fine.
So i guess there is a problem in the code for the JCheckBox.

The problem is :

  • when the JCheckBox is checked, then I need only 1 click to uncheck it
  • however when the JCheckBox is unchecked, then I need to click it twice (not doubleclick) to check it

EDIT

An example project (as small as i can make it) :

    import java.awt.Component;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;

    public class frmCheck extends JApplet
    {
        JTable mgrdData;
        DefaultTableModel mtableModel;

        public void init()
        {
            mtableModel = new DefaultTableModel();
            mtableModel.setColumnCount(2);
            mtableModel.setRowCount(4);
            mgrdData = new JTable(mtableModel);
            mgrdData.getSelectionModel().addListSelectionListener(new RowListener());
            add(mgrdData);
            for (int i=1;i<mtableModel.getRowCount();i++)
            {
                addCheck(1);
            }
        }

        private class RowListener implements ListSelectionListener
        {
            public void valueChanged(ListSelectionEvent event)
            {
                if (event.getValueIsAdjusting()) return;
            }
        }

        private void addCheck(int intCol)
        {
            mgrdData.getColumnModel().getColumn(intCol).setCellRenderer(new TableCellRenderer()
            {
                public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean isFocused,int row,int col)
                {
                    JCheckBox rendererComponent = new JCheckBox();
                    String strVal="";
                    if (value!=null) strVal = value.toString();
                    if (strVal.equals("1"))
                    {
                        rendererComponent.setSelected(true);
                    } else
                    {
                        rendererComponent.setSelected(false);
                    }
                    return rendererComponent;
                }
            });
            DefaultCellEditor cellEditor = new DefaultCellEditor(new JCheckBox());
            cellEditor.setClickCountToStart(1);
            cellEditor.addCellEditorListener(new CellEditorListener()
            {
                public void editingCanceled(ChangeEvent e) {}
                public void editingStopped(ChangeEvent e)
                {
                    JCheckBox checkBox = (JCheckBox)((DefaultCellEditor)e.getSource()).getComponent();
                    System.out.println("isSelected = " + checkBox.isSelected());
                    if (checkBox.isSelected())
                    {
                        System.out.println("Sent 0");
                    } else
                    {
                        System.out.println("Sent 1");
                    }
                }
            });
            mgrdData.getColumnModel().getColumn(intCol).setCellEditor(cellEditor);
        }
    }

The html page which i use to view the Applet is :

<html>
  <body>
    <applet code="frmCheck.class" width="1016" height="822"></applet>
  </body>
</html>

To make it an executable (application) you can add the following main() function to it (above the init() function, and below the declaration of mtableModel)

        public static void main(String[] args) {
        frmCheck myApplet = new frmCheck(); // define applet of interest
        Frame myFrame = new Frame("Applet Holder"); // create frame with title

        // Call applet's init method (since Java App does not
        // call it as a browser automatically does)
        myApplet.init();    

        // add applet to the frame
        myFrame.add(myApplet, BorderLayout.CENTER);
        myFrame.pack(); // set window to appropriate size (for its elements)
        myFrame.setVisible(true); // usual step to make frame visible

      } 

When you click on an unchecked checkbox, then the console shows :

isSelected = true
Sent 0

after that the checkbox is still unchecked
when i click the checkbox again (thus for a second time), then the console shows :

isSelected = false
Sent 1

the checkbox flashes as checked for a short time, and then becomes unchecked again
when i then click the checkbox again (thus for a third time), then it behaves like being clicked for the first time

I want the checkbox to :
– send 1 when unchecked and clicked
– Send 0 when checked and clicked

The real project is a bit more complex : by checking the JCheckBox I send a message to a device, which answers with its current state, which is processed and shown as a checked JCheckBox.
The processing works fine as when I change the state in the device via another channel, then the JCheckBox shows the current state perfectly

I would like the JCheckBox to respond to 1 click :

  • when it’s checked and I click it then it should send “REG SCH 4 = 0”
  • when it’s unchecked and I click it then it should send “REG SCH 4 = 1”

Ok here is the code i have so far :

The JTable is created as follows :

JTable mgrdData;
DefaultTableModel mtableModel;
mtableModel = new DefaultTableModel(null,new String[0]);
mgrdData = new JTable(mtableModel);
mgrdData.getSelectionModel().addListSelectionListener(new RowListener());
mgrdData.setFillsViewportHeight(true);
String[] strHeader = {"Naam","Waarde"};
mtableModel.setColumnIdentifiers(strHeader);

The columns are configured as follows :

        addReadOnly(0);
//          addCombo(1,new String[]{"UIT","AAN"}); //this works fine
        addCheck(1);

Column 0 is readonly, and column 1 has 2 possible values
where addCombo(1,new String[]{“UIT”,”AAN”}); perfectly works

The functions addReadOnly and addCombo and addCheck are as follows :

private void addReadOnly(int intCol)
{
    JTextField txtField = new JTextField();
    txtField.setEditable(false);
    DefaultCellEditor cellEditor = new DefaultCellEditor(txtField);
    mgrdData.getColumnModel().getColumn(intCol).setCellEditor(cellEditor);
}

private void addCombo(int intCol,final String[] strItems)
{
    //add combobox
    mgrdData.getColumnModel().getColumn(intCol).setCellRenderer(new TableCellRenderer()
    {
        public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean isFocused,int row,int col)
        {
            JComboBox rendererComponent = new JComboBox(strItems);
            if (value!=null) rendererComponent.setSelectedItem(value.toString());
            return rendererComponent;
        }
    });
    DefaultCellEditor cellEditor = new DefaultCellEditor(new JComboBox(strItems));
    cellEditor.setClickCountToStart(1);
    cellEditor.addCellEditorListener(new CellEditorListener()
    {
        private boolean blnChanged=false;
        public void editingCanceled(ChangeEvent e) {}
        public void editingStopped(ChangeEvent e)
        {
            if (blnChanged==true)
            {
                JComboBox comboBox = (JComboBox)((DefaultCellEditor)e.getSource()).getComponent(); 
                sendVal(String.valueOf(comboBox.getSelectedIndex()));
                blnChanged = false;
            } else
            {
                blnChanged = true; 
            } 
        }
    });
    mgrdData.getColumnModel().getColumn(intCol).setCellEditor(cellEditor);
}

private void addCheck(int intCol)
{
    //add checkbox
    mgrdData.getColumnModel().getColumn(intCol).setCellRenderer(new TableCellRenderer()
    {
        public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean isFocused,int row,int col)
        {
            JCheckBox rendererComponent = new JCheckBox();
            String strVal="";
            if (value!=null) strVal = value.toString();
//              if (strVal.equals("AAN"))
            if (strVal.equals("1"))
            {
                rendererComponent.setSelected(true);
            } else
            {
                rendererComponent.setSelected(false);
            }
            return rendererComponent;
        }
    });
    DefaultCellEditor cellEditor = new DefaultCellEditor(new JCheckBox());
    cellEditor.setClickCountToStart(1);
    cellEditor.addCellEditorListener(new CellEditorListener()
    {
        public void editingCanceled(ChangeEvent e) {}
        public void editingStopped(ChangeEvent e)
        {
            JCheckBox checkBox = (JCheckBox)((DefaultCellEditor)e.getSource()).getComponent();
            System.out.println("isSelected = " + checkBox.isSelected());
            if (checkBox.isSelected())
            {
                sendVal("0");
                System.out.println("Sent 0");
            } else
            {
                sendVal("1");
                System.out.println("Sent 1");
            }
        }
    });
    mgrdData.getColumnModel().getColumn(intCol).setCellEditor(cellEditor);
}

Test results after 3 times clicking :

JCheckBox is checked, result after 1 click :

isSelected = true
Sent : REG SCH 4 = 0
Sent 0

After this the JCheckBox is unchecked

JCheckBox is unchecked, result after 1 click :

isSelected = true
Sent : REG SCH 4 = 0
Sent 0

After this the JCheckBox is still unchecked

JCheckBox is unchecked but clicked once already, result after the second click :

isSelected = false
Sent : REG SCH 4 = 1
Sent 1

After this the JCheckBox is checked

I search and found solution for when you always needed 2 clicks, or solutions with fixed checkboxes instead of dynamically, but not a case like mine ….

could anyone please shed a light ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:28:41+00:00Added an answer on June 17, 2026 at 6:28 pm

    Answer

    Problem was that you were treating the checkbox value as a String when it’s a boolean.

    Working code is the following:

    Boolean val = new Boolean(false);
    if (value != null){
        val = (Boolean) value;
    }
    if (val.booleanValue()) {
        rendererComponent.setSelected(true);
    }else{
         rendererComponent.setSelected(false);
    }
    return rendererComponent;
    

    Old answer

    I’m still looking at it, but I think the problem might be due to the fact that the JCheckBox is selected or not depending on:

    if (strVal.equals("AAN"))
            {
                rendererComponent.setSelected(true);
            } else
            {
                rendererComponent.setSelected(false);
            }
    

    Isn’t "AAN" a value for the dropbox instead than for the checkbutton?

    An also, you’re creating a new JCheckBox each time.

    Maybe a SSCCE could be useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still fairly new to Java and have a question in regards to JTable
I have been working on Eclipse recently. I am fairly new to java programming,
I'm fairly new to programming in general but have been working with Java for
I'm fairly new to Java development, and people have been suggesting I use Eclipse
I am fairly new to java so bear with me please, basically, below I
I am a fairly new java programmer. I only have about five weeks of
I am fairly new to java and I have been struggling with this exercise
Fairly new to Java, so I hope the wording makes sense. I have some
I'm fairly new to Java & Android, so I have little idea what I
I am fairly new to Java. I have constructed a single JUnit test class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.