1

I have a table which has both button and text box in some cells. Now I am able to put both the components in the table cell. Thanks for OscarRyz's code. Now my problem is I have to align these components such that the button is to extreme right of the cell and text box should start from the extreme left of the cell to the start of the button. (In simple, they should be adjacent to each other and occupy entire cell.) How can I align them inside the cell?

Currently it looks like this,

enter image description here

But what I was looking is for the view something like this,

enter image description here

I have looked at Component Border code but can't figure out how to use it for the table.

Any help will be appreciated.

Community
  • 1
  • 1
Amarnath
  • 8,736
  • 10
  • 54
  • 81

2 Answers2

4

A JPanel with BorderLayout where you put the JTextField in the BorderLayout.CENTER and the JButton in the BorderLayout.EAST should do it.

And for future reference: bookmark the Visual guide to layout managers if you are looking for a LayoutManager

Robin
  • 36,233
  • 5
  • 47
  • 99
  • You mean to say that I should add jpanel to the table cell and then add both these components to that panel. – Amarnath Sep 27 '12 at 13:35
  • 1
    I mean that if the renderer would return a panel as described above you would obtain the desired result. – Robin Sep 27 '12 at 13:38
  • Thanks Robin .. u made me know table cells are not only for data but also for panels or may be what ever we want to insert .. :) – Amarnath Sep 28 '12 at 05:49
  • @Che `ClassCastException` comes from `myEditor.setCompValues((MyComponents)value);`. The values passed in that method are the values contained in your `TableModel` – Robin Sep 28 '12 at 06:16
  • But my table model contains many columns and I need to have button and text-box only for one column and my remaining columns have the data as usual. So I am using this rendering only for that column. Is this wrong way of doing? or shall I pass entire row of data and use this rendering class to fill the row data. If I am wrong please explain. – Amarnath Sep 28 '12 at 06:34
  • Then only make those cells editable ... then you won't enter the editor code for the other cells. But if you have extra questions, start a new one instead of asking them in the comments – Robin Sep 28 '12 at 08:01
3
  • JPanel has implemented FlowLayout then to pretty accepting the PreferredSize came from its childs,

  • notice everything depends of your TableCellRenderer and TableCellEditor too

  • my view, my example

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thanks. I will try. It looks as it will solve my problem .. :) – Amarnath Sep 27 '12 at 13:44
  • don't forget to add EmtyBorders to the JPanel, in Editor and Renderer too – mKorbel Sep 27 '12 at 13:46
  • I have updated my post with code. I am getting a class cast exception. Please have a look and can u tell me where I am wrong. – Amarnath Sep 28 '12 at 05:37
  • I just want to ask one thing .. the example which you wrote is for the entire table's row. But what I have done is only for one column. Is this correct way what I am doing or shall I use the rendering for the entire row like what you have done. – Amarnath Sep 28 '12 at 06:37