I have a jtable and inside this table is a column of checkboxes (other info on following columns).
if the user clicks on the checkbox to change it, which it does, there is a popup that comes up for an administers password approval... got that. If the approval is accepted, the new value remains unchanged, if it isn't, the value of the checkbox is reverted back to its original value.
What I need is the general syntax for getting the current state of the checkbox inside that table and changing it's state

Here is what I was thinking.....

if (test == "false")  // password confirmation failed - need to reset checkbox
    if (Level_0.getValueAt(row, col) = true)  //row and col is the location of ckbox in the table Level_0
        {Level_0.setValueAt(row, col).setSelected(false);}
    else
        {Level_0.getValueAt(row, col).setSelected(true);}

Line #2 is to get current state of the checkbox.
Lines #3 and #5 reverses the state of the checkbox.

Thanks in advance for all the help

Recommended Answers

All 5 Replies

if (test == "false")

either test is a boolean, or it is a String ... either way, it is badly coded.

in case it's a boolean, you'll want one of the next options:

if ( test == false )

if ( test != true )

or, the simplest of all:

if ( !test )

if test is a String, on the other hand, you should know the basics of OO and how to use it in Java. you can compare Objects using the == operator, but it will compare the reference, not the value of the Object. so both values might be equal to "false", yet test == "false" might still return false (even though you expect it to return true).

for a String, compare it this way:

if ( "false".equals(test) )

.. in most code you'll see it like this:

if ( test.equals("false") )

but the first has one major advantage: where as test.equals("false") might throw a NullPointerException if test hasn't been instantiated, "false".equals(test) will never throw one, seeing that "false" is the object on which the method is called.

  1. is required to override cancelCellEditing() and stopCellEditing() for TableCellEditor, you can to create startCellEditing() nested proper Boolean flag

  2. otherwise, for better help sooner post an SSCCE, short, runnable, compilable, just about JFrame with JTable, invoked JOptionPane from TableCellEditor

if (Level_0.getValueAt(row, col) = true)

You have a = (assignment) where you should have == (equals) (plus then see posts above)

if (something == true) //row and col is the location of ckbox in the table Level_0
   {Level_0.setValueAt(row, col).setSelected(false);}
else
   {Level_0.getValueAt(row, col).setSelected(true);}

is a really long and tortuous way to say

Level_0.getValueAt(row, col).setSelected(! something);

Thanks a bunch all of ya.... got it working.

this bit of code is needed for over 50 different tables in my project in one form or another.

Standard JTable does not come with an interactive mode such as those found in simple spreadsheets. Adding a simple record oftentimes requires an 'Add' button, a dialog box asking for values then updating the JTable itself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.