Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: I can't use JTable in a JFrameForm despite of import

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question I can't use JTable in a JFrameForm despite of import

    Hello everyone, I'm a newbie. I have a question to ask you
    I'm creating a mini program like following figure:
    8691410223_272d1097c1.jpg
    I have already created 2 above Frames.
    When I catch event for Add contact ( in frame Add new contact).
     private void btnAddContactActionPerformed(java.awt.event.ActionEvent evt) {
            DefaultTableModel tm = (DefaultTableModel)jTable1.getModel();
        }
    There are a error is underlined at "jTable1": Netbean has suggested me create class/Field jTable1, but jTable1 has been created in frame "Phone book".
    In my view, I just import the Frame Phone book to Frame Add new contact to use jtable1. Whether it's true? What am I doing wrong? And what's solution?
    Thanks for your help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I can't use JTable in a JFrameForm despite of import

    error is underlined at "jTable1"
    Please copy the full text of the compiler's error messages and paste it here,
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    trchinh_it (May 1st, 2013)

  4. #3
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: I can't use JTable in a JFrameForm despite of import

    Quote Originally Posted by Norm View Post
    Please copy the full text of the compiler's error messages and paste it here,
    Yean, here is my code:
    AddContact Form
    package tut11phonebook;
     
    import javax.swing.JFrame;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    import tut11phonebook.PhoneBook2;
     
     
    public class AddContact extends javax.swing.JFrame {
     
        /** Creates new form AddContact */
        public AddContact() {        
            initComponents();
            this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }
     
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jPanel1 = new javax.swing.JPanel();
            txtPhone = new javax.swing.JTextField();
            txtName = new javax.swing.JTextField();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            btnAddContact = new javax.swing.JButton();
            btnCancel = new javax.swing.JButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Add new contact");
            setResizable(false);
     
            jLabel1.setText("Name");
     
            jLabel2.setText("Phone");
     
            btnAddContact.setText("Add contact");
            btnAddContact.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnAddContactActionPerformed(evt);
                }
            });
     
            btnCancel.setText("Cancel");
     
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(76, 76, 76)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                    .addGap(155, 155, 155)
                                    .addComponent(btnCancel))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(btnAddContact)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 155, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addContainerGap())
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(21, 21, 21)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 80, Short.MAX_VALUE)
                                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
                                    .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGap(39, 39, 39))))
            );
     
            jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtName, txtPhone});
     
            jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnAddContact, btnCancel});
     
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(46, 46, 46)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnAddContact)
                        .addComponent(btnCancel))
                    .addGap(26, 26, 26))
            );
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(19, Short.MAX_VALUE))
            );
     
            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-416)/2, (screenSize.height-231)/2, 416, 231);
        }// </editor-fold>                        
     
        private void btnAddContactActionPerformed(java.awt.event.ActionEvent evt) {                                              
            DefaultTableModel tm = (DefaultTableModel)jTable1.getModel();
        }                                             
     
        public static void main(String args[]) {
            /* 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(AddContact.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(AddContact.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(AddContact.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(AddContact.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() {
                    new AddContact().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton btnAddContact;
        private javax.swing.JButton btnCancel;
        public javax.swing.JLabel jLabel1;
        public javax.swing.JLabel jLabel2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField txtName;
        private javax.swing.JTextField txtPhone;
        // End of variables declaration                   
    }
    PhoneBook2 Frame
    package tut11phonebook;
     
    import javax.swing.JLabel;
    import tut11phonebook.AddContact;
     
    public class PhoneBook2 extends javax.swing.JFrame {
     
        /** Creates new form PhoneBook2 */
        public PhoneBook2() {
            initComponents();
     
     
        }
     
     
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jPanel1 = new javax.swing.JPanel();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTable1 = new javax.swing.JTable();
            btnAddContact = new javax.swing.JButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Phone book");
     
            jTable1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
            jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                    {null, null},
                    {null, null},
                    {null, null},
                    {null, null}
                },
                new String [] {
                    "Name", "Phone"
                }
            ));
            jScrollPane1.setViewportView(jTable1);
            jTable1.getColumnModel().getColumn(0).setHeaderValue("Name");
            jTable1.getColumnModel().getColumn(1).setResizable(false);
            jTable1.getColumnModel().getColumn(1).setHeaderValue("Phone");
     
            btnAddContact.setText("Add Contact");
            btnAddContact.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnAddContactActionPerformed(evt);
                }
            });
     
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(93, 93, 93)
                            .addComponent(btnAddContact)))
                    .addContainerGap())
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(20, 20, 20)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(btnAddContact)
                    .addContainerGap(49, Short.MAX_VALUE))
            );
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
     
            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-330)/2, (screenSize.height-324)/2, 330, 324);
        }// </editor-fold>                        
     
        private void btnAddContactActionPerformed(java.awt.event.ActionEvent evt) {                                              
            close();
            AddContact s = new AddContact();
            s.setVisible(true);
        }                                             
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* 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(PhoneBook2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(PhoneBook2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(PhoneBook2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(PhoneBook2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() {
                    new PhoneBook2().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton btnAddContact;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTable jTable1;
        // End of variables declaration                   
     
        private void close() {
     
        }
    }
    Error displays in AddContact form - btnAddContactActionPerformed

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I can't use JTable in a JFrameForm despite of import

    Where is the full text of the compiler's error message?
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    trchinh_it (May 1st, 2013)

  7. #5
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: I can't use JTable in a JFrameForm despite of import

    Yean, thanks for your suggesion
    here is full text error message:
    cannot find symbol
      symbol:   variable jTable1
      location: class tut11phonebook.AddContact
    ----

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I can't use JTable in a JFrameForm despite of import

    The compiler can not find a definition that is in scope (within the same pair of {}s) for the variable named in the error message.
    Check why it wasn't found: spelling error, definition out of scope, missing completely
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    trchinh_it (May 1st, 2013)

  10. #7
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: I can't use JTable in a JFrameForm despite of import

    Quote Originally Posted by Norm View Post
    The compiler can not find a definition that is in scope (within the same pair of {}s) for the variable named in the error message.
    Check why it wasn't found: spelling error, definition out of scope, missing completely
    I've checked again and ensuring that there is not any spelling error/ missing completely. I just concern why I have created jTable1 (actually that is automatically generate code -drag and drop) in class PhoneBook2 and import it into AddContact then I use jTable1 in AddContact, but it's still not availabe to use. You can see very clear in source code uploaded above
    Help me please!

  11. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I can't use JTable in a JFrameForm despite of import

    Quote Originally Posted by trchinh_it View Post
    I've checked again and ensuring that there is not any spelling error/ missing completely .... Help me please!
    Check again. Where was jTable1 defined?

  12. #9
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: I can't use JTable in a JFrameForm despite of import

    Quote Originally Posted by jps View Post
    Check again. Where was jTable1 defined?
    It was defined in PhoneBook2 Frame
     private void initComponents() {
     
            jPanel1 = new javax.swing.JPanel();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTable1 = new javax.swing.JTable();     
    ................
    ......................
    // Variables declaration - do not modify                     
        private javax.swing.JButton btnAddContact;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTable jTable1;
        // End of variables declaration

    I wondered whether I misunderstood about scope of JTable1 .? I created it in PhoneBook then import and use in AddContact2
    Please look at my code

  13. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I can't use JTable in a JFrameForm despite of import

    Quote Originally Posted by trchinh_it View Post
    I wondered whether I misunderstood about scope of JTable1 .?
    The declaration in PhoneBook2 is not visible from within the AddContact class.
    You can use an object of the class to gain access to it's members

Similar Threads

  1. Import if Import is Found, Else
    By blazedGinger in forum Java Theory & Questions
    Replies: 5
    Last Post: March 9th, 2013, 06:43 PM
  2. Replies: 21
    Last Post: November 27th, 2012, 10:58 PM
  3. Import data form text into Jtable
    By redpower1989 in forum AWT / Java Swing
    Replies: 10
    Last Post: November 24th, 2011, 08:35 AM