Question

I have a JFrame with a BorderLayout. I added a JPanel to the NORTH side of the JFrame. In this panel I want to add components to it in an absolute positioning. In the Center side of the JFrame I added another JPanel which should take a huge space. However when I run the application I see nothing from the North JPanel as the Center JPanel occupied all the space of the JFrame! How can I give vertical space to the North JPanel?

I really need to used absolute positioning for the north JPanel.

Here's my code:

public class AAAA extends JFrame {

    private JPanel contentPane;

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

    /**
     * Create the frame.
     */
    public AAAA() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1136, 520);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.NORTH);
        panel.setLayout(null);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.setBounds(0, 0, 117, 29);
        panel.add(btnNewButton);

        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.CENTER);
    }

}
Was it helpful?

Solution 2

I really need to used absolute positioning for the north JPanel.

Why? If we know why you think you need to do this we can probably offer a better approach.

Don't use a null layout. Swing was designed to be used with layout managers.

The BorderLayout will respect the preferred height of the component added to the NORTH. The preferred height is zero so nothing is displayed.

Note: I am not suggesting that you set the preferred height of the panel, that is the job of the layout manager and that is why you should always use a layout manager. Layout managers do more than just set the size/location of a component.

OTHER TIPS

Update 1

I see you have already selected an answer (prematurely, I think). Here is the first iteration of what I believe you are trying to achieve. Without need for setting bounds or preferred sizes..

Laid Out 1

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class AAAA extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    AAAA frame = new AAAA();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public AAAA() {
        super("Laid Out");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // OMG! If you can make a GUI break at 1336 px wide, it should be 
        // possible to make it break at ..much smaller!
        //setBounds(100, 100, 1136, 520);
        setBackground(Color.YELLOW);
        contentPane = new JPanel();
        contentPane.setBackground(Color.BLUE);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        // make it a FlowLayout as FlowLayout.LEADING with no spacing to 
        // make the button snug up against the top left
        JPanel panel = new JPanel(
                new FlowLayout(FlowLayout.LEADING, 0, 0));
        panel.setBackground(Color.GREEN);
        contentPane.add(panel, BorderLayout.NORTH);
        //panel.setPreferredSize(new Dimension(1024,400));

        JButton btnNewButton = new JButton("New button");
        // we change the margin to make the button bigger than natural size.
        btnNewButton.setMargin(new Insets(6, 22, 6, 22));
        panel.add(btnNewButton);

        JPanel panel_1 = new JPanel();
        // create a solic color image to both pad the GUI and 
        // provide visual indication of where it is.
        BufferedImage bi = new BufferedImage(
                400,200,BufferedImage.TYPE_INT_RGB);
        JLabel padder = new JLabel(new ImageIcon(bi));
        panel_1.add(padder);
        panel_1.setBackground(Color.RED);
        contentPane.add(panel_1, BorderLayout.CENTER);
        pack();
        setMinimumSize(getSize());
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top