Resizing issues with SCROLLBAR_AS_NEEDED and frame size

I am having trouble in a big project that I'm working on, so I wanted to observe scrollbar behavior in regard to frame resizing in an application that does nothing but display swing components. I want to be able to make a frame as large as some defined maximum size, and have the frame resize to the maximum size if the frame is stretched to some greater value. I'm not sure how to take into consideration the frame title size or other things that can affect the size of the window so that the content pane is a desired size, and do that in a platform independent way. Here's some code:

package scrollpaneresizebug;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.event.ComponentEvent;

import java.awt.event.ComponentListener;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

publicclass ScrollPaneResizeBugextends JFrameimplements WindowListener,

ComponentListener{

// JPanel outerPanel;

JPanel scrollPanel;

JPanel northPanel;

JPanel eastPanel;

JPanel southPanel;

JPanel westPanel;

JButton button;

JScrollPane scrollPane;

int maxWidth = 911;

int maxHeight = 737;

public ScrollPaneResizeBug(){

super("Scroll Pane Resize Bug");

setLayout(new BorderLayout());

button =new JButton("Button");

northPanel =new JPanel();

northPanel.setMinimumSize(new Dimension(900, 50));

northPanel.setMaximumSize(new Dimension(900, 50));

northPanel.setPreferredSize(new Dimension(900, 50));

northPanel.setBackground(Color.BLACK);

southPanel =new JPanel();

southPanel.setMinimumSize(new Dimension(900, 50));

southPanel.setMaximumSize(new Dimension(900, 50));

southPanel.setPreferredSize(new Dimension(900, 50));

southPanel.setBackground(Color.BLACK);

eastPanel =new JPanel();

eastPanel.setMinimumSize(new Dimension(50, 600));

eastPanel.setMaximumSize(new Dimension(50, 600));

eastPanel.setPreferredSize(new Dimension(50, 600));

eastPanel.setBackground(Color.BLACK);

westPanel =new JPanel();

westPanel.setMinimumSize(new Dimension(50, 600));

westPanel.setMaximumSize(new Dimension(50, 600));

westPanel.setPreferredSize(new Dimension(50, 600));

westPanel.setBackground(Color.BLACK);

scrollPanel =new JPanel();

scrollPanel.setMinimumSize(new Dimension(800, 600));

scrollPanel.setMaximumSize(new Dimension(800, 600));

scrollPanel.setPreferredSize(new Dimension(800, 600));

scrollPanel.setBackground(Color.BLUE);

scrollPane =new JScrollPane(

JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,

JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

scrollPane.setMinimumSize(new Dimension(800, 600));

scrollPane.setMaximumSize(new Dimension(800, 600));

scrollPane.setPreferredSize(new Dimension(800, 600));

scrollPanel.add(button);

add(scrollPane, BorderLayout.CENTER);

add(northPanel, BorderLayout.NORTH);

add(southPanel, BorderLayout.SOUTH);

add(eastPanel, BorderLayout.EAST);

add(westPanel, BorderLayout.WEST);

//getContentPane().add(outerPanel);

scrollPane.setViewportView(scrollPanel);

addWindowListener(this);

addComponentListener(this);

this.setSize(new Dimension(800, 600));

this.setVisible(true);

}

publicvoid componentResized(ComponentEvent e){

int newWidth = getWidth();

int newHeight = getHeight();

if (newWidth > maxWidth){

newWidth = maxWidth;

}

if (newHeight > maxHeight){

newHeight = maxHeight;

}

super.setSize(new Dimension(newWidth, newHeight));

}

publicvoid componentMoved(ComponentEvent arg0){

// TODO Auto-generated method stub

}

publicvoid componentShown(ComponentEvent arg0){

// TODO Auto-generated method stub

}

publicvoid componentHidden(ComponentEvent arg0){

// TODO Auto-generated method stub

}

publicvoid windowOpened(WindowEvent arg0){

// TODO Auto-generated method stub

}

publicvoid windowClosing(WindowEvent arg0){

System.exit(0);

}

publicvoid windowClosed(WindowEvent arg0){

// TODO Auto-generated method stub

}

publicvoid windowIconified(WindowEvent arg0){

// TODO Auto-generated method stub

}

publicvoid windowDeiconified(WindowEvent arg0){

// TODO Auto-generated method stub

}

publicvoid windowActivated(WindowEvent arg0){

// TODO Auto-generated method stub

}

publicvoid windowDeactivated(WindowEvent arg0){

// TODO Auto-generated method stub

}

publicstaticvoid main(String[] args){

ScrollPaneResizeBug sprb =new ScrollPaneResizeBug();

sprb.setVisible(true);

}

}

[9029 byte] By [Steve973a] at [2007-10-2 7:58:33]
# 1

> I'm not sure how to take into consideration the frame title size or other things that can affect the size of the window so that the content pane is a desired size

frame.getSize() gives the size of the frame

frame.getInsets() gives the size of the title bar and borders

the difference is the content pane

camickra at 2007-7-16 21:49:30 > top of Java-index,Desktop,Core GUI APIs...
# 2

Why would the JScrollPane be as big as the entire frame when you added 800x600 panels on all sides of it? It's going to be in the center surrounded by huge panels. I don't know why you expect anything different. If you want the JScrollPane to fill the entire panel then add it to the center and don't add anything to the north, west, south or east. By your description what you probably want is the JScrollPane to be the contentPane itself.

JFrame frame = new JFrame();

JScrollPane scrollPane = new JScrollPane();

frame.setContentPane(scrollPane);

frame.setSize(600, 600);

frame.setVisible(true);

kablaira at 2007-7-16 21:49:30 > top of Java-index,Desktop,Core GUI APIs...
# 3

> Why would the JScrollPane be as big as the entire

> frame when you added 800x600 panels on all sides of

> it? It's going to be in the center surrounded by

> huge panels.

Thanks, kablair. However, if you look at the code, these panels are not 800x600 on all sides. The north and south panels only add a combined height of 100, and the east and west panels only add a combined width of 100. so the whole window, minus insets, should be 900x700.

Anyway, i took the advice of the guy above, and that has gotten me much closer. In the bigger app that I'm trying to calculate the size, now I'm only 2 pixels away from the true size, although I can't figure out what I'm forgetting.

Steve973a at 2007-7-16 21:49:30 > top of Java-index,Desktop,Core GUI APIs...