2

I am trying to build a set of two labels and a button (label-button-label) that looks something like the top row of this:

enter image description here.

I am struggling to do this with Java Swing. I've tried BorderLayout, with BorderLayout.WEST, BorderLayout.CENTER, BorderLayout.EAST but that makes the button fill the space: enter image description here

here's the code I used for that:

panel = new JPanel(new BorderLayout());
l1 = new JLabel("l1");
button = new JButton("B");
l2 = new JLabel("l2");
panel.add(l1, BorderLayout.WEST);
panel.add(button, BorderLayout.CENTER);
panel.add(l2, BorderLayout.EAST);

I've also tried GridBagLayout, and the closest I've come there is to have them spaced out, but not hugging the sides:

enter image description here

Code for that:

panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL;
l1 = new JLabel("L1");
l2 = new JLabel("L2");
button = new JButton("B");
c.weightx = Integer.MAX_VALUE;
c.gridx = 0;
panel.add(l1, c);
c.weightx = 1;
c.gridx = 1;
panel.add(button, c);
c.weightx = Integer.MAX_VALUE;
c.gridx = 2;
panel.add(l2, c);

Any ideas?

3 Answers 3

4

Here are two ways to doit. Once with a BorderLayout and once with a GridBagLayout:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGridBagLayout {

    protected void initUI1() {
        final JFrame frame = new JFrame("Grid bag layout");
        frame.setTitle(TestGridBagLayout.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel l1 = new JLabel("L1");
        JLabel l2 = new JLabel("L2");
        JButton b = new JButton("B");
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        panel.add(l1, gbc);
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.CENTER;
        panel.add(b, gbc);
        gbc.weightx = 0;
        panel.add(l2, gbc);
        frame.add(panel);
        frame.setSize(800, 100);
        frame.setVisible(true);
    }

    protected void initUI2() {
        final JFrame frame = new JFrame("Border layout");
        frame.setTitle(TestGridBagLayout.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new BorderLayout());
        JLabel l1 = new JLabel("L1");
        JLabel l2 = new JLabel("L2");
        JButton b = new JButton("B");
        JPanel wrappingPanel = new JPanel(new FlowLayout());
        wrappingPanel.add(b);
        panel.add(l1, BorderLayout.WEST);
        panel.add(l2, BorderLayout.EAST);
        panel.add(wrappingPanel);
        frame.add(panel);
        frame.setLocation(0, 125);
        frame.setSize(800, 100);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestGridBagLayout test = new TestGridBagLayout();
                test.initUI1();
                test.initUI2();
            }
        });
    }

}
1
  • Thanks. I used your BorderLayout implementation, since GridBagLayout seems to be more configurable than I need. Jan 24, 2013 at 12:59
4

Try GridLayout with LEFT, CENTER & RIGHT labels.

    JPanel p = new JPanel(new GridLayout(1,0));
    p.add(new JLabel("Test", JLabel.LEFT));
    p.add(new JLabel("Test", JLabel.CENTER));
    p.add(new JLabel("Test", JLabel.RIGHT));

image

SSCCE:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/a/14501446/230513 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel(new GridLayout(1, 0));
        p.add(new JLabel("Test", JLabel.LEFT));
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton("Test"));
        p.add(buttonPanel);
        p.add(new JLabel("Test", JLabel.RIGHT));
        f.add(p, BorderLayout.NORTH);
        f.add(new JPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 120);
            }
        }, BorderLayout.CENTER);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}
1
  • +1, nice idea. Never thought to use GridLayout and alignements to achieve this. Yet, if he uses a JButton in the middle, it will still stretch (like a BorderLayout), won't it? Jan 24, 2013 at 12:45
0

You can add a "cheater," like this:

JButton buttonNeeded = new JButton();
   window.add(buttonNeeded);
JButton cheaterButton = new JButton();
   cheaterButton.setEnabled(false);
   window.add(cheaterButton);
   

The cheaterButton fills the screen, but then the user can't click it or see it. Not great for everything, but it works for adding JButtons, at least.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.