Results 1 to 4 of 4
Thread: Change JList using JDialog
- 11-27-2011, 01:08 AM #1
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
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.....
JDialog 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 }
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 }
-
Re: Change JList using JDialog
Your problem is here:
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:class TestDialog extends javax.swing.JDialog { TestApplet testApplet = new TestApplet(); // class names capitalized)
Changes marked by // !! commentsJava 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; }
- 11-28-2011, 10:28 AM #3
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
Re: Change JList using JDialog
Thank you that solved my problem.
-
Similar Threads
-
Link one JList to another JList
By mib1bee in forum AWT / SwingReplies: 1Last Post: 12-31-2010, 07:10 PM -
Link one JList to another JList
By mib1bee in forum Advanced JavaReplies: 1Last Post: 12-30-2010, 06:35 PM -
Link one JList to another JList
By mib1bee in forum New To JavaReplies: 1Last Post: 12-30-2010, 06:24 PM -
Diable JList in JDialog at runtime
By png in forum AWT / SwingReplies: 5Last Post: 05-31-2010, 03:48 AM -
JDialog
By frenk_castle in forum AWT / SwingReplies: 3Last Post: 05-11-2010, 12:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks