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
608 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
Comment Utility
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
Comment Utility

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

>> 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
Comment Utility


>> 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
Comment Utility
Excellent. Exactly what I was looking for. Thank you very much!
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Thanx 4 axxepting
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights  challenge 17 59
bigHeights  challenge 13 55
Fibonacci challenge 11 82
backtracking recursion  code 19 39
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)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

772 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

11 Experts available now in Live!

Get 1:1 Help Now