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
610 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
starter POM and spring-boot-starter,  spring-boot-web 2 54
Weekend adv creator 3 36
web application structure 18 76
Java string replace 11 43
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

786 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