Results 1 to 7 of 7
  1. #1
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default 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

    Java 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
    
    }
    and it calls this JPanel class
    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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's all about passing and using references. for instance:

    NewJApplet.java
    Java 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();
            }
        }
    
        //........
    Language.java
    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();
        }
    Myself, I'd not bother with all of this but instead use a CardLayout.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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"));
       }
       
    }

  5. #5
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    Thanks your code worked great!

  6. #6
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    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

    Java Code:
    private NewJApplet newapplet;
        
        
        /** Creates new form Language */
        public Language(NewJApplet newapplet){
            this.newapplet = newapplet;
            initComponents();
        }
    into my EnterSocial class, I get an error in my Language class at the line
    EnterSocial social = new EnterSocial();

    Java Code:
    private void englishButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
            EnterSocial social = new EnterSocial();
            newapplet.setContentPane(social);
            social.revalidate();
            newapplet.repaint();
        }
    Do you know why this is? Am i trying to instantiate something twice?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Convert THE APPLET CODE TO SWING USING JPANEL
    By jammyjamsticy in forum AWT / Swing
    Replies: 2
    Last Post: 12-22-2011, 10:16 AM
  2. can't get anything to display on my JPanel
    By v1nsai in forum AWT / Swing
    Replies: 9
    Last Post: 08-25-2009, 09:25 AM
  3. Display output in Jframe or JPanel
    By rammurugesan in forum AWT / Swing
    Replies: 12
    Last Post: 04-14-2009, 02:45 PM
  4. How to display image from byte array in JPANEL
    By waqasdaar in forum AWT / Swing
    Replies: 0
    Last Post: 03-22-2009, 01:11 AM
  5. how to draw x-y graph in Jpanel.--not in APPLET.
    By vincent2001@gmail.com in forum New To Java
    Replies: 2
    Last Post: 08-24-2008, 06:01 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
  •