Q:

Swing layout border margins on fullscreen

I have Java Swing application with JFrame using BorderLayout and inside it is a JPanel using CardLayout. I am displaying 3 different cards.
If I manually set the size of the JFrame, then the content is displayed like I want it. Label with image is in south east corner.

But when I set it to full screen, there is to much margin:

Here is the code with which I set it to full screen:
Frame[] frames = Frame.getFrames();
        JFrame frame =  (JFrame) frames[0];
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
        //frame.getContentPane().setPreferredSize( Toolkit.getDefaultToolkit().getScreenSize());
        frame.setUndecorated(true);
        //frame.setSize(600,500);
        frame.setVisible(true);
        frame.setLayout(new BorderLayout());

Cards are build with Netbeans GUI builder and for layout is set "Free Design".
Application will be whole time in full screen, where I would like that label with the image is SE corner, like it is on resized window(image example 1). Do I need to change layout for this or is it something else?

A:

Note that these UIs have a small border around the entire UI. To remove it, comment out the line:
ui.setBorder(new EmptyBorder(4,4,4,4));

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

public class ImageInSouthEast {

    private JComponent ui = null;

    ImageInSouthEast() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new GridBagLayout());
        ui.setBorder(new EmptyBorder(4,4,4,4));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.WEST;
        gbc.gridwidth = 2;
        gbc.weighty = .5;
        gbc.weightx = .5;
        gbc.gridx = 0;
        gbc.gridy = 0;

        // first add the labels
        for (int ii=1; ii<5; ii++) {
            gbc.gridy = ii;
            if (ii==4) {
                gbc.gridwidth = 1;
            }
            JLabel l = new JLabel("Label " + ii);
            l.setFont(l.getFont().deriveFont(50f));
            ui.add(l, gbc);
        }

        // now for the image!
        BufferedImage bi = new BufferedImage(100, 50, BufferedImage.TYPE_INT_RGB);
        JLabel l = new JLabel(new ImageIcon(bi));
        gbc.anchor = GridBagConstraints.LAST_LINE_END;
        gbc.gridx = 2;
        gbc.weighty = 0;
        ui.add(l, gbc);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                ImageInSouthEast o = new ImageInSouthEast();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

