View Single Post
  #3 (permalink)  
Old 11-13-2007, 10:20 PM
hardwired hardwired is online now
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
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); } }
Reply With Quote