Search code examples
javaeclipsejbuttonjdialog

How to add a button into a specific JPanel


I have a bigPanel. Inside the bigPanel has multiple panels(panel_1, panel_2, panel_3, etc....)

bigPanel use GridBagLayout. panel_1 and etc use GridLayout.

In the other panel, I have created a ADD button. If user click this, a JDialog appear and user must fill the form:

  1. Button Name:
  2. Number :
  3. Alphabet :

After user finished fill in the JDialog form, a new button will be created based on the JDialog form.

This code will be executed after user click FINISHED button in JDialog form:

if(Number=="1"){
                    if(Alphabet=="A")
                    {   

                    JButton newButton = new JButton(buttonName);                        
                    //bigPanel.add(newButton);
                    //bigPanel.updateUI();  
                    panel_1.add(newButton);
                    panel_1.updateUI();

                    JOptionPane.showMessageDialog(null,"Successfully added !");
                    }

                    }
                    else if(Alphabet=="B"){
                    JButton newButton = new JButton(buttonName);                        
                    //bigPanel.add(newButton);
                    //bigPanel.updateUI();  
                    panel_2.add(newButton);
                    panel_2.updateUI();

                    JOptionPane.showMessageDialog(null,"Successfully added !");
                    }
                    ...
                }
if (Number=="2"){
                    ...
                    }
                 ...

I test the JDialog with Number 1 and Alphabet A. Currently the result I got is warning in console that indicates this line have problems:

panel_1.add(newButton);//line 313

Warning in Console:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at UI2$3.actionPerformed(UI2.java:313)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

The button not added. But if I changed to

bigPanel.add(newButton);
bigPanel.updateUI();

the newButton was added to bigPanel. Unfortunately, I want the newButton to be added into panel_1 not bigPanel..


Solution

  • Please check if panel_1 is added