Nested JScrollPanes: who gets a scroll bar ?
I have a JFrame that contains a JScrollPane (A) taht contains a JTabbedPane that contains a JScrollPane (B) that contains a JTable. When I resize the window, I get scrollbars. But I can't control where. I would like scroll bars to appear on the root JScrollPane (A) only as a "last resort" when there is no other way to represent the JTabbedane (for example if it has hundreds of tabs). The rest of the time, I would like to have scrollbars only for the JTable (B).
Here is a code example:
package test;
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.table.*;
publicclass NestedScrollpanesextends JFrame{
privatestaticfinallong serialVersionUID = 1;
private TableModel getTableModel(){
returnnew DefaultTableModel(new Object[]{
"A","B","C","D","E",
"F","G","H","I","J",
"K","L","M","N","O"}, 100);
}
public NestedScrollpanes(){
super("Test");
JPanel rootPanel =new JPanel(new BorderLayout());
JTabbedPane tabbedPane =new JTabbedPane();
JScrollPane rootScrollPane =new JScrollPane(tabbedPane);// A
JTable table =new JTable(getTableModel());
JScrollPane tableScrollPane =new JScrollPane(table);// B
getContentPane().add(rootPanel, BorderLayout.CENTER);
rootPanel.add(rootScrollPane);
tabbedPane.addTab("TAB", tableScrollPane);
setSize(100, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
publicstaticvoid main(String[] args){
new NestedScrollpanes();
}
}
Any idea ?
[3318 byte] By [
Roukinea] at [2007-11-26 16:18:51]

# 3
How's this:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.*;
import javax.swing.table.*;
public class NestedScrollpanes extends JFrame {
private static final long serialVersionUID = 1;
private TableModel getTableModel() {
return new DefaultTableModel(new Object[] {
"A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"K", "L", "M", "N", "O"}, 100);
}
public NestedScrollpanes() {
super("Test");
JPanel rootPanel = new JPanel(new BorderLayout());
JTabbedPane tabbedPane = new OptionalScrollTabbedPane();
JScrollPane rootScrollPane = new JScrollPane(tabbedPane); // A
JTable table = new JTable(getTableModel());
JScrollPane tableScrollPane = new JScrollPane(table); // B
JPanel panel = new JPanel();
panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS ) );
for ( int k = 0 ; k < 50 ; k++ )
panel.add( new JTextField() );
getContentPane().add(rootPanel, BorderLayout.CENTER);
rootPanel.add(rootScrollPane);
tabbedPane.addTab("ScrollPane", tableScrollPane);
tabbedPane.addTab( "TextFields", panel );
setSize(200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new NestedScrollpanes();
}
public static class OptionalScrollTabbedPane extends JTabbedPane implements Scrollable {
private int unitIncrement = new JTextField().getPreferredSize().height;
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}
public int getScrollableBlockIncrement( Rectangle visibleRect, int orientation, int direction ) {
int block;
JViewport view = (JViewport) SwingUtilities.getAncestorOfClass( JViewport.class, this );
if ( view != null )
block = orientation == SwingConstants.VERTICAL ? view.getHeight() : view.getWidth();
else // arbitrarily set block size to one-tenth the dimension
block = ( orientation == SwingConstants.VERTICAL ? getHeight() : getWidth() ) / 10;
return block;
}
public boolean getScrollableTracksViewportHeight() {
boolean rv = false;
int index = getSelectedIndex();
if ( index != -1 ) {
rv = ( getComponentAt( index ) instanceof JScrollPane );
}
return rv;
}
public boolean getScrollableTracksViewportWidth() {
boolean rv = false;
int index = getSelectedIndex();
if ( index != -1 ) {
rv = ( getComponentAt( index ) instanceof JScrollPane );
}
return rv;
}
public int getScrollableUnitIncrement( Rectangle visibleRect, int orientation, int direction ) {
return unitIncrement;
}
}
}