• Post Reply Bookmark Topic Watch Topic
  • New Topic

Issues with JButtons in JTables  RSS feed

 
Andrea Marasco
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone

Here is my problem: I have a JTable with some data in it and a JButton in the last column of the TableModel (for each row).

When you click on this JButton, other than the perfectly working application logic, I want it to become disabled (setEnabled(false);) and to have a certain text within it.

This is perfectly working. The problem enters when I have more than one row: if there is another enabled JButton in the next row to this disabled JButton and the row with the disabled JButton gets removed (tableModel.removeRow(someIndex);) then the someIndex+1 row is moved to the someIndex row BUT the new JButton that was supposed to be enabled stays disabled!

Here's what I mean:

Before removing the first row:


After removing the first row:


I want that JButton, after the row removal, to be enabled. How can I overcome to this problem?

Here it is some of my code:



Thanks to everyone that will help me in advance :-)
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrea,
Welcome to the Ranch!

At first look, I am concerned that you check if the 'cell' is an instance of JButton in this call:


This is not good. A model should hold data and not GUI components. Renderers and Editors then dictate on how that data is used. I would suggest you to go through the tutorial to understand more.

You have provided some code, but we are unable to deduce the issue from the same. I would suggest that you write a small runnable program which replicates the issue and paste the same. This is called SSCCE. This makes it easier for fellow ranchers to simply copy your code and run it rightaway - and that means they can see the issue and provide suggestions.

 
Andrea Marasco
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've solved my problem by using a MouseListener and then firing the event whenever I click on buttons (I don't know if I have to mark this thread as solved); however I'd like to talk more about this argument.

Ranganathan Kaliyur Mannar wrote:
A model should hold data and not GUI components. Renderers and Editors then dictate on how that data is used.


I know about the model-view-controller design pattern, the thing is that a JTable is a GUI component so I don't think it is that bad that the model contains GUI components. Also I think that the rendering/behavior of the components should be responsibility of the components not of the JTables, that's why I have put those JButton in the components and then I basically told the cell editor/renderer: "Whenever you encounter a JButton just let him express the way he wants".

Ranganathan Kaliyur Mannar wrote:
You have provided some code, but we are unable to deduce the issue from the same. I would suggest that you write a small runnable program which replicates the issue and paste the same. This is called SSCCE. This makes it easier for fellow ranchers to simply copy your code and run it rightaway - and that means they can see the issue and provide suggestions.


Ok then, here's a full example:



Try to create 5 buttons and then delete the third row (the one with the index 2); you will see that this example is deleting the fourth row and not the third one.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrea Marasco wrote:I know about the model-view-controller design pattern, the thing is that a JTable is a GUI component so I don't think it is that bad that the model contains GUI components.

Well, then, which one do you think represents the 'view' here? view is represented by the GUI component and the model should contain real data - not GUI components. The GUI components just use/reflect the data.
For example, a check box is a GUI component. The model would hold data via a boolean variable. The checkbox will then reflect the data - it will be checked if the boolean value is true and checked if the value is false.
 
Andrea Marasco
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you're right.

So if I want to use a single TableCellRenderer for every cell in the JTable and still use correctly the model-view-controller pattern then I would adopt a solution like this (this is just an example):



Now I'm just storing a String in the TableModel and that String is inside a class that tells the renderer to render that String inside a JButton.

This should be the correct way, isn't that? And what about my non-functioning program? And then, do I have to mark this thread as solved since I've solved my problem?
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have used an extremely difficult way to set the renderers. Instead, you can use some simpler ways:

1) use the setDefaultRenderer() method of JTable. This takes the class type as the first argument. So, you could use something like:
table.setDefaultRenderer(MyClass.class, new MyRenderer());

In this case, the renderer class would have to deal with only one column which means deal with one component.

2) Even better - you don't have to create a class like StringJButton, just for the sake of it. You could get the TableColumn instance and then set the renderer, like:
table.getTableColumn(0).setCellRenderer(<renderer>);

In this case, you attach a renderer to a specific column via the column number, so, your renderer will be specific for this column. Again, you don't have to deal with ifs and create different components here.

PS: When you are writing a renderer, you can have the class directly extend from the GUI component and simply return 'this' in the getTableCellEditorComponent method. There is no need to create instances repeatedly here. The tutorial provides examples for all cases.

Also, if there are specific cases to be dealt with, you can always use the 'column' and 'row' parameter apart from the 'value' parameter.

And, you can mark the thread solved if you think your problem is solved.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!