Results 1 to 7 of 7
- 10-17-2009, 09:46 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
display a new jpanel in an applet
God applets just boggle my mind, but anyways. I've created a class which extends JApplet and calls a class which extends JPanel. I have a button on this JPanel class and when it is clicked, I want it to display a different JPanel.
I'm not sure how to do this.
Here's my JApplet class
and it calls this JPanel classJava Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * NewJApplet.java * * Created on Oct 16, 2009, 6:52:11 PM */ package mainPackage; import javax.swing.JApplet; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; /** * * @author Administrator */ public class NewJApplet extends javax.swing.JApplet { /** Initializes the applet NewJApplet */ public void init() { try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { Language lan = new Language(); setContentPane(lan); } }); } 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() { javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 298, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 297, Short.MAX_VALUE) ); }// </editor-fold> // Variables declaration - do not modify // End of variables declaration }
Java Code:/* /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * Language.java * * Created on Oct 17, 2009, 2:40:54 PM */ package mainPackage; /** * * @author Administrator */ public class Language extends javax.swing.JPanel { /** Creates new form Language */ public Language() { 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() { jLabel1 = new javax.swing.JLabel(); englishButton = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N jLabel1.setText("Please select a language"); englishButton.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N englishButton.setText("English"); englishButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { englishButtonActionPerformed(evt); } }); jButton2.setText("jButton2"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addComponent(englishButton) .addComponent(jButton2)) .addContainerGap(225, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addGap(18, 18, 18) .addComponent(englishButton) .addGap(58, 58, 58) .addComponent(jButton2) .addContainerGap(148, Short.MAX_VALUE)) ); }// </editor-fold> private void englishButtonActionPerformed(java.awt.event.ActionEvent evt) { EnterSocial social = new EnterSocial(); setContentPane(social); } // Variables declaration - do not modify private javax.swing.JButton englishButton; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; // End of variables declaration }
and when the englishButton is clicked, I want it to display this JPanel class
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * EnterSocial.java * * Created on Oct 17, 2009, 2:29:39 PM */ package mainPackage; /** * * @author Administrator */ public class EnterSocial extends javax.swing.JPanel { /** Creates new form EnterSocial */ public EnterSocial() { 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() { jLabel1 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jLabel1.setText("jLabel1"); jTextField1.setText("jTextField1"); jButton1.setText("jButton1"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addGroup(layout.createSequentialGroup() .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jButton1))) .addContainerGap(240, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1)) .addContainerGap(234, Short.MAX_VALUE)) ); }// </editor-fold> // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JTextField jTextField1; // End of variables declaration }
In the second piece of code, where it says setContentPane(social); gives an error. Anybody know how to do this?
-
It's all about passing and using references. for instance:
NewJApplet.java
Language.javaJava Code:public class NewJApplet extends javax.swing.JApplet { public void init() { try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { // pass a reference of the applet to the Language object Language lan = new Language(NewJApplet.this); setContentPane(lan); } }); } catch (Exception ex) { ex.printStackTrace(); } } //........
Myself, I'd not bother with all of this but instead use a CardLayout.Java Code:public class Language extends javax.swing.JPanel { private NewJApplet newapplet; Language( NewJApplet newapplet) { this.newapplet = newapplet; initComponents(); } //....... private void englishButtonActionPerformed(java.awt.event.ActionEvent evt) { EnterSocial social = new EnterSocial(); newapplet.setContentPane(social); social.revalidate(); newapplet.repaint(); }
-
For the record: I thoroughly hate NetBeans-generated code. It's ugly, bloated, and impossible to read and quickly understand what it's doing. In the future, if possible, you would be better off posting non-NetBeans generated code. This way more folks can compile your code, and can read it. All you need to post is code that creates a JPanel with a button and label which would replace 200 lines with 10 max.
-
For instance, here's a CardLayout example. See how much shorter and more understandable the code is without the NetBeans-generated junk:
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class NewJApplet extends JApplet { public static final String PANEL1 = "Panel 1"; public static final String PANEL2 = "Panel 2"; private CardLayout cardlayout = new CardLayout(); private JPanel mainPanel = new JPanel(cardlayout); public void init() { try { javax.swing.SwingUtilities.invokeAndWait(new Runnable() { public void run() { createGUI(); } }); } catch (Exception e) { System.err.println("createGUI didn't successfully complete"); } } private void createGUI() { Panel1 panel1 = new Panel1(this); Panel2 panel2 = new Panel2(this); mainPanel.add(panel1, PANEL1); mainPanel.add(panel2, PANEL2); getContentPane().add(mainPanel); } public void swapCard(String name) { cardlayout.show(mainPanel, name); } } class Panel1 extends JPanel { public Panel1(final NewJApplet newapplet) { JButton selectEnglish = new JButton("English"); selectEnglish.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { newapplet.swapCard(NewJApplet.PANEL2); } }); add(new JLabel("Please Select a Language:")); add(selectEnglish); } } class Panel2 extends JPanel { public Panel2(final NewJApplet newapplet) { add(new JLabel("Panel 2")); } }
- 10-17-2009, 10:47 PM #5
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
Thanks your code worked great!
- 10-18-2009, 10:02 PM #6
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
CardLayout does look alot cleaner.
I have another question now. If I want to now call another JPanel, how would I do that. If i copy this
into my EnterSocial class, I get an error in my Language class at the lineJava Code:private NewJApplet newapplet; /** Creates new form Language */ public Language(NewJApplet newapplet){ this.newapplet = newapplet; initComponents(); }
EnterSocial social = new EnterSocial();
Do you know why this is? Am i trying to instantiate something twice?Java Code:private void englishButtonActionPerformed(java.awt.event.ActionEvent evt) { EnterSocial social = new EnterSocial(); newapplet.setContentPane(social); social.revalidate(); newapplet.repaint(); }
-
No idea. You're far better off creating a small non-netbeans class that compiles and demonstrates your error.
Similar Threads
-
Convert THE APPLET CODE TO SWING USING JPANEL
By jammyjamsticy in forum AWT / SwingReplies: 2Last Post: 12-22-2011, 09:16 AM -
can't get anything to display on my JPanel
By v1nsai in forum AWT / SwingReplies: 9Last Post: 08-25-2009, 08:25 AM -
Display output in Jframe or JPanel
By rammurugesan in forum AWT / SwingReplies: 12Last Post: 04-14-2009, 01:45 PM -
How to display image from byte array in JPANEL
By waqasdaar in forum AWT / SwingReplies: 0Last Post: 03-22-2009, 12:11 AM -
how to draw x-y graph in Jpanel.--not in APPLET.
By vincent2001@gmail.com in forum New To JavaReplies: 2Last Post: 08-24-2008, 05:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks