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);
}
}

