1

I have a JDialog generated using Jetbrains Intellij's GUI designer (as this is a quick one-off GUI project). Inside the dialog I have a JScrollPane which I want to insert components into at runtime

So, on the scroll pane i ticked "Custom Creation" to add the item to my java class, and my code is now roughly as follows:

public class MyDialog extends JDialog { // other fields private JButton addButton; private JScrollPane scrollPane;

public BuilderCreationDialog() {
    setContentPane(contentPane);
    setModal(true);
    getRootPane().setDefaultButton(buttonOK);
   // other action listeners

    addButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            onAdd();
        }
    });
}

private void onAdd() {
    addFieldJPanel("valueA", "valueB");
}

private void addFieldJPanel(final String name, final String type) {
    final JPanel fieldPanel = new JPanel();
    fieldPanel.add(new JLabel(name));
    fieldPanel.add(new JLabel(type));
    final JButton removalButton = new JButton();
    removalButton.addActionListener((e) -> {
        // do something
    });
    fieldPanel.add(removalButton);
    fieldPanel.revalidate();
    fieldPanel.repaint();
    fieldPanel.setVisible(true);
    scrollPane.add(fieldPanel);
    pack();
    scrollPane.revalidate();
    scrollPane.repaint();
}

public static void main(String[] args) {
    MyDialog dialog = new MyDialog();
    dialog.pack();
    dialog.setVisible(true);
    System.exit(0);
}

private void createUIComponents() {
    scrollPane = new JBScrollPane();
}

}

However, when i click on the button the scollPane remains empty. What am I missing?

mangusbrother
  • 3,988
  • 11
  • 51
  • 103

0 Answers0