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

Dani AI

Generated

A clean way to keep a checkbox column in sync with your password gate is to make the decision in the editor itself. Let the editor compare the tentative value to the model’s current value, ask for approval only when it actually changes, and if denied, cancel editing so the view snaps back to the original value. No manual toggling or fishing for the renderer state is needed, and it works whether the user clicks or uses the keyboard.

// called by JTable when the user commits the edit
@Override
public boolean stopCellEditing() {
  int r = table.convertRowIndexToModel(table.getEditingRow());
  int c = table.convertColumnIndexToModel(table.getEditingColumn());
  Boolean before = (Boolean) table.getModel().getValueAt(r, c);
  Boolean after  = (Boolean) getCellEditorValue();

  if (!java.util.Objects.equals(before, after) && !approvedByAdmin()) {
    cancelCellEditing();     // reverts view to model's original value
    return false;            // block commit
  }
  return super.stopCellEditing();
}

Wire it up once per checkbox column:

table.getColumnModel().getColumn(checkCol).setCellEditor(new AdminCheckboxEditor(table));

Two implementation tips that save time across your 50 tables: (1) make your model return Boolean.class for that column so JTable uses a checkbox editor automatically; (2) if the change truly must be protected, also enforce the rule in your model’s setValueAt as a backstop in case code attempts to flip the value programmatically. This is essentially what was aiming at with stop/cancel editing; once you gate the commit, the explicit invert suggested by is unnecessary because the editor either commits or reverts. JTable absolutely supports editable cells via editors/renderers; this pattern follows Swing’s intended design. See DefaultCellEditor and the Swing tutorial’s section on How to Use Tables for background.

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.