0

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

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jack Twain
  • 6,273
  • 15
  • 67
  • 107
  • 1
    The first thing you should try is setting a distinct BG color to every element, the frame itself (e.g `Color.YELLOW`), `contentPane` (e.g `Color.BLUE`), the `panel` (I used `Color.GREEN`) and the `panel_1` ((e.g `Color.RED`). Then it it is easier to determine what is appearing, and where. But the bigger question is.. Why, *Why, **Why?*** do you think you need to use a `null` layout? Can you provide ASCII art (or an image with a simple drawing) of the GUI as it should appear in smallest size and (if resizable) with extra width/height? BTW, using those colors here, I see.. – Andrew Thompson Jan 16 '14 at 16:29
  • .. a thin blue border and a 1-2 px green area along the top right of a large red area. Is that what you were expecting? – Andrew Thompson Jan 16 '14 at 16:30

2 Answers2

4

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());
    }
}
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
3

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • @AndrewThompson, I was not recommending setting the preferred size. I was attempting to explain why the code doesn't work and give one more reason for using a layout manager. Added a little note. – camickr Jan 16 '14 at 18:13
  • Thanks for adding the note. Unfortunately the OP seems to have disappeared (last visit one hour ago) probably to reappear in a couple of days with a broken GUI, but I think that is part of the problem. Even mentioning 'preferred size' has them grasping that one straw and running with it till the next problem comes up. Oh well, the answer is good now for future visitors.. – Andrew Thompson Jan 16 '14 at 18:28
  • @AndrewThompson actually setting the preferred size worked really well! – Jack Twain Jan 17 '14 at 20:54
  • 1
    @alex, setting the preferred size is NOT the proper solution. The proper solution is to use a layout manager. – camickr Jan 17 '14 at 21:43