Swing UI elements not initialized

Answered

Hello,
in my plugin I'm using some forms (f.e. for Language settings) created by Intellij

RMB -> New -> Swing UI Designer -> Create Dialog Class

But that generated component in some cases fails that elements are null -> it seems that when developing a plugin (running gradle runIde) all is ok, but once released and installed from marketplace it fails:

java.awt.IllegalComponentStateException: contentPane cannot be set to null.
	at java.desktop/javax.swing.JRootPane.setContentPane(JRootPane.java:594)
	at java.desktop/javax.swing.JDialog.setContentPane(JDialog.java:1036)
	at gdscript.settings.GdDownloadSdk.<init>(GdDownloadSdk.java:24)
	at gdscript.settings.component.GdSettingsComponents.addSdk$lambda$3(GdSettingsComponents.kt:65)
	at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
	at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
	at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
	at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
	at java.desktop/javax.swing.AbstractButton.doClick(AbstractButton.java:374)
	at java.desktop/javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1028)
	at java.desktop/javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1072)
	at java.desktop/java.awt.Component.processMouseEvent(Component.java:6656)
	at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3385)
	at java.desktop/java.awt.Component.processEvent(Component.java:6421)
...

How are those components supposed to be initialized?

0
8 comments

P.S. I've tried to change gradle build to IntelliJ as writtent at the end of:

https://www.jetbrains.com/help/idea/work-with-gradle-projects.html#delegate_build_gradle

But no luck... or does it not affect builds run with gradle configuration?

0

Ok I've found it.
Not sure why it worked when runned for development, but 'Ive been missing main method inside form class and even found out that maybe I've been building it wrong and should call buildPlugin instead of just build

1

Hey, can your maybe show your (exameple) code. I have the same problem, but dont have any solutions

0

Hi, if I remember correctly, it was following class (shortened version)

public class GdDownloadSdk extends JDialog {
   private JPanel contentPane;
   // other components…
   public GdDownloadSdk() {
       setContentPane(contentPane);
       setModal(true);
       getRootPane().setDefaultButton(buttonOK);
       setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
       addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
               onCancel();
           }
       });
   }
   private void onOK() { … }
   private void onCancel() { … }
   private void createUIComponents() {
       versionCb = new ComboBox<>();
       // initialize other components…
   }
   public static void main(String[] args) {
       GdDownloadSdk dialog = new GdDownloadSdk();
       dialog.pack();
       dialog.setVisible(true);
       System.exit(0);
   }
}

 

And problem was solved by adding public static void main(String[] args) for whatever reason.

Have no idea why it was required and I no longer use it for months, but was probably due to something trying to test that class so it was looking for main method?

0

That's strange, I have it 1:1 exactly the same but it doesn't work for me. how do you open the dialogue? Thanks for your help

0

I can't get rid of this error.

java.awt.IllegalComponentStateException: contentPane cannot be set to null.
 

0

It's not the whole code, contentPane should be initialized at 

// initialize other components
0

I just can't get any further, maybe you can look over our code


public class ConnectDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTabbedPane tabbedPane1;
    private JPasswordField passwordField1;
    private JTexta;
    private JList list1;

    public ConnectDialog() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        // add your code here
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

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

    private void createUIComponents() {
        // place custom component creation code here
        contentPane = new JPanel();
    }
}
0

Please sign in to leave a comment.