2

I would like to implement a mechanism in my program which will auto rescale JScrollPane when columns are to long to be fully seen in the JScrollpane's viewport:

For example my JTable look like:

   ___________ JScrollPanel _____________
  |                                      |
  |     __________ JTable _______________|_ ___    
  |    |________|______|______|_____|____|_|___|    
  |    |        |      |      |     |    | |   |
  |    |________|______|______|_____|____|_|___|
  |______________________________________|

Now how to set to auto rescal JscrollPanel toward wider JTable

   ___________ JScrollPanel ______________________
  |                                      <------>|        
  |     __________ JTable _________________ ___  | 
  |    |________|______|______|_____|____|_|___| |  
  |    |        |      |      |     |    | |   | |
  |    |________|______|______|_____|____|_|___| |
  |                                     <------->|
  |______________________________________________|

Sometimes i get data from JTableModel and i dont know how long data will be i JTable headers.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
user3719630
  • 83
  • 2
  • 9
  • 1
    Key information would be what layout managers you're using, and how you try to size your components and the GUI. Better to do no direct sizing if at all possible, but instead to let the components and the smart layout managers decide what the best size should be. – Hovercraft Full Of Eels Jul 12 '14 at 20:57
  • i use javax.swing.GroupLayout it is standard layout in Netbeans pallete. I trying to not set width in JTable and i setSize in JScrollPanel. But when my data from JTabelModel fill my JTable sometimes JTable was to long (with data) toward shorter JScrollPanel – user3719630 Jul 12 '14 at 21:12
  • 1
    I would use another layout for your GUI. For instance, have the JScrollPane added to a BorderLayout using JPanel in the CENTER position, and make sure to `pack()` the GUI before displaying it. – Hovercraft Full Of Eels Jul 12 '14 at 21:24

1 Answers1

1

JTable implements Scrollable, so you may be able to leverage the result returned by getPreferredScrollableViewportSize(). The default implementation may not be ideal, but you can override the method to get a better fit. This example returns a multiple of the table's getRowHeight(). For column widths, you may want to experiment with the AUTO_RESIZE_* settings available to setAutoResizeMode(), discussed here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045