Results 1 to 9 of 9
- 10-19-2009, 03:59 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
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
NewJApplet ClassJava 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; } }
SelectLanguage ClassJava 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
EnterSocial ClassJava 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 }
PersonalInfo ClassJava 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 }
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 }
-
- 10-19-2009, 04:16 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
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?
- 10-19-2009, 04:18 AM #4
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
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.
- 10-19-2009, 04:41 AM #6
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
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
-
- 10-19-2009, 05:12 AM #8
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
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.
-
Similar Threads
-
display a new jpanel in an applet
By toymachiner62 in forum Java AppletsReplies: 6Last Post: 10-18-2009, 11:10 PM -
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 display a list of items and on click display subitems?
By mandyj in forum New To JavaReplies: 8Last Post: 12-29-2008, 07:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks