Results 1 to 4 of 4
  1. #1
    lost1 is offline Member
    Join Date
    Nov 2007
    Posts
    3
    Rep Power
    0

    Default 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,



    Java 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);
        }
    }

  2. #2
    lost1 is offline Member
    Join Date
    Nov 2007
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the help.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java 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 [i]initDialog[/i] and [i]actionPerformed[/i]
        // 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 [i]actionPerformed[/i] 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 [i]setSize[/i].
            // But okay if you want.
            profileDialog.pack();
            profileDialog.setVisible(true);
        }
    
        private void initDialog()
        {
            profileDialog = new JDialog(this, false);
            // [i]dispose[/i] 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);
        }
    }

  4. #4
    lost1 is offline Member
    Join Date
    Nov 2007
    Posts
    3
    Rep Power
    0

    Default Appreciated.

    Thanks for the response. I actually figured it all out and recoded everything and it all works now.

    Again, thanks for your response.

Similar Threads

  1. Run RCP action from outside of the Eclipse RCP
    By leonbandas in forum Eclipse
    Replies: 1
    Last Post: 01-07-2008, 07:15 AM
  2. Multiple listeners per event
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-03-2008, 10:06 AM
  3. jsp:forward action
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 10:04 AM
  4. jsp:param action
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 10:03 AM
  5. Few action in one Jbutton
    By kubiasty in forum New To Java
    Replies: 0
    Last Post: 07-25-2007, 10:19 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
  •