1

I want to know how to add components dynamically to a JDialog. I know there is a similar question on SO here, but as you can see, I have his solution as a part of my code.

So the idea is that on click of the button, I need to add a component on the dialog. The sample code is below:

import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test extends JFrame {
/**
 * 
 */
private static final long serialVersionUID = 1L;

public static void main(String args[]) {
    Test test = new Test();
    test.createDialog();
}

public void createDialog() {
    DynamicDialog dialog = new DynamicDialog(this);
    dialog.setSize(300, 300);
    dialog.setVisible(true);
}
}

class DynamicDialog extends JDialog {
/**
 * 
 */
private static final long serialVersionUID = 1L;

public DynamicDialog(final JFrame owner) {
    super(owner, "Dialog Title", Dialog.DEFAULT_MODALITY_TYPE);
    final JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    final JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.add(Box.createRigidArea(new Dimension(3, 10)));
    panel.add(createLabel("Click on add"));
    panel.add(Box.createRigidArea(new Dimension(23, 10)));
    panel.add(createLabel("To add another line of text"));
    panel.add(Box.createHorizontalGlue());
    mainPanel.add(panel);
    mainPanel.add(Box.createRigidArea(new Dimension(3, 10)));

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
    buttonPanel.add(Box.createHorizontalGlue());

    JButton button = new JButton();
    button.setText("Add another line");
    buttonPanel.add(button);
    mainPanel.add(buttonPanel);
    mainPanel.add(Box.createRigidArea(new Dimension(3, 10)));

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            Container contentPane = owner.getContentPane();
            JPanel _panel = new JPanel();
            _panel.setLayout(new BoxLayout(_panel, BoxLayout.X_AXIS));
            _panel.add(Box.createHorizontalGlue());
            _panel.add(createLabel("Added!"));
            contentPane.add(_panel);

            contentPane.validate();
            contentPane.repaint();
            owner.pack();
        }
    });

    pack();
    setLocationRelativeTo(owner);
    this.add(mainPanel);
}

JLabel createLabel(String name) {
    JLabel label = new JLabel(name);
    return label;
}
}
Community
  • 1
  • 1
Anand Sainath
  • 1,807
  • 3
  • 22
  • 48

1 Answers1

3

If you add it to the main panel it will work, you were adding it to the content pane of the frame which it seems it does not show up anywhere.

button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {

        JPanel _panel = new JPanel();
        _panel.setLayout(new BoxLayout(_panel, BoxLayout.X_AXIS));
        _panel.add(Box.createHorizontalGlue());
        _panel.add(createLabel("Added!"));
        mainPanel.add(_panel);

        mainPanel.validate();
        mainPanel.repaint();
        owner.pack();
    }
})
Spiff
  • 3,873
  • 4
  • 25
  • 50
  • Though if you have to go with `owner.pack()` then there is no need to explicitly call `child.validate/revalidate` and `child.repaint`, since this would be done implicitly, by virtue of the call to `pack` on owner :-) +1 :-) – nIcE cOw Oct 11 '13 at 16:53