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

    Default How to display info when a JPanel gains focus

    I took Furables advice and I'm using a CardLayout and not using the net-beans GUI builder this time. I'm trying to make it so when my PersonalInfo class gains focus, the JTextFields will fill in. I have a method that calls a database to get the fields. The problem is that when I execute String soc = Social.getSocial(); in the PersonalInfo class, it executes before the JPanel gains focus so it's null. and when it calls the method to query the database it's obviously null.

    Basically what i'm trying to say is I need to create a method for when the JPanel gains focus. I'm not sure how to do that.

    I have 5 classes created.

    Social Class
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package cardlayout;
    
    /**
     *
     * @author Administrator
     */
    public class Social {
    
        private static String socialSecurityNo;
    
        public Social(){
            socialSecurityNo = "";
        }
    
        public Social(String social){
            socialSecurityNo = social;
        }
        public static void setSocial(String social){
            socialSecurityNo = social;
        }
    
        public static String getSocial() {
            return socialSecurityNo;
        }
    
        public String toString() {
            return socialSecurityNo;
        }
    
    }
    NewJApplet Class
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * NewJApplet.java
     *
     * Created on Oct 18, 2009, 5:22:48 PM
     */
    
    package cardlayout;
    
    import java.awt.CardLayout;
    import javax.swing.JPanel;
    
    /**
     *
     * @author Administrator
     */
    public class NewJApplet extends javax.swing.JApplet {
    
        public static final String SelectLanguage = "Panel 1";
        public static final String EnterSocial = "Panel 2";
        public static final String PersonalInfo = "Panel 3";
        private CardLayout cardLayout = new CardLayout();
        private JPanel mainPanel = new JPanel(cardLayout);
    
        /** Initializes the applet NewJApplet */
        public void init() {
            try {
                java.awt.EventQueue.invokeAndWait(new Runnable() {
                    public void run() {
                        createGUI();
    
                    }
                });
            } catch (Exception ex) {
                System.err.println("createGUI didn't complete successfully");
            }
        }
    
        private void createGUI() {
            SelectLanguage lan = new SelectLanguage(this);
            EnterSocial soc = new EnterSocial(this);
            PersonalInfo per = new PersonalInfo(this);
            mainPanel.add(lan, SelectLanguage);
            mainPanel.add(soc, EnterSocial);
            mainPanel.add(per, PersonalInfo);
            getContentPane().add(mainPanel);
        }
    
        public void swapCard(String name) {
            cardLayout.show(mainPanel, name);
        }
    }
    
    
    
    
        /*
        @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, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );
        }// </editor-fold>
    */
    
        // Variables declaration - do not modify
        // End of variables declaration
    SelectLanguage Class
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * Panel1.java
     *
     * Created on Oct 18, 2009, 5:34:44 PM
     */
    
    package cardlayout;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    
    /**
     *
     * @author Administrator
     */
    public class SelectLanguage extends javax.swing.JPanel {
    
        public SelectLanguage(final NewJApplet newapplet){
    
            JButton selectEnglish = new JButton("English");
            JButton selectSomali = new JButton("Somali");
            JButton selectVietnamese = new JButton("Vietnamese");
            JButton selectHebrew = new JButton("Hebrew");
            JButton selectCambodian = new JButton("Cambodian");
            JButton selectChinese = new JButton("Chinese");
            JButton selectJapanese = new JButton("Japanese");
            add(new JLabel("Please select a language"));
            add(selectEnglish);
            add(selectSomali);
            add(selectVietnamese);
            add(selectHebrew);
            add(selectCambodian);
            add(selectChinese);
            add(selectJapanese);
    
            selectEnglish.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    newapplet.swapCard(NewJApplet.EnterSocial);
                }
            });
    
            selectSomali.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    newapplet.swapCard(NewJApplet.EnterSocial);
                }
            });
            selectVietnamese.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    newapplet.swapCard(NewJApplet.EnterSocial);
                }
            });
            selectHebrew.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    newapplet.swapCard(NewJApplet.EnterSocial);
                }
            });
            selectCambodian.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    newapplet.swapCard(NewJApplet.EnterSocial);
                }
            });
            selectChinese.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    newapplet.swapCard(NewJApplet.EnterSocial);
                }
            });
            selectJapanese.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    newapplet.swapCard(NewJApplet.EnterSocial);
                }
            });
    
    
            
        }
    
       /*
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );
        }// </editor-fold>
    */
    
        // Variables declaration - do not modify
        // End of variables declaration
    
    }
    EnterSocial Class
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * Panel2.java
     *
     * Created on Oct 18, 2009, 5:44:43 PM
     */
    
    package cardlayout;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    
    /**
     *
     * @author Administrator
     */
    public class EnterSocial extends javax.swing.JPanel {
    
        public EnterSocial(final NewJApplet newapplet) {
            
            add(new JLabel("Please Enter Your Social Security Number"));
            final JTextField enterSocial = new JTextField(10);
            JButton okButton = new JButton("OK");
            add(enterSocial);
            add(okButton);
    
          
            okButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //sets the Social class to whatever is in the textfield
                    //sets the Social Security number to a String
                    Social.setSocial(enterSocial.getText());
    
                    //Social.setSocial(enterSocial.getText());
                    System.out.println("EnterSocial social is: " + Social.getSocial());
    
                    newapplet.swapCard(NewJApplet.PersonalInfo);
                    
                }
            });
    
        }
    
    
    
        /*
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );
        }// </editor-fold>
    */
    
        // Variables declaration - do not modify
        // End of variables declaration
    
    }
    PersonalInfo Class
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * Panel2.java
     *
     * Created on Oct 18, 2009, 5:44:43 PM
     */
    
    package cardlayout;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    
    /**
     *
     * @author Administrator
     */
    public class EnterSocial extends javax.swing.JPanel {
    
        public EnterSocial(final NewJApplet newapplet) {
            
            add(new JLabel("Please Enter Your Social Security Number"));
            final JTextField enterSocial = new JTextField(10);
            JButton okButton = new JButton("OK");
            add(enterSocial);
            add(okButton);
    
          
            okButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //sets the Social class to whatever is in the textfield
                    //sets the Social Security number to a String
                    Social.setSocial(enterSocial.getText());
    
                    //Social.setSocial(enterSocial.getText());
                    System.out.println("EnterSocial social is: " + Social.getSocial());
    
                    newapplet.swapCard(NewJApplet.PersonalInfo);
                    
                }
            });
    
        }
    
    
    
        /*
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );
        }// </editor-fold>
    */
    
        // Variables declaration - do not modify
        // End of variables declaration
    
    }

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

    Default

    Quote Originally Posted by toymachiner62 View Post
    I took Furables advice and I'm using a CardLayout and not using the net-beans GUI builder this time. I'm trying to make it so when my PersonalInfo class gains focus, the JTextFields will fill in. I have a method that calls a database to get the fields. The problem is that when I execute String soc = Social.getSocial(); in the PersonalInfo class, it executes before the JPanel gains focus so it's null. and when it calls the method to query the database it's obviously null.
    This doesn't make sense. What is obviously null? The JPanel? Nope. It doesn't need focus to be non-null. Something else? Likely. Why does Social class even have a static getSocial() method?

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

    Default

    I meant its obviously null because it executes right away when the applet starts and so it isn't even at the JPanel which asks for the Social Security Number and sets it to Social.

    How do I make it so it won't execute that statement until that JPanel gains focus?

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

    Default

    getSocial was static because how I was using it previously I was trying to instantiate a copy of Social and then set a variable to it and then get that variable in a different class and that was the only way I could get it to work.

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

    Default

    Quote Originally Posted by toymachiner62 View Post
    getSocial was static because how I was using it previously I was trying to instantiate a copy of Social and then set a variable to it and then get that variable in a different class and that was the only way I could get it to work.
    Don't you see how that static method as it is currently configured and used in your program will always return null? I'm guessing that your problem has nothing to do with GUI, cardlayout or focus, but rather that this is the problem. A litle debugging on your part will prove whether I am right or wrong.

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

    Default

    But if I set Socal.setSocial inside of a button ActionEvent and then printout the value it works fine. It's just that if it's not inside of an actionEvent it executes when the program starts

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

    Default

    Quote Originally Posted by toymachiner62 View Post
    But if I set Socal.setSocial inside of a button ActionEvent and then printout the value it works fine. It's just that if it's not inside of an actionEvent it executes when the program starts
    So what is the current problem???

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

    Default

    I want the statement Social.getSocial(); to execute in the class PersonalInfo, when that JPanel appears. I also want to call a method in the class PersonalInfo when that JPanel appears. Right now that line executes when the program starts.

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

Similar Threads

  1. display a new jpanel in an applet
    By toymachiner62 in forum Java Applets
    Replies: 6
    Last Post: 10-18-2009, 11:10 PM
  2. can't get anything to display on my JPanel
    By v1nsai in forum AWT / Swing
    Replies: 9
    Last Post: 08-25-2009, 08:25 AM
  3. Display output in Jframe or JPanel
    By rammurugesan in forum AWT / Swing
    Replies: 12
    Last Post: 04-14-2009, 01: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, 12:11 AM
  5. Replies: 8
    Last Post: 12-29-2008, 07:12 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
  •