I had a Java assignment about a month ago, which was about building a GUI. I used GroupLayout to manage the position of the components. I ran into a problem where if I put a very long string of text into a JTextField and resize the outer window, the textfield suddenly "bursts".
I fixed the issue using GridBagLayout, but I wanted to come back to the original problem in hopes of getting a better understanding of GroupLayout.
Here's a SSCCE that demonstrates this problem. (I tried to minimize it as much as I can, I apologize if my example is too long.)
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
JTextField text1;
JTextField text2;
JPanel myPanel;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> new Main());
}
public Main() {
super("Sussy Imposter");
createComponents();
setLayout();
configureSettings();
}
public void createComponents() {
text1 = new JTextField(20);
text2 = new JTextField(20);
text1.setMaximumSize(text1.getPreferredSize());
text2.setMaximumSize(text2.getPreferredSize());
myPanel = new JPanel();
myPanel.setBackground(Color.CYAN);
myPanel.setPreferredSize(new Dimension(100, 100));
}
public void setLayout() {
Container c = getContentPane();
GroupLayout groupLayout = new GroupLayout(c);
c.setLayout(groupLayout);
groupLayout.setAutoCreateGaps(true);
groupLayout.setAutoCreateContainerGaps(true);
groupLayout.setHorizontalGroup(
groupLayout.createSequentialGroup()
.addComponent(myPanel)
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(text1)
.addComponent(text2))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup()
.addComponent(myPanel)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(text1)
.addComponent(text2))
);
}
public void configureSettings() {
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
When I copy-paste this text: Let me send you to space

