This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Custom Cell renderer... A solution? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Custom Cell renderer... A solution?" Watch "Custom Cell renderer... A solution?" New topic
Author

Custom Cell renderer... A solution?

P Plus
Greenhorn

Joined: May 12, 2009
Posts: 11
Hi,
As a newbie here I need to know if I code a custom cell renderer for my table, it would solve my problem.

Here is the situation:

I have a table with 2 columns, 2 combo box cells.
I have defined 2 buttons to add and remove the selected rows.
When selecting a combo box cell, it goes to edit mode and if I delete that row and then selecting another row (cell) I am thrown with this exception:
"Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1"

So I thought that a custom cell renderer would solve the problem.

Any ideas?

Cheers.
John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

Erm hard-to-tell as there's no code but I don't think the problem is in the rendering, especially if you can already see the combo boxes.

My guess is maybe the delete code isn't firing the tableRowsRemoved event but I really don't know until I see some code. Try creating a Short Complete Example (Google it) and it might help work out what's wrong.


Passed: SCJP 6 (90%), SCJD 6
Other: Spring training, extensive Swing experience
P Plus
Greenhorn

Joined: May 12, 2009
Posts: 11
Hi, here is the delete code. It is supposed to remove the row from the model, but it seems it is just removing it from the GUI. An I thinks that's why it is throwing arrayIndexOutof Bounds exception - The array inside the combo box is all messed up. Am I missing something here?
P Plus
Greenhorn

Joined: May 12, 2009
Posts: 11
Well, it seems that the rowsRemoved requires a TableModelEvent event. But I am handling that with a mouse clicked event.

Any ideas?

Cheers.
Karthick Dharani Vidhya
Ranch Hand

Joined: Feb 23, 2008
Posts: 88
I think some more code should give us the idea.


Karthick D V
P Plus
Greenhorn

Joined: May 12, 2009
Posts: 11
Alright, that's the way I have created the combo boxes and set the elements into them:



then comes the add and delete codes - see my previous posts.

Thanks.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
using just a single column for a comboBox editor, I can reproduce your problem
only if the last row is selected/deleted, then another selected after deletion.

adding this line seems to fix the problem

myjTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

as an aside, you should be using an actionListener for your button instead of a mouseListener,
it's no big deal, just consistency, but if you prefer mouseListener, note that mouseClicked
is definitely not the right choice, as it doesn't always fire. mousePressed is a far better choice.

to test mouseClicked, click and hold, move the mousePointer just a bit, now release. the event won't fire.
P Plus
Greenhorn

Joined: May 12, 2009
Posts: 11
Hi Michael,

Thanks for the tip. I changed the mouseClicked to mousePressed and added that property to the table with no luck. I then tested that same property for the combo box, and to my wonder it accepted the property but it did not solve the problem.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
about the only thing left is to post a compilable/runnable program,
so we can see exactly what's happening.

just the bare bones, comboBox to include new String[]{"a","b","c"} etc
P Plus
Greenhorn

Joined: May 12, 2009
Posts: 11
Well, if that's what you need here we go
Just ignore the first part, Check the code from line 58
P Plus
Greenhorn

Joined: May 12, 2009
Posts: 11
Sorry; was a duplicated post.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
OK, this is what I suggested you post

> about the only thing left is to post a compilable/runnable program,
> so we can see exactly what's happening.
> just the bare bones,...

here's an interesting exercise for you:

copy the code you posted
now go to another pc, one with a java jdk,
but one that doesn't have netbeans, or have any of the micrm package classes
paste the code into notepad and try to compile via the command line

now you will be seeing what we see i.e. we can do nothing with the code you posted.

if you want a solution, you'll have to post something we can compile and run.
P Plus
Greenhorn

Joined: May 12, 2009
Posts: 11
Oh, I see. Here are the bare bones (horns)
The attachment plugin of the forum is a headache. It does not accept zip nor txt nor java extensions. I doubt if it accepts any file at all.


Thanks.
P Plus
Greenhorn

Joined: May 12, 2009
Posts: 11
Bump!?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
a combination of the follwing 2 changes seems to work OK
1) adding the indicated line


2) changing the mouseListener from pressed to released (for remove)
P Plus
Greenhorn

Joined: May 12, 2009
Posts: 11
Thanks. I will give it a shot.
P Plus
Greenhorn

Joined: May 12, 2009
Posts: 11
Thank you mate. That did solve the problem.

Thanks again everyone.

Cheers.
P.
 
Consider Paul's rocket mass heater.
 
subject: Custom Cell renderer... A solution?
 
Similar Threads
JCOMBO Box traversal problem
Row Highlight on a JTable
Custom Cell Renderer for JTable
table is not redrawn
Cell renderer for different JComboBox's in same column?