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
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
611 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 34
ejb on wildfly 5 30
How to determine if a string is a valid SHA value 7 35
web project error add remove 1 28
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

861 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