How can I have get the layout in picture ? Current issue I am facing is that I am able see the dropdown 1 and text box but I do not see the dropdown 2 and text box aligned to it displayed.
private final JComboBox firstComboBox;
private final JComboBox secondComboBox;
private final JTextField textField;
private final JTextField textField2;
private final JPanel cards = new JPanel(new CardLayout());
private final JPanel first = new JPanel();
private final JPanel second = new JPanel();
public Layout() {
super(BoxLayout.X_AXIS);
Vector<Enum> v = new Vector<Enum>();
firstComboBox = new JComboBox(v);
secondComboBox = new JComboBox(v);
first.add(firstComboBox);
second.add(secondComboBox);
textField = new JTextField(50);
textField.setDocument(new JTextFieldLimit(50));
cards.add(first);
cards.add(second);
add(cards);
first.add(textField, "NORMAL");
first.add(new JPanel(), "EXISTS");
textField2 = new JTextField(50);
textField2.setDocument(new JTextFieldLimit(50));
second.add(textField2, "NORMAL");
second.add(new JPanel(), "EXISTS");
}
I copied your code into my IDE. The code was so confusing to me that I cleared the screen and started from scratch.
Here's the GUI I came up with, based on your drawing.
I used a GridBagLayout
as I suggested in my comments.
Here's the complete, no changes needed, absolutely runnable code.
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class GridBagLayoutTest3 implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new GridBagLayoutTest3());
}
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public JPanel createMainPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 5, 5, 5);
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy = 0;
JLabel label = new JLabel("Title");
label.setFont(panel.getFont().deriveFont(Font.BOLD, 36f));
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label, gbc);
gbc.gridwidth = 1;
gbc.gridy++;
String[] greek = { "alpha", "beta", "gamma" };
JComboBox<String> greekComboBox = new JComboBox<>(greek);
panel.add(greekComboBox, gbc);
gbc.gridx++;
JTextField textField1 = new JTextField(40);
panel.add(textField1, gbc);
gbc.gridx = 0;
gbc.gridy++;
String[] ordinals = { "first", "second", "third" };
JComboBox<String> ordinalsComboBox = new JComboBox<>(ordinals);
panel.add(ordinalsComboBox, gbc);
gbc.gridx++;
JTextField textField2 = new JTextField(40);
panel.add(textField2, gbc);
return panel;
}
}