1

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. enter image description here

But when I set it to full screen, there is to much margin: enter image description here

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?

Jure
  • 799
  • 6
  • 25
  • 1
    So do you want to delete the margins between the text or increase the font size? – nick zoum Jul 19 '16 at 09:17
  • I wnat to have label with the image to be in the lower down corner like is on the first image. – Jure Jul 19 '16 at 09:19
  • 2
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). Another way is to create a new `BufferedImage` at run-time. – Andrew Thompson Jul 19 '16 at 10:24
  • One way to achieve (what I think you're *trying* to achieve) is to combine layouts. Either with a `GridBagLayout` with appropriate wights & anchors, or (simpler) by combining layouts. Note that the 'combine layouts' approach would lead to the image *always* being below the last line of text - whereas you seem to want it to be parallel to the last line when the GUI is at the minimum size. – Andrew Thompson Jul 19 '16 at 10:30
  • @AndrewThompson Not necessarily, he could use a `layeredPane` with 2 `JPanels`. The first, that is in the back will have the Text and the second, which is in front will be opaque and have the image at the SE corner. – nick zoum Jul 19 '16 at 10:48
  • @nickzoum Yes.. but wouldn't that open the possibility of the image appearing over/under the existing text? – Andrew Thompson Jul 19 '16 at 10:49
  • @AndrewThompson That's they only way to make the text and image appear side by side when the JFrame gets resized. – nick zoum Jul 19 '16 at 10:53
  • @AndrewThompson as you can see in the first image, the image would hide part of the last text if it was any longer. – nick zoum Jul 19 '16 at 10:54
  • Like @AndrewThompson allready said I wasn't clear enough about what I want to achive. Application will be in the full screen all the time. When is in full screen position of all elements is ok, except for the label with the image, which should be in SE corner. – Jure Jul 19 '16 at 10:59

2 Answers2

2

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

enter image description here enter image description here

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

If you just want to remove the gap between the text then you could just use BoxLayout.

Set the layout by doing this:

Container pane = frame.getContentPane();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
pane.add(Box.createHorizontalGlue());

Adding an element

public void add(Component comp, int gap){
    //comp is the component that will be added
    //gap is the extra space after the last component and this
    pane.remove(pane.getComponents().length - 1);
    pane.add(Box.createVerticalStrut(gap));
    pane.add(comp);
    //Obviously pane or frame need to be visible to use this method
}

Add Text by doing this:

add(new JLabel(text), 5);

Add the image by doing this:

JPanel panel = new JPanel();
panel.add(image, BorderLayout.EAST);
panel.setOpaque(false);
add(Box.createHorizontalGlue(),0);
add(panel,0);
nick zoum
  • 7,216
  • 7
  • 36
  • 80
  • i don't care so much about the spacing between the text. I would like to have label with the image(logo) in the bottom right corner like is in the first image. – Jure Jul 19 '16 at 10:02
  • @Jure just follow my instructions, it works for both – nick zoum Jul 19 '16 at 10:43