Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By doWhile

Thread: JTable / JCheckbox editing in groups

  1. #1
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default 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.

    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;
    	}
    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?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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)
    DarrylBurke likes this.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default 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.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JTable / JCheckbox editing in groups

    Quote Originally Posted by Diargg View Post
    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.
    So there are three states: UP, DOWN and DEFAULT.

    Quote Originally Posted by Diargg View Post
    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.
    Check out camickr's Table Stop Editing Java Tips Weblog

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default 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" ?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JTable / JCheckbox editing in groups

    Quote Originally Posted by Diargg View Post
    Perhaps a pull down
    That's what I suggested at #3.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: JTable / JCheckbox editing in groups

    Ah, by combo I somehow thought you meant radiobuttons. Thanks!

Similar Threads

  1. jcheckbox and jtable
    By seredi in forum New To Java
    Replies: 2
    Last Post: 05-05-2012, 07:08 AM
  2. how to sort jtable with row groups
    By mordjah in forum AWT / Swing
    Replies: 0
    Last Post: 10-25-2010, 05:23 PM
  3. JCheckbox in jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 3
    Last Post: 09-07-2010, 05:29 AM
  4. JCheckbox in a Jtable Column
    By kedia_rohit in forum AWT / Swing
    Replies: 1
    Last Post: 04-25-2010, 12:45 PM
  5. How to add a jcheckbox in jtable
    By lakshayghai in forum AWT / Swing
    Replies: 9
    Last Post: 04-14-2010, 11:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •