Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1221
  • Last Modified:

JTable/JScrollPane horizontal scrolling problem

Hi,

In a JApplet, I have a JTable embedded into a JScrollPane and I want to be able to scroll the table horizontally and vertically. The vertical scrolling works perfectly well, but I can't get the horizontal scrolling to work. The horizontal scrollbar appears but has no index, altough the columns spread past the rightmost limit of the scrollpane. Any ideas?

Here is my JTable/JScrollPane creation code (the table model is very simple and contains 12 text columns) :

getContentPane().setLayout(null) ;

m_table = new JTable(m_model) ;

for (int iCol = 0 ; iCol < m_model.getColumnCount() ; iCol++)
m_table.getColumnModel().getColumn(iCol).setMinWidth(200) ;

JScrollPane scrollPane = new JScrollPane(
m_table,
      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
      JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
m_table.setPreferredScrollableViewportSize(new Dimension(1600, 700)) ;
scrollPane.setBounds(0,0,1150,498) ;

getContentPane().add(scrollPane) ;
0
stsanz
Asked:
stsanz
  • 3
  • 2
  • 2
  • +1
1 Solution
 
CEHJCommented:
What happens on removing this?:

>>getContentPane().setLayout(null) ;
0
 
TolsCommented:
This works fine:

getContentPane().setLayout(new BorderLayout());
getContentPane().add(scrollPane,BorderLayout.CENTER) ;
0
 
CEHJCommented:
Of course, if you're adding to a JFrame, the above code is redundant, but as I mentioned earlier, the problem is likely caused by your removing the layout manager.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
TolsCommented:
I've checked it. RootLayout is good enough. CEHJ is right.
0
 
objectsCommented:
> m_table.setPreferredScrollableViewportSize(new Dimension(1600, 700)) ;

As you are using a fixed size table this line is redundant.

To get your horizontal scroll pane you just need to turn off auto column resizing:

JTable m_table = new JTable(m_model) ;
m_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
for (int iCol = 0 ; iCol < m_model.getColumnCount() ; iCol++)
   m_table.getColumnModel().getColumn(iCol).setMinWidth(200) ;
0
 
stsanzAuthor Commented:
Thanks!
0
 
objectsCommented:
0
 
CEHJCommented:
Unless you have a very good reason, calculating your own layouts is not a good idea. Try running your code on another OS and you'll see what I mean.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now