Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-13-2007, 11:04 AM
Member
 
Join Date: Nov 2007
Posts: 3
lost1 is on a distinguished road
Action Event and Listeners
I'm new here; hello. I've got my code below and I can't seem to fgiure out how to get the information from PersonalProfile frame to display in the ProfleInfo frame. Can anyone help me out?

Thanks,



Code:
import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class PersonalProfile extends JFrame implements ActionListener { //Declare variables for user input public JFrame PersonalProfile = new JFrame("Personal Profile"); public JLabel NameLabel = new JLabel("Name"); public JTextField Name = new JTextField(); public JLabel AddressLabel = new JLabel("Address"); public JTextArea Address = new JTextArea(); public JRadioButton bs, ns, phd; public JCheckBox vb = new JCheckBox("Visual Basic"); public JCheckBox j = new JCheckBox("Java"); public JCheckBox sjsp = new JCheckBox("Servlets and JSP"); public JCheckBox dbp = new JCheckBox("Database Programming"); public JButton CreateProfile = new JButton("Create Profile"); public JFrame ProfileInfo = new JFrame(); //New instance of Person Profile class public PersonalProfile() { Border lineBorder = new LineBorder(Color.BLACK, 1); //Name panel using Border layout manager JPanel pan1 = new JPanel(new BorderLayout()); JLabel NameLabel = new JLabel("Name"); JTextField Name = (new JTextField(20)); pan1.add(NameLabel, BorderLayout.WEST); pan1.add(Name, BorderLayout.SOUTH); //Address panel using Border layout manager JPanel pan2 = new JPanel(new BorderLayout()); JLabel AddressLabel = new JLabel("Address"); JTextArea Address = (new JTextArea(30, 20)); Address.setLineWrap(true); pan2.add(AddressLabel, BorderLayout.WEST); pan2.add(Address, BorderLayout.EAST); //Degree panel using Grid Layout JPanel pan3 = new JPanel(new GridLayout(3, 2, 5, 5)); JRadioButton bs = new JRadioButton("B.S"); JRadioButton ms = new JRadioButton("M.S"); JRadioButton phd = new JRadioButton("Ph.D"); //A grouping instance to prevent more than one radio button selection at a time ButtonGroup DegreeGroup = new ButtonGroup(); //Adding radio buttons to group DegreeGroup.add(bs); DegreeGroup.add(ms); DegreeGroup.add(phd); //Add radio buttons to the panel pan3.add(bs); pan3.add(ms); pan3.add(phd); pan3.setBorder(lineBorder); //Skills/experience panel JPanel pan4 = new JPanel(new GridLayout(4, 2, 5, 5)); pan4.add(vb); pan4.add(j); pan4.add(sjsp); pan4.add(dbp); pan4.setBorder(lineBorder); JPanel pan5 = new JPanel(); setLayout(new BorderLayout()); pan5.add(CreateProfile, BorderLayout.CENTER); setLayout(new GridLayout(5, 1, 5, 5)); add (pan1); add (pan2); add (pan3); add (pan4); add (pan5); CreateProfile.addActionListener(this); } public void actionPerformed(ActionEvent e) { ProfileInfo.pack(); ProfileInfo.setTitle("Profile Info"); ProfileInfo.setSize(300,400); ProfileInfo.getContentPane().setLayout(new GridLayout()); ProfileInfo.setVisible(true); ProfileInfo.setLocationRelativeTo(null); ProfileInfo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(NameLabel); getContentPane().add(Name); getContentPane().add(AddressLabel); getContentPane().add(Address); String text = Name.getText(); Address.getText(); } public static void main(String[] args) { PersonalProfile frame = new PersonalProfile(); frame.pack(); frame.setTitle("Personal Profile"); frame.setSize(300,400); frame.setVisible(true); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-13-2007, 12:21 PM
Member
 
Join Date: Nov 2007
Posts: 3
lost1 is on a distinguished road
Thanks for the help.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-13-2007, 09:20 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,141
hardwired is on a distinguished road
Code:
import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class PP extends JFrame implements ActionListener { //Declare variables for user input // This class is a (extends) JFrame so we don't need this line. // public JFrame PersonalProfile = new JFrame("Personal Profile"); // JDialog components. // These need to be declared as member variables (in class scope) // so they can be seen in both initDialog and actionPerformed // methods below. Use different names to differentiate these // variables from the JFrame component variable names. public JLabel profileNameLabel = new JLabel("Name"); public JTextField profileName = new JTextField(); public JLabel profileAddressLabel = new JLabel("Address"); public JTextArea profileAddress = new JTextArea(); // JFrame components. // Declaring your components inside the constructor with the same // name as the components you were trying to use in your (other JFrame) // dialog hides the member variables. So you were trying to read the // member variables which had no user input when you wanted to get the // user input from the components which were added to the JFrame. But // these JFrame components were not accessible since they were declared // as local variables. We need to declare the JFrame components as // member variables (next) so they can be seen in the constructor and // in the actionPerformed method where we need to read/retrieve // their input data to pass to the dialog components. JTextField Name; JTextArea Address; // These variables were hidden/shadowed by the local variable // declarations in the constructor. Okay now. public JRadioButton bs, ms, phd; // typo ^ public JCheckBox vb = new JCheckBox("Visual Basic"); public JCheckBox j = new JCheckBox("Java"); public JCheckBox sjsp = new JCheckBox("Servlets and JSP"); public JCheckBox dbp = new JCheckBox("Database Programming"); public JButton CreateProfile = new JButton("Create Profile"); // Better to have only one JFrame for an app. // Use dialogs for other displays. // public JFrame ProfileInfo = new JFrame(); JDialog profileDialog; //New instance of Person Profile class public PP() { initDialog(); Border lineBorder = new LineBorder(Color.BLACK, 1); //Name panel using Border layout manager JPanel pan1 = new JPanel(new BorderLayout()); // Local variable. JLabel NameLabel = new JLabel("Name"); // Instantiate member variable (in class scope). Name = (new JTextField(20)); pan1.add(NameLabel, BorderLayout.WEST); pan1.add(Name, BorderLayout.SOUTH); //Address panel using Border layout manager JPanel pan2 = new JPanel(new BorderLayout()); JLabel AddressLabel = new JLabel("Address"); Address = (new JTextArea(30, 20)); Address.setLineWrap(true); pan2.add(AddressLabel, BorderLayout.WEST); pan2.add(Address, BorderLayout.EAST); //Degree panel using Grid Layout JPanel pan3 = new JPanel(new GridLayout(3, 2, 5, 5)); // Declaring these radioButtons here as local variables hides // the member variable declarations made above. So the member // variables will have a null value when you try to access // them in event code later. To fix this remove the local // declarations so that we only instantiate them (with the // "new" operator) here. /*JRadioButton*/ bs = new JRadioButton("B.S"); /*JRadioButton*/ ms = new JRadioButton("M.S"); /*JRadioButton*/ phd = new JRadioButton("Ph.D"); //A grouping instance to prevent more than one radio button // selection at a time ButtonGroup DegreeGroup = new ButtonGroup(); //Adding radio buttons to group DegreeGroup.add(bs); DegreeGroup.add(ms); DegreeGroup.add(phd); //Add radio buttons to the panel pan3.add(bs); pan3.add(ms); pan3.add(phd); pan3.setBorder(lineBorder); //Skills/experience panel JPanel pan4 = new JPanel(new GridLayout(4, 2, 5, 5)); pan4.add(vb); pan4.add(j); pan4.add(sjsp); pan4.add(dbp); pan4.setBorder(lineBorder); JPanel pan5 = new JPanel(); setLayout(new BorderLayout()); pan5.add(CreateProfile, BorderLayout.CENTER); setLayout(new GridLayout(5, 1, 5, 5)); add (pan1); add (pan2); add (pan3); add (pan4); add (pan5); CreateProfile.addActionListener(this); } public void actionPerformed(ActionEvent e) { // Now you can retrieve the input data from the JFrame // components and post it to/in the JDialog components. String text = Name.getText(); profileName.setText(text); profileAddress.setText(Address.getText()); // Not necessary since you have already called setSize. // But okay if you want. profileDialog.pack(); profileDialog.setVisible(true); } private void initDialog() { profileDialog = new JDialog(this, false); // dispose removes the native peer; the dialog is // not affected. profileDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); profileDialog.setTitle("Profile Info"); profileDialog.setSize(300,400); profileDialog.getContentPane().setLayout(new GridLayout(0,1)); profileDialog.setLocationRelativeTo(null); // Calling getContentPane actually is retrieving the JFrame // contentPane, ie, the enclosing class. So specify the dialog. profileDialog.getContentPane().add(profileNameLabel); profileDialog.getContentPane().add(profileName); profileDialog.getContentPane().add(profileAddressLabel); profileDialog.getContentPane().add(profileAddress); } public static void main(String[] args) { PP frame = new PP(); frame.setTitle("Personal Profile"); // Only one of these next two lines is needed, not both. // frame.pack(); frame.setSize(300,400); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Always call this last. frame.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-14-2007, 05:26 AM
Member
 
Join Date: Nov 2007
Posts: 3
lost1 is on a distinguished road
Appreciated.
Thanks for the response. I actually figured it all out and recoded everything and it all works now.

Again, thanks for your response.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run RCP action from outside of the Eclipse RCP leonbandas Eclipse 1 01-07-2008 08:15 AM
Multiple listeners per event Java Tip Java Tips 1 01-03-2008 11:06 AM
jsp:forward action Java Tip Java Tips 0 12-24-2007 11:04 AM
jsp:param action Java Tip Java Tips 0 12-24-2007 11:03 AM
Few action in one Jbutton kubiasty New To Java 0 07-25-2007 11:19 AM


All times are GMT +3. The time now is 10:40 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org