0

In a special stage of my profgram , I want to change the rows hieght , this works but there is a gray empty space still in the table as shown in the image enter image description here

i tried the following

jTable1.setRowHeight(60);

jTable2.setPreferredSize(new java.awt.Dimension(120,225));
jTable2.setMaximumSize(new java.awt.Dimension(120,225));
jTable2.setMinimumSize(new java.awt.Dimension(120,225));

but the size of the table does not changed. Any solutions?

athspk
  • 6,722
  • 7
  • 37
  • 51
Calm Sea
  • 181
  • 2
  • 4
  • 15
  • Please edit your question to include an [sscce](http://sscce.org/) that shows your current approach. – trashgod Sep 29 '12 at 23:20
  • don't use setXXSize **ever**, for reasons see http://stackoverflow.com/a/7229519/203657 – kleopatra Sep 30 '12 at 12:24
  • layout problems are always solved ... by the layoutManager (as already noted in the comments to @MadProgrammer answer). So first, exactly define what you want to reach in which context. – kleopatra Sep 30 '12 at 12:31
  • @kleopatra thanks any way , what i want exactly is the following : when i change the width of the rows while the program is running , the rows width changes but the size of the table does not change , it remains fixed. I hope i could explain the problem better now – Calm Sea Sep 30 '12 at 17:52

1 Answers1

4

Take a look at JTable.setFillsViewportHeight

Table fillsViewportHeight = false

Without

Table fillsViewportHeight = true

With

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • First, why? Second, you might like to have a read through this http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi – MadProgrammer Sep 29 '12 at 20:22
  • because the view or the shape will not be beautiful and the row will look not equals – Calm Sea Sep 29 '12 at 20:38
  • In that case, set the viewport background color to the same color as the table's background. – MadProgrammer Sep 29 '12 at 20:40
  • The size of the table is dependent on a number of factors. The first been the size of the tables content, the second been the layout manager of it's parent container – MadProgrammer Sep 29 '12 at 21:07
  • Use `frame.pack()` and the size will be good. Note that this does not resize the table, but rather the parent container – Robin Sep 29 '12 at 21:27
  • @MadProgrammer thanks but last question : what is layout manager of table parent container?? – Calm Sea Sep 29 '12 at 21:30
  • +1 for `setFillsViewportHeight()`. Maybe `setPreferredScrollableViewportSize()`, followed by `pack()`? – trashgod Sep 29 '12 at 21:31
  • 1
    @CalmSea The size of the table can be altered to meet the requirements of the layout manager of table's parent container (ie, when you add a table to a scroll pane, you are adding to a `JViewPort` container), this can effect the size of the table. You need to be careful with `JTable` as changing the preferred size could result in the table truncating it's contents. Better to let the `JTable` calculate it's requirements and manipulate the size through a layout manager. Try changing the view ports layout manager to something like `BorderLayout` for example – MadProgrammer Sep 29 '12 at 21:37
  • 1
    @trashgod setting the scrollable pref is nearly as bad as using settXXSize. Nearly because the default implementation doesn't do much useful in core (JXTable uses the visibleRowCount/ColumnCount properties to calculate the pref in px) – kleopatra Sep 30 '12 at 12:28
  • @kleopatra: Agree; bad habit from aqua's awkwardly large default vertical dimension. Mindful of these [caveats](http://stackoverflow.com/q/7229226/230513), would overriding `getPreferredScrollableViewportSize()` be reasonable? – trashgod Sep 30 '12 at 15:11
  • @trashgod that's what JXTable does, so guess my answer :-) To (maybe) be clearer: don't do it on the application level but on a mini-framework level. For finer-grained control, use an appropriate LayoutManager – kleopatra Sep 30 '12 at 15:15