aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Adding a JButton (Interactive) to a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding a JButton (Interactive) to a JTable" Watch "Adding a JButton (Interactive) to a JTable" New topic
Author

Adding a JButton (Interactive) to a JTable

Tim-Ting Chang
Greenhorn

Joined: Jan 13, 2009
Posts: 25
I am having problem with adding an interactive JButton to a JTable. I am able to render the look of JButton using CellRenderer, but it is not interactive. I try to use CellEditor but currently JButton is still not clickable.

I'll paste the main function, my implementation of the CellEditor class, and my tableModel source codes. Rendered output is attached as .jpg. If the code seems too long, take a look at .jpg and it may be a bit more clear.







[Thumbnail for out.jpg]

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Please read your private messages regarding an important announcement.


As for your problem, you editor is returning a label. That doesn't feel right, does it?

I've created my own table button renderer / editor class, and this is basically how the editing works:
- getTableCellEditorComponent returns a JButton - you still want to show a JButton after clicking
- getCellEditorValue does the actual handling

A short excerpt of that class:
The pushed field was required to prevent getCellEditorValue to trigger button clicks multiple times.

Also, the fireButtonPressed() method marked by // 1 fires a special event I created. That allowed me to use the editor class for any kind of button in JTables.


Moving to the Swing forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tim-Ting Chang
Greenhorn

Joined: Jan 13, 2009
Posts: 25
As for your problem, you editor is returning a label. That doesn't feel right, does it?


I thought I return a new JButton() when the BUY SELL or QUANTITY column is selected. I use the if statement to ensure that.



I think I understand the rest of the code, but I don't understand why we need:



Since the only way to handle a event I've learned so far is through an ActionListener object, which gets added to the button of interest. When are these two functions called? I be appriciated if you can explain, because I have been buffled by the meanings of these functions for days.

Thanks for any help
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Tim-Ting Chang wrote:
As for your problem, you editor is returning a label. That doesn't feel right, does it?


I thought I return a new JButton() when the BUY SELL or QUANTITY column is selected. I use the if statement to ensure that.


Sorry, I missed those JButton returns.

I think I understand the rest of the code, but I don't understand why we need:



Since the only way to handle a event I've learned so far is through an ActionListener object, which gets added to the button of interest. When are these two functions called? I be appriciated if you can explain, because I have been buffled by the meanings of these functions for days.

Thanks for any help

The getCellEditorValue method is the one that does the actual triggering. The problem with adding an ActionListener to the JButton is that the first click calls getCellEditorComponent, and does not fire an action performed event. I once solved that by using a mouse listener on the JTable, but my new class is more elegant - all button functionality in one single class.
Tim-Ting Chang
Greenhorn

Joined: Jan 13, 2009
Posts: 25
Sorry, I missed those JButton returns.


So where do you think the problem is? If I return a new JButton() and new JSpinner() instance, why aren't they clickable? In general, what do I need to ensure that a JButton or a JSpinner component is clickable on a JTable?

The getCellEditorValue method is the one that does the actual triggering. The problem with adding an ActionListener to the JButton is that the first click calls getCellEditorComponent, and does not fire an action performed event. I once solved that by using a mouse listener on the JTable, but my new class is more elegant - all button functionality in one single class.


so what you are saying is that even if I add an ActionListener to the JButton or the JSpinner, the actionPerformed method will not get executed? or that the actionPerformed is invoked by a 2nd click, preceeded by invokation of getCellEditorComponent()?

If you known of any good tutorial on JTable, please let me know.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Tim-Ting Chang wrote:so what you are saying is that even if I add an ActionListener to the JButton or the JSpinner, the actionPerformed method will not get executed? or that the actionPerformed is invoked by a 2nd click, preceeded by invokation of getCellEditorComponent()?

I haven't tried the combination of actionPerformed and getCellEditorComponent, so I don't know which one goes first. I do know that the first click does indeed not invoke actionPerformed - only every other click afterwards.

If you known of any good tutorial on JTable, please let me know.

Try http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
But I don't think anything like button renderers / editors is mentioned there. After using Google I found this java2s.com example which was actually my inspiration.
Tim-Ting Chang
Greenhorn

Joined: Jan 13, 2009
Posts: 25
I believe I've isolated the sympton, but I do not have the solution.

If I insert a println statement to my getCellRendererComponent(), I can see that the function gets called 9 times, which corresponds to the number of JButtonand JSpinner I added. But when the println statement is inserted to getCellEditorComponent(), there is no output; that implies, for some reason, the getCellRendererComponent() doesn't get called.

I studied the code you pointed at and I tried everything to make my code look like the examples', the example code does exactly what I want, but when i modify my own to follow the example code, nothing happens.

is this anything to do with isCellEditable() function?

if it is, then shouldn't it be handled by DefaultCellEditor class which I am extending from?

I don't know what to do, I've been stuck on this for days now.

Thanks for your help, I'll keep trying but if you have any insight, please let me know.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Tim-Ting Chang wrote:If I insert a println statement to my getCellRendererComponent(), I can see that the function gets called 9 times, which corresponds to the number of JButtonand JSpinner I added. But when the println statement is inserted to getCellEditorComponent(), there is no output; that implies, for some reason, the getCellRendererComponent() doesn't get called.

I studied the code you pointed at and I tried everything to make my code look like the examples', the example code does exactly what I want, but when i modify my own to follow the example code, nothing happens.

is this anything to do with isCellEditable() function?

Yes it is. If a cell is not editable, it's cell editor will never be activated. For buttons and spinners in JTables to work, the cells must be editable.

if it is, then shouldn't it be handled by DefaultCellEditor class which I am extending from?

You're not extending DefaultCellEditor, you're implementing TableCellEditor.
Tim-Ting Chang
Greenhorn

Joined: Jan 13, 2009
Posts: 25
I have modified my code to make it extends DefaultCellEditor. and I have extended the isCellEditable() both here, and in my TableModel.
The example code uses extends only, no implements. So I extends my InputEditor to see if it will work, given that implements doesn't work.

The code that you gave me extends DefaultCellEditor. I have seen it working. and I did everything that I can to make my code look like the example code. but my InputEditor class doesn't work, because getCellEditorComponent() doesn't get called.

I've declared a isCellEditable() and make it return true always. so what's there left to do?

There is gotta more to it.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

That's not what the example code on java2s.com does, is it? It is this method that does the actual work.

Also, you might want to move the addActionListener call to the constructor. Now you're adding a new action listener each time the editor is shown. If you press a lot, that will be a lot action listeners.


No idea though why your editor never gets called. Anybody else?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding a JButton (Interactive) to a JTable