Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default Referencing JPanel Class

    I want to reference a JPanel I created in the NetBeans GUI Editor. It lives in a class called 'NGWCard1', which extends JPanel and uses 'this' to refer to the JPanel. I can't change the code because it won't let me. Can anyone help?

    I put the class in its own file 'NGWCard1.java' and put that into a package. What I want is to reference the JPanel in NGWCard1 remotely from another class with its own .java file in the package.

    This is the code in the other class that should reference the JPanel in the NGWCard1 class:

    Java Code:
    public void addComponentToPane(Container pane) {
            GetCards();
            ngwcards.add(NGWCard1);
            pane.add(ngwcards, BorderLayout.CENTER);
        }

    Here is NGWCard1

    Java Code:
    public class NGWCard1 extends javax.swing.JPanel {
        
        /**
         * Creates new form NGWCard1
         */
        public NGWCard1() {
            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.
         */
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
        private void initComponents() {
            ngwBackCrd1 = new javax.swing.JButton();
            ngwClearCrd1 = new javax.swing.JButton();
            ngwCancelCrd1 = new javax.swing.JButton();
            ngwNextCrd1 = new javax.swing.JButton();
            jTextField1 = new javax.swing.JTextField();
            jLabel1 = new javax.swing.JLabel();
            ngwInnerJPanel = new javax.swing.JPanel();
    
            ngwBackCrd1.setText("Back");
            ngwBackCrd1.setEnabled(false);
            ngwBackCrd1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ngwBackCrd1ActionPerformed(evt);
                }
            });
    
            ngwClearCrd1.setText("Clear");
            ngwClearCrd1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ngwClearCrd1ActionPerformed(evt);
                }
            });
    
            ngwCancelCrd1.setText("Cancel");
            ngwCancelCrd1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ngwCancelCrd1ActionPerformed(evt);
                }
            });
    
            ngwNextCrd1.setText("Next");
            ngwNextCrd1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ngwNextCrd1ActionPerformed(evt);
                }
            });
    
            jLabel1.setFont(new java.awt.Font("Helvetica", 1, 14));
            jLabel1.setText("Please enter a title for your new game in the field below");
    
            org.jdesktop.layout.GroupLayout ngwInnerJPanelLayout = new org.jdesktop.layout.GroupLayout(ngwInnerJPanel);
            ngwInnerJPanel.setLayout(ngwInnerJPanelLayout);
            ngwInnerJPanelLayout.setHorizontalGroup(
                ngwInnerJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 597, Short.MAX_VALUE)
            );
            ngwInnerJPanelLayout.setVerticalGroup(
                ngwInnerJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 293, Short.MAX_VALUE)
            );
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .add(127, 127, 127)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
                        .add(layout.createSequentialGroup()
                            .add(ngwBackCrd1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 84, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 19, Short.MAX_VALUE)
                            .add(ngwClearCrd1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 84, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(ngwCancelCrd1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 84, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(ngwNextCrd1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 87, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(131, 131, 131))
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(ngwInnerJPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
    
            layout.linkSize(new java.awt.Component[] {ngwBackCrd1, ngwCancelCrd1, ngwClearCrd1, ngwNextCrd1}, org.jdesktop.layout.GroupLayout.HORIZONTAL);
    
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .add(ngwInnerJPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(44, 44, 44)
                    .add(jLabel1)
                    .add(42, 42, 42)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(34, 34, 34)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(ngwBackCrd1)
                        .add(ngwClearCrd1)
                        .add(ngwCancelCrd1)
                        .add(ngwNextCrd1))
                    .add(107, 107, 107))
            );
    
            layout.linkSize(new java.awt.Component[] {ngwBackCrd1, ngwCancelCrd1, ngwClearCrd1, ngwNextCrd1}, org.jdesktop.layout.GroupLayout.VERTICAL);
    
        }// </editor-fold>                        
    
        private void ngwNextCrd1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
        }                                           
    
        private void ngwCancelCrd1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:
        }                                             
    
        private void ngwClearCrd1ActionPerformed(java.awt.event.ActionEvent evt) {                                             
    // TODO add your handling code here:
        }                                            
    
        private void ngwBackCrd1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
        }                                           
    
        public void GetThisCard() {
            initComponents();
        }
        
        
        // Variables declaration - do not modify                     
        private javax.swing.JLabel jLabel1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JButton ngwBackCrd1;
        private javax.swing.JButton ngwCancelCrd1;
        private javax.swing.JButton ngwClearCrd1;
        private javax.swing.JPanel ngwInnerJPanel;
        private javax.swing.JButton ngwNextCrd1;
        // End of variables declaration                   
       
       
    }

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    This is the code in the other class that should reference the JPanel in the NGWCard1 class:
    You should just pass the reference of your ngwcards to the constructor of your other class. After getting that reference, save it inside a local variable and use it in your addComponentToPane method. It will look like this:

    Java Code:
    class OtherClass {
    
    NGWCard1 card;
    
    OtherClass(NGWCard1 card) {
    
        this.card = card;
    
    }
    
    public void addComponentToPane(Container pane) {
            ngwcards.add(card);
            pane.add(ngwcards, BorderLayout.CENTER);
    }
    
    }

  3. #3
    Join Date
    Jul 2007
    Posts
    13
    Rep Power
    0

    Default Reference glitch

    I tried your suggestion, but it seems to confuse another reference I passed to 'Otherclass'. Actually, Otherclass is called 'NewGameWizard' and goes like this (the GetCards method is unnecessary now with your code, but I'm not sure what to do so I left it in):

    Java Code:
    package RpgEngine;
    
    /**
     *
     * @author davidthompson
     */
    
    import RpgEngine.NGWCard1;
    import RpgEngine.NGWCard2;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class NewGameWizard {
        //manages CardLayout
        JDialog newGameWizard;
        JPanel ngwcards; //a panel that uses CardLayout
    
        NGWCard1 card;
    
    NewGameWizard(NGWCard1 card) {
    
        this.card = card;
    
    }
      
        public void addComponentToPane(Container pane) {
            GetCards();
            ngwcards.add(card);
            pane.add(ngwcards, BorderLayout.CENTER);
        }
        
        public void createAndShowGUI() {
            //Create and set up the window.
            newGameWizard = new JDialog(new Frame(), "New Title", true);
            newGameWizard.setDefaultCloseOperation(newGameWizard.DISPOSE_ON_CLOSE);
            //newgamewizardframe.setLocationRelativeTo(Component);
            //Create and set up the content pane.
            NewGameWizard demo = new NewGameWizard();
            demo.addComponentToPane(newGameWizard.getContentPane());
            
            //Display the window.
            newGameWizard.setSize(600,600);
            newGameWizard.setLocationRelativeTo(null);
            newGameWizard.setVisible(true);
            
            ngwcards = new JPanel(new CardLayout());
           
            
        };
        
         public void GetCards(){
            
            NGWCard1 card1 = new NGWCard1();
            card1.GetThisCard();
            NGWCard2 card2 = new NGWCard2();
            card2.GetThisCard();
        }
    
       
    }

Similar Threads

  1. JPanel won't update
    By ibanez270dx in forum New To Java
    Replies: 3
    Last Post: 01-06-2009, 08:59 PM
  2. refresh JPanel
    By olesja in forum AWT / Swing
    Replies: 1
    Last Post: 04-16-2008, 03:58 PM
  3. Problem with JPanel
    By ibanez270dx in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 05:04 PM
  4. JPanel Problems
    By Riftwalker in forum AWT / Swing
    Replies: 6
    Last Post: 10-15-2007, 11:16 PM
  5. Replies: 5
    Last Post: 08-11-2007, 03:14 AM

Posting Permissions

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