Results 1 to 11 of 11
Thread: How to make a JPanel focusable
- 10-20-2009, 12:23 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
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.) } }); } }
-
1) JPanels in general don't get focus.
2) Didn't we already go over this stuff yesterday? It's not a focus issue.
- 10-20-2009, 02:21 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
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.
- 10-20-2009, 02:24 AM #4
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
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
-
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.
- 10-20-2009, 04:53 AM #6
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
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
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 FurableJava 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.) } });
-
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.
- 10-20-2009, 05:20 AM #8
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
I thought about that but this class
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).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); } }); }
-
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-21-2009, 03:34 AM #10
Member
- Join Date
- Oct 2009
- Posts
- 63
- Rep Power
- 0
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.
- 10-21-2009, 10:09 AM #11
Member
- Join Date
- Oct 2009
- Posts
- 1
- Rep Power
- 0
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
-
.add to a JPanel
By harrier in forum NetBeansReplies: 11Last Post: 07-13-2010, 10:24 AM -
need help with JPanel Images
By Thebigchalupa in forum New To JavaReplies: 5Last Post: 04-23-2009, 07:15 PM -
How to make a Jframe un-focusable when another Jframe is active?
By Robert_85 in forum Advanced JavaReplies: 4Last Post: 04-22-2009, 11:02 PM -
How to use Jpanel
By Manfizy in forum NetBeansReplies: 0Last Post: 02-19-2009, 12:34 PM -
How to make a JPanel highlighted on mouseclick
By aneesahamedaa in forum AWT / SwingReplies: 3Last Post: 09-01-2008, 09:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks