-2

I am implementing a JTable containing some JButton, and I found this nice example upon which I based my table.

I tried to implement my own table to suit my needs, using the files ButtonEditor.java and ButtonRenderer.java from the example.

Actually, the column named "1" is made of buttons, and I would like to assign a different String myString to each one.

With a simple modification of the ButtonEditor class I assign the same myString to the whole column using:

table.getColumn("1").setCellRenderer(new ButtonRenderer());
table.getColumn("1").setCellEditor(new ButtonEditor(new JCheckBox(), myString));

but it's not what I want. I would like to choose the row as well. So how do I have to modify the ButtonEditor class to accomplish this ?

szlak
  • 39
  • 1
  • 6
  • Please post your best [mcve], and show, perhaps in an image, exactly what you're trying to achieve. – Hovercraft Full Of Eels Feb 04 '17 at 18:05
  • Note that the solution is to either change the editor or extend it, so that its behavior matches what you desire, but you will want show your code attempt first and specify the exact behavior you're looking for to get a decent solution. – Hovercraft Full Of Eels Feb 04 '17 at 18:06
  • It's always best for you to post your own [mcve] since you will want to make it as easy as possible for the volunteers here to understand exactly what you're trying to do and what problems you're having. Also this is a question and answer site, meaning the quality of the questions is paramount, and so you'll want to avoid having to make folks go to off-sites for the code, since that makes the question and answer less helpful to future visitors. Finally, if you find a comment "not useful" then flag it to the moderators for their action, and if they agree, they'll zap it. Good luck. – Hovercraft Full Of Eels Feb 04 '17 at 19:15
  • hmmm...On Hold. Too bad. There's a simple solution to this one. – DevilsHnd - 退職した Feb 04 '17 at 21:12
  • Why are you complaining? You've got an answer in a situation where I didn't think an answer was possible, and that's OK with me. Hopefully you're ahead and now can continue with your project. – Hovercraft Full Of Eels Feb 05 '17 at 02:57
  • @DevilsHnd, `There's a simple solution to this one.` - you can always update your question with the "simple solution" that you ended up using if the answer given here didn't help. – camickr Feb 05 '17 at 18:24
  • @camickr, It's not my question. – DevilsHnd - 退職した Feb 06 '17 at 01:47
  • @DevilsHnd, oops. Yes I also get frustrated when people keep closing questions preventing people from providing answers. Especially when you keep seeing names of the same individuals. – camickr Feb 06 '17 at 04:30

1 Answers1

3

Check out the Table Button Column for a class that allows you to render the column like a button.

You also need to provide the Action to be invoked when the button is clicked.

I would like to choose the row as well.

The row that was clicked is available in the ActionEvent.

camickr
  • 321,443
  • 19
  • 166
  • 288