0

I've set the size of my JTable column to 200 (using setPreferredWidth()), and the size of my JTextField inside it to 100 (using setSize()).

Still, I see my text field stretched out to the entire 200 pixels of the column, ignoring the size I set.

What should I do to set different sizes for my column and text field?

Danny
  • 1
  • 1
  • This is the way editors (and renderers) work. They always ocuupy the entire area of the cell. Why would you want this to be any different? What do you want to do with the other 100 pixels? Sounds like a strange design. Post your actual requirement and not your solution and maybe someone can suggest a better solution. – camickr May 29 '11 at 15:26
  • And please, never use `setSize()`, `setMin/Pref/MaxSize()` on any Swing component, hard-coding sizes in pixels is very bad! Try to trust Swing on calculating the best possible sizes for your components (maybe with some help, but not provided as hard-coded pixels). – jfpoilpret May 29 '11 at 15:34

1 Answers1

2

Use a custom TableCellRenderer and/or TableCellEditor to make the cell have a specific size for the JTextField and also have additional space in the table.

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

Also see: http://download.oracle.com/javase/tutorial/uiswing/layout/problems.html as suggested

(As a side note this sounds strange, maybe take a second and ask your self is a JTable really the best component to use in this situation).

jzd
  • 23,473
  • 9
  • 54
  • 76