2

How could I put a JPanel into a cell of a JTable?
(There would be a column what would contain String objects, and an other column with JPanel objects. )

kleopatra
  • 51,061
  • 28
  • 99
  • 211
user
  • 6,567
  • 18
  • 58
  • 85

2 Answers2

1

http://download.oracle.com/javase/tutorial/uiswing/components/table.html#renderer

Read the part that talks about using Custom Renderers. The ColorRenderer example puts a JLabel in a cell. You could easily change that to use a JLabel instead.

Shahriar
  • 280
  • 2
  • 9
  • Now, I've successfully added a `JPanel` with a `JButton` to the cell, but only the upper-half of the button is visible, and if I click on the cell, it does nothing. Is it possible to make the button on the panel "clickable"? – user Sep 26 '10 at 11:20
  • 1
    can't you just add an ActionListener to the button? But I guess you will need to pass in the table's row, column info to the listener as well – Shahriar Sep 27 '10 at 19:59
  • 1
    I do not believe that you can actually interact with a JPanel inside of a table cell doing this. If I am wrong please let me know, because I am trying to achieve the same thing. – user489041 Apr 28 '11 at 20:54
  • @user489041 hah? Is that true?? Impossible to interact with the component inside of the jpanel (within the JTable cell)...?? – gumuruh Jan 20 '12 at 03:42
  • @gumuruh I believe so. Its been a while since I attempted this. I think it just uses an image of the panel and its contents. – user489041 Jan 24 '12 at 16:22
  • @user489041 but now i'm trying to do what they mentioned about applying a jpanel inside cell's jtable... Loook: http://stackoverflow.com/questions/8923089/swing-jtable-custom-rendering But, I agree that this is quite complex things to do. – gumuruh Jan 30 '12 at 07:18
  • 1
    @gumuruh In all honesty, I found it easier to just create my own table class. I just used JPanels as cells, and a GridLayout to organize them. Worked out pretty well and allowed for the all contents in the cells to be interactive. If I have time later, ill see if I can dig it up. – user489041 Jan 30 '12 at 15:47
  • 1
    You won't success trying to interact with renderers. You need to interact with editors instead. Read the JTable documentation about that. I'm finishing a project that uses a table with differents editors/renderers, as soon as I have the code finished I will post that up. – Sebastian Feb 19 '12 at 08:51
0

Actually you can do that by adding an editor to the data type that the panel covers

divjscr
  • 51
  • 3