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:
.
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:

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:

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?
Here are two ways to doit. Once with a BorderLayout and once with a GridBagLayout: