Search code examples
javaswinglayoutgridbaglayoutimageicon

Layout Manager for background images and text


I'm trying to think of the best layout manager to achieve the picture below. I know absolute positioning is what I am used to, but I can't get the background image using this. GridBagLayout is excellent, but horrifically hard when ever I try I get a separate image for each grid.

Does anyone know an easy way out of this, or easy code to achieve the following?

enter image description here


Solution

  • There are a number of ways you can achieve this.

    The simplest might be to just use what's already availableenter image description here...

    If you don't need the background to be scaled at run-time (ie you can get away with a non-resizable window), simply using a JLabel as the primary container could make your life significantly easier.

    public class LabelBackground {
    
        public static void main(String[] args) {
            new LabelBackground();
        }
    
        public LabelBackground() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new LoginPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class LoginPane extends JLabel {
    
            public LoginPane() {
                try {
                    setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/background.jpg"))));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.anchor = GridBagConstraints.EAST;
                gbc.insets = new Insets(2, 2, 2, 2);
                gbc.gridx = 0;
                gbc.gridy = 0;
    
                JLabel nameLabel = new JLabel("Name: ");
                nameLabel.setForeground(Color.WHITE);
                JLabel passwordLabel = new JLabel("Password: ");
                passwordLabel.setForeground(Color.WHITE);
    
                add(nameLabel, gbc);
                gbc.gridy++;
                add(passwordLabel, gbc);
    
                gbc.anchor = GridBagConstraints.WEST;
                gbc.gridx++;
                gbc.gridy = 0;
                add(new JTextField(20), gbc);
                gbc.gridy++;
                add(new JTextField(20), gbc);
    
                gbc.gridy++;
                gbc.insets = new Insets(10, 2, 2, 2);
                gbc.anchor = GridBagConstraints.EAST;
                add(new JButton("Submit"), gbc);
    
            }
    
        }
    
    }
    

    Updated with example of left alignment

    At the end of the constructor, add...

    JPanel filler = new JPanel();
    filler.setOpaque(false);
    gbc.gridx++;
    gbc.weightx = 1;
    add(filler, gbc);
    

    enter image description here

    You might like to take a look at How to use GridBagLayout for more details