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

    Default How to make a JPanel focusable

    I want to execute some code when this JPanel becomes focused. This JPanel is called from another JPanel. Im not sure where/how to to make an focusListener for this. I assume that's what I need.

    Java Code:
    package cardlayout;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusEvent;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JSeparator;
    import javax.swing.JTextField;
    import javax.swing.event.AncestorListener;
    
    
    public class PersonalInfo extends javax.swing.JPanel {
    
        private static JTextField socialSecurityNo;
        private static JTextField firstName;
        private static JTextField lastName;
        private static JTextField address;
        private static JTextField dob;
    
        public PersonalInfo(final NewJApplet newapplet) {
    
            socialSecurityNo = new JTextField(20);
            firstName = new JTextField(20);
            lastName = new JTextField(20);
            address = new JTextField(20);
            dob = new JTextField(20);
            JButton yesButton = new JButton("Yes");
            JButton noButton = new JButton("No");
    
            add(new JLabel("Your Personal Information"));
            add(new JSeparator());
            add(new JLabel("Social Security Number"));
            add(socialSecurityNo);
            add(new JLabel("First Name"));
            add(firstName);
            add(new JLabel("Last Name"));
            add(lastName);
            add(new JLabel("Address"));
            add(dob);
            add(new JLabel("Date of Birth"));
            add(new JLabel("Is this correct?"));
            add(yesButton);
            add(noButton);
    
    
           yesButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //put new JPanel in here// newapplet.swapCard(newapplet.)
                }
            });
    
            noButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //put new JPanel in here// newapplet.swapCard(newapplet.)
                }
            });
        }
    }

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

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

    Default

    You never answered it. You said "whatever. much luck". I cleaned up the question alot and now it's easier to read. I was hoping this would allow more people to want to help.

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

    Default

    If a JPanel can't be focused then I want to know what I can do to make it so when the JPanel becomes visible, a method can be called like an actionListener

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

    Default

    I said much luck because you never seemed to address my concerns and didn't seem to believe me when I told you that a focus listener isn't needed. Again, you don't need a focus listener because you know when the PersonalInfo panel is going to be displayed because it only happens when you tell it to be displayed (via the cardlayout). You can retrieve the information from the database at that time and put it into the panel. if the information is retrievable (I don't know this for a fact as we don't see the database code), it should be displayed correctly.
    Last edited by Fubarable; 10-20-2009 at 05:01 AM.

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

    Default

    I think i've addressed everything you asked in my other post, but just to clear up any confusion, I have an applet I created. I have a JPanel with buttons on it and when a user clicks that, It goes to another JPanel. I should add that i'm using CardLayout. On this new JPanel, a user enters their Social and clicks the ok button. Once this button is clicked, another JPanel comes up that has JTextFields in them. When this JPanel comes up, I want to query a database and populate the JTextFields, based on the Social that was entered in the previous JPanel.

    I know how to query the database and populate the JTextFields, but the problem is that I need to create an actionListener(I assume) for when this JPanel becomes visible because so that way I can call my method to query the database. Right now if I call my db query in between these lines

    Java Code:
    add(new JLabel("Is this correct?"));
            add(yesButton);
            add(noButton);
    
    
           yesButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //put new JPanel in here// newapplet.swapCard(newapplet.)
                }
            });
    It just executes when the applet is started and so it returns no results. I hope this helps clear up the confusion. Not trying to piss ya off Furable

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

    Default

    Why not call the query and fill the fields wherever you call swapCard(personalInfo)? That's when you know for a fact that the personInfo panel will be displayed.

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

    Default

    I thought about that but this class
    Java Code:
    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
                    Social.setSocial(enterSocial.getText());
    
                    newapplet.swapCard(NewJApplet.PersonalInfo);
                    
    
                }
            });
    
        }
    is the one that calls the JPanel with the textFields and so the textFields can't be referenced (at least I don't know how to).

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

    Default

    I would consider getting that actionlistener code out of that class. For instance,
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MainPanel extends JPanel {
       public static final String PANEL1 = "Panel 1";
       public static final String PANEL2 = "Panel 2";
       
       private Panel1 panel1 = new Panel1();
       private Panel2 panel2 = new Panel2();
       private CardLayout cardlayout = new CardLayout();
       
       public MainPanel() {
          setLayout(cardlayout);
          add(panel1, PANEL1);
          add(panel2, PANEL2);
          
          panel1.addOKActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                String text = panel1.getFieldText();
                panel2.setFieldText(text);
                swapCard(PANEL2);
             }
          });
       }
       
       public void swapCard(String name) {
          cardlayout.show(this, name);
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("MainPanel");
          frame.getContentPane().add(new MainPanel());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
       
    }
    
    class Panel1 extends JPanel {
       JTextField field = new JTextField(10);
       JButton okBtn = new JButton("OK");
       
       Panel1() {
          add(field);
          add(okBtn);
       }
       
       public void addOKActionListener(ActionListener al) {
          okBtn.addActionListener(al);
       }
       
       public String getFieldText() {
          return field.getText();
       }
    }
    
    class Panel2 extends JPanel {
       JTextField field = new JTextField(10);
       
       public Panel2() {
          add(field);
       }
       
       public void setFieldText(String text) {
          field.setText(text);
       }
    }

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

    Default

    You the man Fubarable. Your soluton worked great. I think that's something i'm having a hard time with is passing reference to things in different classes. I know how to make one big long program with many methods just fine, but when it comes to classes I get confused.

    Thanks again for your help.

  11. #11
    tictac is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Default

    Hi everyone,

    I am new to the site and just wanted to add to what I have found lately when trying to get focus for a JScrollPane. I am building a customized builder and could NOT get the JScrollPane to trigger FocusListener event with a mouse click however the tab function triggers the Focus event.

    Therefore I added mouse clicked event Listener for the JScrollPane and within MouseClicked I added the line e.getComponent().requestFocus() which in turn triggers the FocusListener event.

    Hope this helps anyone trying to do the same.

Similar Threads

  1. .add to a JPanel
    By harrier in forum NetBeans
    Replies: 11
    Last Post: 07-13-2010, 10:24 AM
  2. need help with JPanel Images
    By Thebigchalupa in forum New To Java
    Replies: 5
    Last Post: 04-23-2009, 07:15 PM
  3. Replies: 4
    Last Post: 04-22-2009, 11:02 PM
  4. How to use Jpanel
    By Manfizy in forum NetBeans
    Replies: 0
    Last Post: 02-19-2009, 12:34 PM
  5. How to make a JPanel highlighted on mouseclick
    By aneesahamedaa in forum AWT / Swing
    Replies: 3
    Last Post: 09-01-2008, 09:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •