Solved

Swing: Can checkboxes in JTable return two different String values instead of just the boolean "true" and "false"?

Posted on 2008-10-29
8
609 Views
Last Modified: 2012-05-05
Can you create a column in a JTable that will use checkboxes for the editor that doesn't necessarily have values of "true" and "false."  For instance, could checkboxes be used to indicate "male" vs. "female" or "Automatic" vs. "Manual?"  If so, how does one programatically tell Java which option indicates a checkmark and which indicates a blank?

For example:
[  ] Automatic Transmission

Value of the checkbox checked would be "Automatic". Value of the checkbox unchecked would be "Manual".
0
Comment
Question by:ctangent
  • 4
  • 3
8 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 22838889
That's possible indeed.
You have to write your own TableCellEditor for that column.

Maybe inspiring:
Creating a Custom Table Cell Editor in a JTable Component: http://www.exampledepot.com/egs/javax.swing.table/CustEdit.html

In your case -  when I assume that the colunm value is a String being "Automatic" or "Manual" - MyTableCellEditor would be something like


public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
 

        // This is the component that will handle the editing of the cell value

        JComponent component = new JCheckBox("Automatic Transmission");

    

        // This method is called when a cell value is edited by the user.

        public Component getTableCellEditorComponent(JTable table, Object value,

                boolean isSelected, int rowIndex, int vColIndex) {

            // 'value' is value contained in the cell located at (rowIndex, vColIndex)

    

            if (isSelected) {

                // cell (and perhaps other cells) are selected

            }

    

            // Configure the component with the specified value

            // I assume that in your case the value is a String being "Manual" or "Automatic"

            setSelected( ((String)value).equals("Automatic") );

    

            // Return the configured component

            return component;

        }

    

        // This method is called when editing is completed.

        // It must return the new value to be stored in the cell.

        public Object getCellEditorValue() {

            return ((JCheckBox)component).isSelected() ? "Automatic" : "Manual";

        }

    }

Open in new window

0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 22838893

You can seperate this into two problems:

1. Setting checkbox in JTable. Here is a tutorial for that:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

2. Indicate male vs female: you will be better off using a combobox here. I cant imagine how you would visally implement this with a check box. What would be the name of the column? In this example it should be "gender". If that is the name of the column then how could you have a checkbox? A check box is only a square which can have a label or not. In any case, the user only sees one value at a time and can only guess the other value. Thats why it is always used as true and false.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 22838949
Some remarks on the previous post:

>> What would be the name of the column?
Name of the column could be "Manual vs. Automatic Transmission"

editor:
a checkbox with the label "Automatic"

>> In any case, the user only sees one value at a time and can only guess the other value. Thats why it is always used as true and false.
If the column name is "Automatic Transmission" and you use a check box withtout a text,
if it is selected (true), you know it is automatic transmission.
If it is not selected (false) you know it is NOT Automatic Transmission, but you don't know neither what the opposite is...
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 22838999

>> Name of the column could be "Manual vs. Automatic Transmission"

Yes, but it looks like a forced solution used only to enable the checkboxes. I'd name the column Transmission Type and have the combobox or maybe toggle button displaying the current selection.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 37

Expert Comment

by:zzynx
ID: 22839053
I agree that a combo box showing the two possibilties is also a good solution.
But imo a column named "Automatic Transmission" and a checkbox indicating true or false is also OK.
The fact that you don't know what exactly it is when the check box is not selected, shouldn't necessarily be a problem.
You know it is not automatic. That's ok.
Like a column "Vegetarian" and a check box indicating true or false. If not selected you know it is not a vegetarian.
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 22839078


>> But imo a column named "Automatic Transmission" and a checkbox indicating true or false is also OK.
>> Like a column "Vegetarian" and a check box indicating true or false. If not selected you know it is not a vegetarian.

For some things this is acceptable. But I dont think its ok to have a column, say "Male" and then a checkbox indicating you are male, and if you are not male, then we all know you are that other thing :))
0
 

Author Closing Comment

by:ctangent
ID: 31511365
Excellent. Exactly what I was looking for. Thank you very much!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 22848715
Thanx 4 axxepting
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now