Displaying boolean data as 2 Radio Buttons within a JTable
Hi All,
I am having difficulties with displaying boolean data in a table as a pair of radio buttons. I have tried asking parts of the question on here but havent got where i need to. So i will try and very briefly explain what i need and then will gladly take any pointers.
I have a JTable which is populated with a call to a database - this returns a few things - one of which is a boolean. I would like this to be displayed as a pair of radio buttons.
I can change the database call to return an extra boolean field which has the negation of the first if that makes life simpler.
At the moment i have a rather bodged situation where i have the boolean value plus its negation coming back from the db. Each of these columns has a single radio button in them. I have some eventListeners which attempt to simulate a radio button - so if one is selected the other is deselected. But it would really be much better if i could have a button group which takes care of the selection/deselection of the other button.
If you need any snipets of code which i am using i can supply them - but not whole classes (as there is client specific data in most classes!)
Any help would be very much appreciated!
[1236 byte] By [
marsh_1sta] at [2007-11-27 3:05:23]

# 1
Read up on table model, table cell renderers, table cell editor, and JTable usage in general.
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableModel.html
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableCellEditor.html
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableCellRenderer.html
IMHO, the default behavior that displays a checkbox for boolean values is much more elegant than two radio buttons, though.
# 2
Okay well cheers for those links .. i will attempt to find what i am looking for.
As to whether i should use a radio button or not ..... in this case the boolean in the db isnt representing something perfectly simple like Light on/off. Its more like car/plane - so, to the user, saying car = false (unchecked) does not imply plane. And so the user has to be shown the possibilities. And it would be inefficient to store the negation of boolean car as boolean plane when you can easily derive one from the other.
That probably only makes sense to me but thats the way i have to do it.
# 3
This is your third posting on this topic. If you actually told us your requirement then maybe we can come up with solution. As it is now you are wasting everybodies time with such a wierd approach (as everybody has comment on in your previous postings) that we have no idea what to suggest
So what exactly is your requirement?
a) Does the user have a list of items to select from.
b) Does the user have to select:
- one item (in which case a button group should be used and I gave you a solution for that earlier)
- one item or no items (in which case a combo box should be used)
If you can't describe your requirement, then you probably don't have a valid requirement.
# 4
> ..... in this case the boolean in the db isnt representing something
> perfectly simple like Light on/off. Its more like car/plane - so, to the
> user, saying car = false (unchecked) does not imply plane.
> And so the user has to be shown the possibilities.
It sounds like there should be a checkbox column for each choice.
And what if another choice comes later, like bike or bus?
If they have multiple choices, but can only pick one, then you should use a combobox as your editor, rather than radios.
Just my 2c.
# 5
I happen to be doing something that may be similar to what you're looking for. I need a cell renderer for cells with Boolean data to indicate yes or no for a permission the user can set. However, I don't want to use the default Boolean renderer with a singe JCheckBox that indicates yes or no according to whether or not it is checked. I actually want the user to see which of three states is selected: 1) User has not made a selection 2) User has selected yes 3) user has selected no. (I'm doing this because if the user has not made a selection, there is a default permission that will apply.)
So I'm guessing from your description that you have a Boolean value in your cell that if true indicates car but if false indicates airplane. If so, then the simple renderer I wrote will do this for you. You can specify it as the renderer for a specific column using JTable.getColumnModel().getColumn(n).setCellRenderer(new BooleanRenderer())
or you can specify it as the default renderer for all columns with data of type Boolean using JTable.setDefaultRenderer(Boolean.TYPE, new BooleanRenderer())
Below is the code for BooleanRenderer. I changed the values from yes/no to car/airplane, but you can set up any binary pair of values this way (e.g., cat/dog, up/down, Red/Blue). Also note that my code results in no box being checked if the Boolean value in the cell is null. You can program different behavior for null values if desired. You'll probably want to implement a cell editor in pretty much the same way, except that it will read the user input and set the Boolean cell value accordingly. You would probably also want to uncheck one box when the other box is checked, and set the cell value according to the last box that was checked when editing stopped. Read the tutorial section on custom editors to see how to do that.
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public class BooleanRenderer extends JPanel implements TableCellRenderer{
private JCheckBox carBox, apBox;
public BooleanRenderer() {
super();
carBox = new JCheckBox("Car");
apBox = new JCheckBox("Airplane");
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(Box.createRigidArea(new Dimension(3,0)));
add(carBox);
add(Box.createRigidArea(new Dimension(3,0)));
add(apBox);
add(Box.createHorizontalGlue());
}
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if ( ((Boolean)value) == null){
carBox.setSelected(false);
apBox.setSelected(false);
}else if ( ((Boolean)value).booleanValue() == true){
carBox.setSelected(true);
apBox.setSelected(false);
}else if ( ((Boolean)value).booleanValue() == false){
carBox.setSelected(false);
apBox.setSelected(true);
}
return this;
}
}
# 6
Thankyou for all your input and especially MidnightJava who managed to work out basically what i need.
Incase you havent wondered i am not exactly old hand when it comes to gui programing and so still feeling my way.
Basically the users choice is to make this person either a PA or a BUA. And it has been specified to me and i agree that this should be represented as a RadioButton group (it could also be done via a combo box). This has to be done to an existing product and so i cannot simply and easily change the way the how form works.
My first instinct was to try and create a renderer / editor which would return a Radio Button group directly to the jtable - however this did not work because a RadioButton group is not a component.
After looking through the bits and bobs i have been so kindly sent or pointed in the direction of, i think i can see where i was going wrong.
The i think i need to do it is to create a class which extends JPanel which contains the radio button group and all the methods to change the selection and then get the CellEditor to return this JPanel with the buttongroup contained within it? - then use the methods defined within this class from the renderer to select the correct button within the group depending on the boolean value etc
