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
612 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

730 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