I am having trouble adding JComponents to a JDialog when the user clicks a button on the JDialog. Basically I want it to look like this:
Then, when the user clicks "Add New Field" I want it to look like this:
I cannot seem to get the dialog to add the new JLabel or JTextField. Can anyone point me in the right direction?
EDIT: This is the action for the "Add New Field" button (Just trying a label now).
@Action public void addNewField() { Container contentPane = getContentPane(); JLabel label = new JLabel ("welkom"); contentPane.add(label, BorderLayout.CENTER); }
SOLUTION:
I used mre's solution and got it to work. Here is my final function:
@Action public void addNewField() { System.out.println("New Field..."); Container contentPane = getContentPane(); JLabel label = new JLabel ("welcome"); label.setBounds(10,10,100,10); //some random value that I know is in my dialog contentPane.add(label); contentPane.validate(); contentPane.repaint(); this.pack(); }
Another one of my problems is that I am using a "Free Design" layout in NetBeans, which meant that my label was probably in some weird position rather than being in the bounds of my dialog (just a guess). I solved this problem with label.setBounds()
so that it showed exactly where I wanted it to.
You can add components to a JDialog just the way you add to a JFrame since JDialog is a java. awt. Container .
JFrame is a normal window with its normal buttons (optionally) and decorations. JDialog on the other side does not have a maximize and minimize buttons and usually are created with JOptionPane static methods, and are better fit to make them modal (they block other components until they are closed).
JDialog(Dialog owner, boolean modal) Creates a dialog box with the specified Dialog owner and modality. JDialog(Dialog owner, String title) Creates a modeless dialog box with the specified Dialog owner and title.
When dynamically adding/removing components from a container, it's necessary to invoke revalidate()
/validate()
and repaint()
afterward. The former will force the container to layout its components again and the latter will remove any visual "artifacts".
to avoiding any further discusion about required/non-required any of Methods ...
notice: for adds/removes JComponents
(simple structured just in one Row/Column and with same Size on Screen
) is sufficient just action JFrame.pack()
,
but for most complete GUI layed by some of standard Swing LayoutManagers
is required usage of
revalidate(); repaint(); // required in most of cases
example for one Column
import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.LineBorder; public class AddComponentsAtRuntime { private JFrame f; private JPanel panel; private JCheckBox checkValidate, checkReValidate, checkRepaint, checkPack; public AddComponentsAtRuntime() { JButton b = new JButton(); b.setBackground(Color.red); b.setBorder(new LineBorder(Color.black, 2)); b.setPreferredSize(new Dimension(600, 10)); panel = new JPanel(new GridLayout(0, 1)); panel.add(b); f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(panel, "Center"); f.add(getCheckBoxPanel(), "South"); f.setLocation(200, 200); f.pack(); f.setVisible(true); } private JPanel getCheckBoxPanel() { checkValidate = new JCheckBox("validate"); checkValidate.setSelected(false); checkReValidate = new JCheckBox("revalidate"); checkReValidate.setSelected(false); checkRepaint = new JCheckBox("repaint"); checkRepaint.setSelected(false); checkPack = new JCheckBox("pack"); checkPack.setSelected(false); JButton addComp = new JButton("Add New One"); addComp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JButton b = new JButton(); b.setBackground(Color.red); b.setBorder(new LineBorder(Color.black, 2)); b.setPreferredSize(new Dimension(600, 10)); panel.add(b); makeChange(); System.out.println(" Components Count after Adds :" + panel.getComponentCount()); } }); JButton removeComp = new JButton("Remove One"); removeComp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int count = panel.getComponentCount(); if (count > 0) { panel.remove(0); } makeChange(); System.out.println(" Components Count after Removes :" + panel.getComponentCount()); } }); JPanel panel2 = new JPanel(); panel2.add(checkValidate); panel2.add(checkReValidate); panel2.add(checkRepaint); panel2.add(checkPack); panel2.add(addComp); panel2.add(removeComp); return panel2; } private void makeChange() { if (checkValidate.isSelected()) { panel.validate(); } if (checkReValidate.isSelected()) { panel.revalidate(); } if (checkRepaint.isSelected()) { panel.repaint(); } if (checkPack.isSelected()) { f.pack(); } } public static void main(String[] args) { AddComponentsAtRuntime makingChanges = new AddComponentsAtRuntime(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With