I agree with mre (1+ to his answer), but I would also like to add that you may need to call pack()
on the JDialog after adding or removing components especially if the dialog will need to resize to accomodate the component as your images indicate may happen.
Edit 1
For example with a JFrame (but it works the same with a JDialog):
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SwingFoo extends JPanel {
private JTextField nameField = new JTextField(10);
private JComboBox searchTermsCombo = new JComboBox();
private JButton addNewFieldBtn = new JButton("Add New Field");
private JButton submitBtn = new JButton("Submit");
private JPanel centerPanel = new JPanel(new GridBagLayout());
private int gridY = 0;
public SwingFoo() {
GridBagConstraints gbc = createGBC(0, gridY);
centerPanel.add(new JLabel("Name:"), gbc);
gbc = createGBC(1, gridY);
centerPanel.add(nameField, gbc);
gridY++;
gbc = createGBC(0, gridY);
centerPanel.add(new JLabel("Search Terms:"), gbc);
gbc = createGBC(1, gridY);
centerPanel.add(searchTermsCombo, gbc);
gridY++;
addNewFieldBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addNewFieldAction(e);
}
});
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.PAGE_AXIS));
JPanel addNewFieldPanel = new JPanel(new GridLayout(1, 0));
addNewFieldPanel.add(addNewFieldBtn);
addNewFieldPanel.add(new JLabel());
JPanel submitPanel = new JPanel(new BorderLayout());
submitPanel.add(submitBtn);
bottomPanel.add(addNewFieldPanel);
bottomPanel.add(Box.createVerticalStrut(5));
bottomPanel.add(submitPanel);
int eb = 8;
setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
setLayout(new BorderLayout());
add(centerPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
}
private void addNewFieldAction(ActionEvent e) {
GridBagConstraints gbc = createGBC(0, gridY);
centerPanel.add(new JLabel("New Item:"), gbc);
gbc = createGBC(1, gridY);
centerPanel.add(new JTextField(10), gbc);
gridY++;
Window win = SwingUtilities.getWindowAncestor(addNewFieldBtn);
if (win != null) {
win.pack();
win.setLocationRelativeTo(null);
}
}
private GridBagConstraints createGBC(int x, int y) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = (x == 0) ? gbc.LINE_START : gbc.LINE_END;
gbc.fill = (x == 0) ? gbc.BOTH : gbc.HORIZONTAL;
gbc.insets = (x == 0) ? new Insets(5, 0, 5, 5) : new Insets(5, 5, 5, 0);
return gbc;
}
private static void createAndShowGui() {
JFrame frame = new JFrame("SwingFoo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SwingFoo());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
BorderLayout.CENTER
(orBorderLayout.ANYTHING
for that matter). – SladeAdd New Field
button should probably be above where the new fields appear. That way the user could add 5 new fields by clicking the button 5 times (without having to move the mouse during the clicking). – Slade