Please help me tame the GridBagLayout.
I use a GridBagLayout and SpringUtilities to create my MainFrame JFrame window. (See the code snippets below.) The program asks a series of questions. The first question is displayed at start up. The user hits the "Next" button to bring up subsequent questions.
PROBLEM: The window resizes itself before displaying the second question, and remains set to the new size. What�s going on?
Thank you in advance,
Ed
import javax.swing.*;
import utl.SpringUtilities;
public class MainFrame extends javax.swing.Jframe implements MenuConstants
{
// FIELDS
JFrame jFrame;
//Creates new form JMainFrame
public MainFrame()
{
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
*/
private void initComponents()
{
//--- Setup the frame
JFrame.setDefaultLookAndFeelDecorated(true);
jFrame = new JFrame(sTitle+": Key Ideas � Elementary Concepts");
//--- Set up the content pane.
Container newContentPane = jFrame.getContentPane();
//--- Set the layout
newContentPane.setLayout(new GridBagLayout());
newContentPane.setBackground(Color.yellow);
//--- Set the stretchyness of rows and columns in a GridBagLayout
gbc.weightx = 0.5;// Specifies how to distribute extra horizontal space.
gbc.weighty = 0.5; // Specifies how to distribute extra horizontal space.
gbc.fill = GridBagConstraints.NONE; // Don't resize
//--- Top row
setupTopRow(newContentPane);
//--- Middle row
setupMiddleRow(newContentPane);
//--- Bottom row
setupBottomRow(newContentPane);
//--- Add content pane to frame
jFrame.setContentPane(newContentPane);
//--- Create the menus
setupMenus();
//--- Create the menu bar
setupMenuBar();
//--- Install the menu bar in the frame
jFrame.setJMenuBar(menuBar);
jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); jFrame.setSize(798, 570); //(798, 780);
//jFrame.pack(); // Set to minimum/preferred size.
//frame.setVisible(true); // take out in 5.0
//--- Display the window
jFrame.setVisible(true);
}// initComponents
void addGB(Container c, Component component, int x, inty)
{
// Check and set the layout manager to GridBagLayout if necessary
if ((c.getLayout() instanceof GridBagLayout) ==false){
c.setLayout(new GridBagLayout() );
}
// Set objects position
gbc.gridx = x;
gbc.gridy = y;
// Add the object to the newContentPane
c.add(component, gbc);
}//addGB
private void setupTopRow(Container c)
{
int visibleWidth = 638,
visibleHeight = 150;
Dimension minimumSize = new Dimension(visibleWidth, visibleHeight);
canvasFigure = new DrawingPanelTop();
canvasFigure.setOpaque(true);
jlHeader.setBackground(mySet.background);
jlHeader.setOpaque(true);
jlHeader.setHorizontalAlignment(JLabel.CENTER);
jlHeader.setFont(new Font("Dialog", Font.BOLD, 14));
canvasFigure.setMinimumSize(minimumSize);
canvasFigure.setPreferredSize(minimumSize);
canvasFigure.setMaximumSize(minimumSize);
canvasFigure.setBackground(mySet.background);
gbc.gridwidth = 6;
gbc.weighty = 1.0;
// Add the widgets
JPanel jpTop = new JPanel(); // New container
SpringLayout slTop = new SpringLayout();
jpTop.setLayout(slTop);
// Top row
jpTop.add( jlHeader );
jpTop.add( canvasFigure );
//Lay out the buttons in one row and as many columns
//as necessary, with 6 pixels of padding all around.
SpringUtilities.makeCompactGrid(
jpTop, // Container
2, 1, // rows, cols
0, 0, // initialX, initialY,
0, 0); // xPad, yPad
// Add the spring layout to the grid bag
addGB(c, jpTop, 0, 0);
}//setupTopRow
private void setupMiddleRow(Container c)
{
int visibleWidth = 338,
visibleHeight = 280,
canvasHeight = 1800;
// Create JSplitPane with 2 JScrollPanes each with a JPanel to hold widgets
//--- Question
canvasQuestion = new DrawingPanelLeft();
canvasQuestion.setBackground(mySet.background); // JPanel
canvasQuestion.setOpaque(true);
canvasQuestion.setMinimumSize(new Dimension(visibleWidth, canvasHeight));
canvasQuestion.setPreferredSize(new Dimension(visibleWidth, canvasHeight));
canvasQuestion.setMaximumSize(new Dimension(visibleWidth, canvasHeight));
// The visible JScrollPane in the canvas
leftScrollPane = new JScrollPane( canvasQuestion ); // Add JPanel to JScrollPane
leftScrollPane.setBackground(mySet.background);
leftScrollPane.setOpaque(true);
leftScrollPane.setMinimumSize(new Dimension(visibleWidth, visibleHeight));
leftScrollPane.setPreferredSize(new Dimension(visibleWidth, visibleHeight));
leftScrollPane.setMaximumSize(new Dimension(visibleWidth, visibleHeight));
//--- Solution
canvasSolution = new DrawingPanelRight(); // JPanel
canvasSolution.setBackground(mySet.background);
canvasSolution.setOpaque(true);
canvasSolution.setMinimumSize(new Dimension(visibleWidth, canvasHeight));
canvasSolution.setPreferredSize(new Dimension(visibleWidth, canvasHeight));
canvasSolution.setMaximumSize(new Dimension(visibleWidth, canvasHeight));
// The visible JScrollPane in the canvas
rightScrollPane = new JScrollPane( canvasSolution ); // Add JPanel to JScrollPane
rightScrollPane.setBackground(mySet.background);
rightScrollPane.setOpaque(true);
rightScrollPane.setMinimumSize(new Dimension(visibleWidth, visibleHeight));
rightScrollPane.setPreferredSize(new Dimension(visibleWidth, visibleHeight));
rightScrollPane.setMaximumSize(new Dimension(visibleWidth, visibleHeight));
// Add the widgets
JPanel jpMiddle = new JPanel(); // New container
SpringLayout slMiddle = new SpringLayout();
jpMiddle.setLayout(slMiddle);
jpMiddle.setOpaque(true);
// Middle row
jpMiddle.add( leftScrollPane );
jpMiddle.add( rightScrollPane );
// Lay out the buttons in one row and as many columns
// as necessary, with X pixels of padding all around.
SpringUtilities.makeCompactGrid(
jpMiddle, // Container
1, 2, // rows, cols
0, 0, // initialX, initialY,
0, 0); // xPad, yPad
// Add the spring layout to the grid bag
addGB(c, jpMiddle, 0, 1);
}//setupMiddleRow
private void setupBottomRow(Container c)
{
jButtonNext.setBounds(0, 0, 90, 26);
jButtonNext.setBackground(new java.awt.Color(255, 128, 0));
jButtonNext.setFont(new Font("Dialog", Font.BOLD, 16));
jButtonNext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
});
// Add the widgets
JPanel jpBottom = new JPanel(); // New container
SpringLayout slBottom = new SpringLayout();
jpBottom.setLayout(slBottom);
// Bottom row
jpBottom.add(jButtonNext);
//Lay out the buttons in one row and as many columns
//as necessary, with 6 pixels of padding all around.
SpringUtilities.makeCompactGrid(
jpBottom, // Container
1, 1, // rows, cols
0, 0, // initialX, initialY,
0, 0); // xPad, yPad
// Add the spring layout to the grid bag
addGB(c, jpBottom, 0, 2);
}
// Button selection <<<>>><<<>>> <<<>>> <<<>>><<<>>>
private void buttonActionPerformed(java.awt.event.ActionEvent evt)
{
// Get the current state of the buttons
if(evt.getActionCommand().equals("Next Q"))
jbNextActionPerformed();
}
void jbNextActionPerformed()
{
askQuestion();
}
// Main <<<>>><<<>>> <<<>>> <<<>>><<<>>>
public static void main(
String args[])
{
new MainFrame();
}