Reputation: 32323
I have a panel formatted with a GridBagLayout
. Even if I set the size using setMinimumSize
, it still gets too small. When you shrink the window (just using mouse drag), it goes past the size of the label so that the label says like firs...
. However, only the left most label shrinks like this.
However, as you can see from running the code below, it is still possible for the panel to go off the edge of the window. I am okay with that behavior, I just don't want the label's text to be messed up. To clarify: I want neither label to shrink, I want the panel to slide off the edge of the surrounding panel.
Here is the simplest example I could come up with that reproduces this behavior in a way that's easy to see:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class TestMain {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestMain window = new TestMain();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TestMain() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel outer = new JPanel();
outer.setLayout(new BoxLayout(outer, BoxLayout.Y_AXIS));
outer.setBorder(new EmptyBorder(30,30,30,30));
outer.setBackground(Color.DARK_GRAY);
JPanel inner = new JPanel();
inner.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel labelOne = new JLabel();
labelOne.setText("first label");
c.anchor = GridBagConstraints.LINE_END;
inner.add(labelOne, c);
JLabel labelTwo = new JLabel();
labelTwo.setText("second label");
c.anchor = GridBagConstraints.LINE_START;
c.gridx = 2;
inner.add(labelTwo, c);
c.gridx = 1;
c.weighty = 1.0;
inner.add(Box.createHorizontalStrut(100), c);
outer.add(inner);
frame.add(outer);
}
}
Run this code, then resize the window horizontally. Eventually the window gets resized small enough that the label gets messed up.
Upvotes: 2
Views: 711
Reputation: 32323
Based on @camrickr's comments, I tried wrapping the inner panel in a FlowLayout
panel that otherwise does nothing, and that fixed it.
import java.awt.Color;
public class TestMain {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestMain window = new TestMain();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TestMain() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel outer = new JPanel();
outer.setLayout(new BoxLayout(outer, BoxLayout.Y_AXIS));
outer.setBorder(new EmptyBorder(30,30,30,30));
outer.setBackground(Color.DARK_GRAY);
JPanel middleFlowLayout = new JPanel(); // Added this wrapping panel
JPanel inner = new JPanel();
inner.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel labelOne = new JLabel();
labelOne.setText("first label");
c.anchor = GridBagConstraints.LINE_END;
inner.add(labelOne, c);
JLabel labelTwo = new JLabel();
labelTwo.setText("second label");
c.anchor = GridBagConstraints.LINE_START;
c.gridx = 2;
inner.add(labelTwo, c);
c.gridx = 1;
c.weighty = 1.0;
inner.add(Box.createHorizontalStrut(100), c);
middleFlowLayout.add(inner); // Wrap the inner pannel
outer.add(middleFlowLayout);
frame.add(outer);
}
}
Upvotes: 2
Reputation: 324098
I don't know why the GridBagLayout does what it does. But if you give a weightx=1.0 to each label they will both shrink.
The simple solution is to use a FlowLayout. The components are always displayed at their preferred sizes.
Upvotes: 2