?
Solved

[Java]Problem with DefaultTableModel(TableModel)

Posted on 2007-10-06
21
Medium Priority
?
1,233 Views
Last Modified: 2013-11-23
Hi,

I have a problem with the function getColumnClass() of TableModel. I have a column(in my JTable)
 which will store a jcheckbox value, so i create that column and put the value of it is true
 or false.Then in my TableModel i declare the function:
      public Class getColumnClass(int c) {
                   return getValueAt(0, c).getClass();
    }

Well, the true and false value above will be the JCheckBox, but that function also make
the datetime value(which i retrieve from DB) turn into text-based, i.e: it's will show October 7,2007
for 2007-10-7 -00:00:00.000 .And i could not do any edit in that datetime value?Anyone please
help me how i can make the getColumnClass function only effect with the checkbox column?
I also tried to put the checkbox column position on that function but it don't work!
0
Comment
Question by:cassiej81
  • 9
  • 9
  • 3
21 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20029411
you can explicitly set the editor for a column using:

    TableColumn col = table.getColumnModel().getColumn(vColIndex);
    col.setCellEditor(new MyTableCellEditor());
0
 
LVL 92

Accepted Solution

by:
objects earned 750 total points
ID: 20029414
similiar for the renderer

    TableColumn col = table.getColumnModel().getColumn(vColIndex);
    col.setCellRenderer(new MyTableCellRenderer());
0
 

Author Comment

by:cassiej81
ID: 20029432
Thank objects,
But what's MyTableCellRenderer() ? How can i declare and using it?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Expert Comment

by:objects
ID: 20029564
Its the custom renderer you want to render that column
0
 

Author Comment

by:cassiej81
ID: 20029845
Can you please tell me what exactly i need to do to make it work?
 I tried:
      JCheckBox checkBox = new JCheckBox();
      DefaultCellEditor checkBoxEditor = new DefaultCellEditor(checkBox);
        TableColumn col = table.getColumnModel().getColumn(8);
      col.setCellEditor(checkBoxEditor);
With column i want to turn it into JCheckBox in position 8. But it's not work?


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20030151
public Class getColumnClass(int c) {
      return c = intZeroBasedIndexOfCheckboxColumn? Boolean.class : getValueAt(0, c).getClass();
}

Bear in mind your approach could lead to NPE problems though if your model is empty
0
 

Author Comment

by:cassiej81
ID: 20030373
Sorry but what's intZeroBasedIndexOfCheckboxColumn? I'm newbie in Java :(, please explain!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20030379
It means the number that's the index of the checkbox column
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20030388
If it were the first column

return c = 0? Boolean.class : getValueAt(0, c).getClass();
0
 

Author Comment

by:cassiej81
ID: 20030403
I tried, but the Eclipse said "Type mismatch: cannot convert from int to boolean"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20030412
Sorry - typo

return c == 0? Boolean.class : getValueAt(0, c).getClass();
0
 

Author Comment

by:cassiej81
ID: 20030432
Still could not solve :((
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20030452
Is the checkbox showing in the right column?
0
 

Author Comment

by:cassiej81
ID: 20030474
yes, it's still show the checkbox but also return to my problem:

"Well, the true and false value above will be the JCheckBox, but that function also make
the datetime value(which i retrieve from DB) turn into text-based, i.e: it's will show October 7,2007
for 2007-10-7 -00:00:00.000 .And i could not do any edit in that datetime value?"

Please help!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20030482
I don't understand the relationship between a checkbox and a date ..?
0
 

Author Comment

by:cassiej81
ID: 20030496
Ah yes, if i don't use the  getColumnClass function, the datetime value(which retrieve from DB) will in the correct format is 2007-10-7 -00:00:00.000 , but the column which i want be a CheckBox will still in true/false
value.

And when i use the getColumnClass function, the CheckBox column value will turn into CheckBox, with true value is checked and false value is unchecked but i think that function  also make the datetime value turn into another format is "October 7,2007 " and i could not do any edit with this.

So i want to render only the CheckBox column and that is problem now?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 750 total points
ID: 20030588
The two things are not connected. The code i gave you will supply the column with a check box, but if your table model has a Date type in it (as opposed to a String representation of a Date) you won't be able to edit properly since there's no default implementation of a cell editor for a Date type. If you turn it into a String, you won't have a problem
0
 

Author Comment

by:cassiej81
ID: 20030605
ah, yeah i see that, but if i store datetime in varchar type, it's will limit some search ability. But i will thinking about it.

Anyway, i will split the point for you and objects, thank your guy very much :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20030629
:-)

>>but if i store datetime in varchar type ...

Oh no, you wouldn't do that. You'd just put it in the model as a String, or better, make your own Date CellEditor
0
 

Author Comment

by:cassiej81
ID: 20030643
Thank CEHJ, but i use rowUpdate() to made a update from Table to DB so if i put it in the model as String may lead to the update will not work well, and the DateCellEditor seem too complex for a newbie like me :P

So i think i will change type in my db and when i learn more about Java , i will back to resolve it follow the solution you gave :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20030691
Don't do that. You can use the toedter cell editor

table.getColumnModel().getColumn(2).setCellEditor(new com.toedter.calendar.JDateChooserCellEditor());

http://www.toedter.com/en/jcalendar/index.html
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

621 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