Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JTable/JScrollPane horizontal scrolling problem

Posted on 2003-11-24
8
Medium Priority
?
1,211 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

721 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