Solved

JTable/JScrollPane horizontal scrolling problem

Posted on 2003-11-24
8
1,182 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

757 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

26 Experts available now in Live!

Get 1:1 Help Now