Solved

JTable/JScrollPane horizontal scrolling problem

Posted on 2003-11-24
8
1,196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:Tols
ID: 9816404
I've checked it. RootLayout is good enough. CEHJ is right.
0
 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Certificat to iSeries KeyStore 1 31
spring jars download 1 68
ejb example issues 3 90
jmss example java 2 48
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

751 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