Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-11-2007, 10:34 PM
Member
 
Join Date: Jul 2007
Posts: 13
uncopywritable is on a distinguished road
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:

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

Here is NGWCard1

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 }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-11-2007, 11:02 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Quote:
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:

Code:
class OtherClass { NGWCard1 card; OtherClass(NGWCard1 card) { this.card = card; } public void addComponentToPane(Container pane) { ngwcards.add(card); pane.add(ngwcards, BorderLayout.CENTER); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-12-2007, 02:31 PM
Member
 
Join Date: Jul 2007
Posts: 13
uncopywritable is on a distinguished road
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):

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(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
refresh JPanel olesja AWT / Swing 1 04-16-2008 04:58 PM
JPanel won't update ibanez270dx New To Java 2 11-19-2007 05:42 AM
Problem with JPanel ibanez270dx New To Java 2 11-09-2007 06:04 PM
JPanel Problems Riftwalker AWT / Swing 6 10-16-2007 12:16 AM
Edit JPanel Text During Runtime...from another class bdn1404 New To Java 5 08-11-2007 04:14 AM


All times are GMT +3. The time now is 10:55 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org