In this example I use the Box
class to organize components in a JPanel
. My problem is the presence of an unjustified gap that shouldn’t be there. Simply the text field should come at the beginning without the preceding gap. How to fix it?
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
public class GUIExample {
private JPanel container;
private JTextField tf1, tf2, tf3;
private JSplitPane splitpane;
public GUIExample() {
container = new JPanel(new BorderLayout());
tf1 = new JTextField();
tf2 = new JTextField();
tf3 = new JTextField();
splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitpane.setDividerSize(3);
splitpane.setDividerLocation(100);
splitpane.setTopComponent(tf1);
splitpane.setBottomComponent(tf2);
Box boxContainer = Box.createVerticalBox();
boxContainer.add(splitpane);
boxContainer.add(tf3);
container.add(boxContainer, BorderLayout.NORTH);
}
protected JPanel getContainer() {
return this.container;
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> {
GUIExample gui = new GUIExample();
JFrame frame = new JFrame("Example");
frame.setMinimumSize(new Dimension(400, 100));
frame.add(gui.getContainer());
frame.setVisible(true);
frame.pack();
});
}
}