Solved

JTable/JScrollPane horizontal scrolling problem

Posted on 2003-11-24
8
1,185 Views
Last Modified: 2009-12-16
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
Comment
Question by:stsanz
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9810384
What happens on removing this?:

>>getContentPane().setLayout(null) ;
0
 
LVL 2

Expert Comment

by:Tols
ID: 9810576
This works fine:

getContentPane().setLayout(new BorderLayout());
getContentPane().add(scrollPane,BorderLayout.CENTER) ;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9810722
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
 
LVL 2

Expert Comment

by:Tols
ID: 9816404
I've checked it. RootLayout is good enough. CEHJ is right.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 9822808
> 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
 
LVL 6

Author Comment

by:stsanz
ID: 9823137
Thanks!
0
 
LVL 92

Expert Comment

by:objects
ID: 9823143
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9823882
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now