Kevin Schultz
Kevin Schultz

Reputation: 884

JPanels appear to go off the frame?

I am working on an assignment "of sorts" not a school assignment. Having said that, any ideas would be great.

I am using WindowBuilder in Eclipse and have created a basic form. I have used nested JPanel components on a border layout to create it. For some reason, the panels appear as though they are spilling over the edges of the JFrame. I have the frame dimensions set to (500, 400) and the panels are various sizes, but none greater than 400 wide.

Code:

    package SwingAssignment;

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;

import java.awt.GridBagLayout;

import javax.swing.BoxLayout;

import net.miginfocom.swing.MigLayout;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JTextArea;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import com.jgoodies.forms.factories.FormFactory;

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.border.BevelBorder;
import javax.swing.SwingConstants;

public class Swing_Assignemnt {

    private JFrame frmWindowBuilderAssignment;
    private JPanel Center_Panel;
    private JTextArea textArea;
    private JPanel panel_1;
    private JPanel panel;
    private JTextField textField;
    private JPanel panel_2;
    private JTextArea txtrTextarea_0;
    private JTextArea txtrTextarea_1;
    private JPanel panel_3;
    private JTextArea txtrTextareasouth;
    private JLabel lblNewLabel;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Swing_Assignemnt window = new Swing_Assignemnt();
                    window.frmWindowBuilderAssignment.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Swing_Assignemnt() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmWindowBuilderAssignment = new JFrame();
        frmWindowBuilderAssignment.setTitle("Window Builder Assignment");
        //frmWindowBuilderAssignment.setBounds(500, 500, 650, 600);
        frmWindowBuilderAssignment.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmWindowBuilderAssignment.getContentPane().setLayout(new BorderLayout(0, 0));
        frmWindowBuilderAssignment.setVisible(true);
        frmWindowBuilderAssignment.setSize(394, 500);
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        //frmWindowBuilderAssignment.pack();
        //frmWindowBuilderAssignment.pack();
        frmWindowBuilderAssignment.setVisible( true );

        panel = new JPanel();
        panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
        frmWindowBuilderAssignment.getContentPane().add(panel, BorderLayout.NORTH);
        panel.setPreferredSize(new Dimension(200, 40));
        panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        //panel.setBounds(20, 10, 200, 400);
        panel.setVisible(true);

        JComboBox comboBox = new JComboBox();
        panel.add(comboBox);
        comboBox.setPreferredSize(new Dimension(125, 20));
        comboBox.setVisible(true);

        textField = new JTextField();
        panel.add(textField);
        textField.setColumns(10);
        textField.setVisible(true);

        panel_2 = new JPanel();
        panel_2.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
        frmWindowBuilderAssignment.getContentPane().add(panel_2, BorderLayout.CENTER);
        panel_2.setLayout(new GridLayout(1, 2, 2, 2));
        panel_2.setPreferredSize(new Dimension(200, 400));
        panel_2.setVisible(true);


        txtrTextarea_0 = new JTextArea();
        txtrTextarea_0.setText("textArea_0");
        panel_2.add(txtrTextarea_0);
        txtrTextarea_0.setPreferredSize(new Dimension(50, 30));
        txtrTextarea_0.setVisible(true);

        txtrTextarea_1 = new JTextArea();
        txtrTextarea_1.setText("textArea_1");
        panel_2.add(txtrTextarea_1);
        txtrTextarea_1.setVisible(true);
        txtrTextarea_1.setPreferredSize(new Dimension(50, 30));

        panel_3 = new JPanel();
        panel_3.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
        frmWindowBuilderAssignment.getContentPane().add(panel_3, BorderLayout.SOUTH);
        panel_3.setLayout(new GridLayout(2, 1, 2, 2));
        panel_3.setVisible(true);

        txtrTextareasouth = new JTextArea();
        txtrTextareasouth.setText("textArea_South");
        panel_3.add(txtrTextareasouth);
        txtrTextareasouth.setVisible(true);
        txtrTextareasouth.setPreferredSize(new Dimension(200, 150));

        lblNewLabel = new JLabel("Status Label");
        panel_3.add(lblNewLabel);
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setPreferredSize(new Dimension(200, 20));




    }
}

What it looks like:

Window Screen Shot

After some code changes, this is what I now have, I am not sure how to resize the label at the bottom. It only needs to be about 15 tall.

enter image description here

Upvotes: 0

Views: 1554

Answers (2)

camickr
camickr

Reputation: 324118

Don't use setBounds() or setPreferredSize(). Each Swing component should determine its own preferred size and the layout manager will then position the components based on the rules of the layout manager.

Don't use setVisible(true) on all your Swing components (except for the JFrame). By default Swing components are visible.

You should add all the components to the frame before using:

frame.pack()
frame.setVisible( true );

Upvotes: 3

Chase
Chase

Reputation: 88

try using

frmWindowBuilderAssignment.pack();

at the End of your initialising Method.

This set all of your Components to their preferred Sizes and adjust the Frame.

Upvotes: 0

Related Questions