Results 1 to 4 of 4
  1. #1
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default Change JList using JDialog

    My problem is, I have a Jlist inside a JApplet, and I want to change the data in list from a JDialog. The JDialog pops up and the user clicks button on jDialog. They clik a button on jdialog to change the values on the Japplet's JList, but I cannot get it to work. I keep getting NULLPointerException. I am using Netbeans GUI builder and I made a small example of my problem since my original code is very large and has many classes. Someone Please Help!

    JApplet code.....
    Java Code:
    package testjapplet;
    
    import javax.swing.JFrame;
    
    public class testApplet extends javax.swing.JApplet 
    {
        private testDialog theDialog;
        public static void main(String[] args) {
            //... Create an initialize the applet.
            testApplet theApplet = new testApplet();
            
            theApplet.init();         // Needed if overridden in applet
            
            //theApplet.start();        // Needed if overridden in applet
            
            //... Create a window (JFrame) and make applet the content pane.
            JFrame window = new JFrame("Applet Test");
            window.setContentPane(theApplet);
            
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.pack();              // Arrange the components.
            //System.out.println(theApplet.getSize());
            window.setVisible(true);    // Make the window visible.
        }
        /** Initializes the applet testApplet */
        @Override
        public void init() {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(testApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(testApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(testApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(testApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /* Create and display the applet */
            try {
                java.awt.EventQueue.invokeAndWait(new Runnable() {
    
                    public void run() {
                        initComponents();
                    }
                });
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
        /** This method is called from within the init() method to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jScrollPane1 = new javax.swing.JScrollPane();
            theJList = new javax.swing.JList();
            openJButton = new javax.swing.JButton();
            exitJButton = new javax.swing.JButton();
    
            jScrollPane1.setViewportView(theJList);
    
            openJButton.setText("Open");
            openJButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    openJButtonActionPerformed(evt);
                }
            });
    
            exitJButton.setText("Exit");
            exitJButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    exitJButtonActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(52, 52, 52)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(openJButton)
                            .addGap(36, 36, 36)
                            .addComponent(exitJButton)))
                    .addContainerGap(59, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(40, 40, 40)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(openJButton)
                        .addComponent(exitJButton))
                    .addContainerGap())
            );
        }// </editor-fold>
    public void changeList(String[] stuffIn)
    {
        theJList.setListData(stuffIn);
    }
    private void openJButtonActionPerformed(java.awt.event.ActionEvent evt) {
    
            theDialog = new testDialog(null, true);
            theDialog.setVisible(true);
    }
    
    private void exitJButtonActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }
    
        // Variables declaration - do not modify
        private javax.swing.JButton exitJButton;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JButton openJButton;
        private javax.swing.JList theJList;
        // End of variables declaration
    }
    JDialog Code....
    Java Code:
    package testjapplet;
    lass testDialog extends javax.swing.JDialog {
        testApplet testApplet2 = new testApplet();
        /** Creates new form testDialog */
        public testDialog(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            changeJButton = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    
            changeJButton.setText("Change the List");
            changeJButton.setToolTipText("");
            changeJButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    changeJButtonActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(33, 33, 33)
                    .addComponent(changeJButton)
                    .addContainerGap(46, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(43, 43, 43)
                    .addComponent(changeJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(45, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
    private void changeJButtonActionPerformed(java.awt.event.ActionEvent evt) {
            String[] s1 ={"one", "two", "three"};
            testApplet2.changeList(s1);
            setVisible(false);
    }
    
        /**
         * @param args the command line arguments
         */
        // Variables declaration - do not modify
        private javax.swing.JButton changeJButton;
        // End of variables declaration
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Change JList using JDialog

    Your problem is here:

    Java Code:
    class TestDialog extends javax.swing.JDialog {
       TestApplet testApplet = new TestApplet(); // class names capitalized)
    Yes, you are creating a TestApplet object (note that class names should begin with a capital letter to conform with Java naming standards), and calling methods on a viable object, but is it the visualized TestApplet object? Nope it is not, because this code is being called from the dialog, and the visualized TestApplet is already up and running -- it has to be since it created and made the dialog visible. The key here is to call TestApplet methods on the one that is visualized. And to do that, you would pass a reference to the actual TestApplet object into the dialog's constructor. For example (and please note the use of simplified non-NetBeans code to help reduce the shear amount of code we have to slog through):

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class TestApplet2 extends javax.swing.JApplet {
       private TestDialog2 theDialog;
    
       public static void main(String[] args) {
          TestApplet2 theApplet = new TestApplet2();
    
          theApplet.init();
          JFrame window = new JFrame("Applet Test");
          window.setContentPane(theApplet);
    
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          window.pack();
          window.setVisible(true);
       }
    
       @Override
       public void init() {
          try {
             java.awt.EventQueue.invokeAndWait(new Runnable() {
    
                public void run() {
                   initComponents();
                }
             });
          } catch (Exception ex) {
             ex.printStackTrace();
          }
       }
    
       private void initComponents() {
    
          jScrollPane1 = new javax.swing.JScrollPane();
          theJList = new javax.swing.JList();
          openJButton = new javax.swing.JButton();
          exitJButton = new javax.swing.JButton();
    
          jScrollPane1.setViewportView(theJList);
    
          openJButton.setText("Open");
          openJButton.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                openJButtonActionPerformed(evt);
             }
          });
    
          exitJButton.setText("Exit");
          exitJButton.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitJButtonActionPerformed(evt);
             }
          });
    
          JPanel mainPanel = new JPanel();
          mainPanel.add(jScrollPane1);
          mainPanel.add(openJButton);
          mainPanel.add(exitJButton);
    
          getContentPane().add(mainPanel);
       }
    
       public void changeList(String[] stuffIn) {
          theJList.setListData(stuffIn); // !! NPE here
       }
    
       private void openJButtonActionPerformed(java.awt.event.ActionEvent evt) {
          //!! theDialog = new TestDialog2(null, true);
          theDialog = new TestDialog2(null, true, this); //!!
          theDialog.setVisible(true);
       }
    
       private void exitJButtonActionPerformed(java.awt.event.ActionEvent evt) {
          System.exit(0);
       }
    
       private javax.swing.JButton exitJButton;
       private javax.swing.JScrollPane jScrollPane1;
       private javax.swing.JButton openJButton;
       private javax.swing.JList theJList;
    }
    
    class TestDialog2 extends javax.swing.JDialog {
       // !! TestApplet2 testApplet2 = new TestApplet2();
       TestApplet2 testApplet2;
    
       // !! public TestDialog2(java.awt.Frame parent, boolean modal) {
       // super(parent, modal);
       // initComponents();
       // }
    
       // !!
       public TestDialog2(java.awt.Frame parent, boolean modal,
             TestApplet2 testApplet2) {
          super(parent, modal);
          this.testApplet2 = testApplet2; // !!
          initComponents();
       }
    
       private void initComponents() {
    
          changeJButton = new javax.swing.JButton();
    
          setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    
          changeJButton.setText("Change the List");
          changeJButton.setToolTipText("");
          changeJButton.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeJButtonActionPerformed(evt);
             }
          });
    
          JPanel mainPanel = new JPanel();
          mainPanel.add(changeJButton);
          getContentPane().add(mainPanel);
    
          pack();
       }
    
       private void changeJButtonActionPerformed(java.awt.event.ActionEvent evt) {
          String[] s1 = { "one", "two", "three" };
          testApplet2.changeList(s1);
          setVisible(false);
       }
    
       private javax.swing.JButton changeJButton;
    }
    Changes marked by // !! comments

  3. #3
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default Re: Change JList using JDialog

    Thank you that solved my problem.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Change JList using JDialog

    You're welcome! Thanks for letting me know!

Similar Threads

  1. Link one JList to another JList
    By mib1bee in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 07:10 PM
  2. Link one JList to another JList
    By mib1bee in forum Advanced Java
    Replies: 1
    Last Post: 12-30-2010, 06:35 PM
  3. Link one JList to another JList
    By mib1bee in forum New To Java
    Replies: 1
    Last Post: 12-30-2010, 06:24 PM
  4. Diable JList in JDialog at runtime
    By png in forum AWT / Swing
    Replies: 5
    Last Post: 05-31-2010, 03:48 AM
  5. JDialog
    By frenk_castle in forum AWT / Swing
    Replies: 3
    Last Post: 05-11-2010, 12:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •