Results 1 to 8 of 8
- 05-15-2012, 03:21 PM #1
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
JTable / JCheckbox editing in groups
One of the UI elements in my program displays a table with two boolean values included. There are other String elements in the same row. JTable automatically creates checkboxes for the boolean values, but I would like to change their behavior slightly. Namely, up to 1 of them can be selected - the item can't be both "up" and "down", but it can be neither (nothing checked). Would you suggest I modify the behavior of JTable for this? I've tried accessing the checkboxes themselves, but I can't seem to find the right way in through the JTable API. Right now I've got code that:
- takes in the value
- Checks the table to see if the same row, opposite boolean is true
- If so, set it to false and set this one to true
- If not, toggle this boolean.
Seems like it's only a few lines of code (below), so I'm not worried about complexity. Just wanted to make sure that there's not an easier way.
The issue with the above code is that it acts a bit... wonky? Whenever I click on the checkbox, it flies to the left hand side of the cell, and remains there until it loses focus. It also takes a second click, sometimes, to make it actually toggle. If (and this is an issue for all of the cells) it still has focus after I hit "OK" and enter the save method, I lose the data stored there (it doesn't seem to update the model). Any clues?Java Code:private JCheckBox flag; public BoolEditor() { flag=new JCheckBox(); flag.setEnabled(true); } //Implement the one CellEditor method that AbstractCellEditor doesn't. public Object getCellEditorValue() { return flag.isSelected(); } //Implement the one method defined by TableCellEditor. public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row,int column) { int otherBool; if(column==2) otherBool=3; else otherBool=2; if((boolean) table.getValueAt(row, otherBool)){ table.setValueAt(false, row, otherBool); flag.setSelected(true); } else{ flag.setSelected( ! flag.isSelected()); } return flag; }
- 05-15-2012, 05:02 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: JTable / JCheckbox editing in groups
Moved to AWT / Swing
You don't define how you construct your JTable, but you might consider adding this constraint on the data/model side, rather than the view side - when the model gets updated the constraint is applied, which then can tell the view it needs updating (fireTableDataChanged() method in AbstractTableModel)
- 05-15-2012, 06:17 PM #3
Re: JTable / JCheckbox editing in groups
I second doWhile's recommendation to code the constraint into the model. However, I wouldn't try to cascade changes; it should be trivial to return false from isCellEditable() for one boolean cell when the other one in the same row has a value of true. That way, you would have to uncheck one before you can check the other -- not quite the same as your stated requirement.
Quite apart from that, I think I would go for a combo with 3 entries corresponding to the 3 permissible states.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-15-2012, 06:32 PM #4
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: JTable / JCheckbox editing in groups
I added the constraints model side, and made them cascade. The logic behind this is that if the user want it to be 'up', then clearly they don't want it to be 'down'. Thus if they select up, we can safely deselect down. Would you recommend differently so as to prevent user errors?
I didn't go with three because the states aren't related outside of being exclusive. The default option is simply a lack of the other two states.
Unfortunately I still have a problem with the cells not finalizing the edits made. Is there a way to finalize the model before reading it in? The only thing I can think of is making an editor that updates the model as the user types.
- 05-15-2012, 07:14 PM #5
Re: JTable / JCheckbox editing in groups
So there are three states: UP, DOWN and DEFAULT.
Check out camickr's Table Stop Editing « Java Tips Weblog
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-15-2012, 07:47 PM #6
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: JTable / JCheckbox editing in groups
Thank you for that second one, I don't know how I didn't find that while googling - it's the first result on "jtable stop editing"...
Re: state count - I still disagree, but I see what you're saying. I just don't see the utility in adding a 3rd option - it seems that it would simply be confusing for the user. They're extra flags, not general states, my initial analogy was a poor one. Logically, the object would default to expected behaviour without these. One is a pre-processing flag, the other is a guideline that's enforced via making another segment uneditable. I suppose precedence exists for both use cases - if you're customizing a car, you don't need 2 checkboxes for a spoiler "I want one || I don't want one" - It's assumed.
Perhaps a pull down "Default || Flag || Preprocess" ?
- 05-15-2012, 09:10 PM #7
- 05-15-2012, 09:11 PM #8
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Similar Threads
-
jcheckbox and jtable
By seredi in forum New To JavaReplies: 2Last Post: 05-05-2012, 07:08 AM -
how to sort jtable with row groups
By mordjah in forum AWT / SwingReplies: 0Last Post: 10-25-2010, 05:23 PM -
JCheckbox in jtable
By anilkumar_vist in forum Advanced JavaReplies: 3Last Post: 09-07-2010, 05:29 AM -
JCheckbox in a Jtable Column
By kedia_rohit in forum AWT / SwingReplies: 1Last Post: 04-25-2010, 12:45 PM -
How to add a jcheckbox in jtable
By lakshayghai in forum AWT / SwingReplies: 9Last Post: 04-14-2010, 11:32 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks