views:

129

answers:

2

So here I have two classes: Customer Order Class and Confirmation Class. I want to access the data stored in LastNameTextField (Customer Order Class) and set it as the text for UserLastNameLabel (Confirmation Class) after clicking a "Submit" button. For some reason however, the output displays nothing.

Snippet of my code:

package customer_order;

public class customer_order extends Frame{
    private static final long serialVersionUID = 1L;
    private JPanel jPanel = null;
    private JLabel LastNameLabel = null;
    protected JTextField LastNameTextField = null;
    private JButton SubmitButton = null;
    public String s;

    public customer_order() {
        super();
        initialize();
    }

    private void initialize() {
        this.setSize(729, 400);
        this.setTitle("Customer Order");

        this.add(getJPanel(), BorderLayout.CENTER);
    }

    /**
     * This method initializes LastNameTextField    
     *  
     * @return javax.swing.JTextField   
     */
    public JTextField getLastNameTextField() {
        if (LastNameTextField == null) {
            LastNameTextField = new JTextField();
            LastNameTextField.setBounds(new Rectangle(120, 100, 164, 28));
            LastNameTextField.setName("LastNameTextField");
                }

        return LastNameTextField;
    }
    /**
     * This method initializes SubmitButton 
     *  
     * @return javax.swing.JButton  
     */
    private JButton getSubmitButton() {
        if (SubmitButton == null) {
            SubmitButton = new JButton();
            SubmitButton.setBounds(new Rectangle(501, 225, 96, 29));
            SubmitButton.setName("SubmitButton");
            SubmitButton.setText("Submit");
            SubmitButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
                    //THE STRING I WANT
          s = LastNameTextField.getText();
                    java.awt.EventQueue.invokeLater(new Runnable() {
                        public void run() {
                            new confirmation().setVisible(true);

                        }
                    });
                }
            });     
        }
        return SubmitButton;
    }
package customer_order;

public class confirmation extends customer_order{
    private static final long serialVersionUID = 1L;
    private JPanel jPanel = null;  //  @jve:decl-index=0:visual-constraint="58,9"
    private JLabel LastNameLabel = null;
    private JLabel UserLastNameLabel = null;


    //  @jve:decl-index=0:
    /**
     * This method initializes frame    
     *  
     * @return java.awt.Frame   
     */
    public confirmation() {
        super();
        initialize();
    }

    private void initialize() {
        this.setSize(729, 400);
        this.setTitle("Confirmation");

        this.add(getJPanel(), BorderLayout.CENTER);
    }
    /**
     * This method initializes jPanel   
     *  
     * @return javax.swing.JPanel   
     */
    private JPanel getJPanel() {
        if (jPanel == null) {
            UserLastNameLabel = new JLabel();
            UserLastNameLabel.setBounds(new Rectangle(121, 60, 167, 26));
            //THE PROBLEM?
            UserLastNameLabel.setText(s);

        }
        return jPanel;
    }
+3  A: 

The s field will only have a value if the getSubmitButton method is called (it doesn't seem to be in your code), the button it returns is added to the form, and the user has already clicked on the button.

When you call new confirmation(), you get a new object with its own field s, and for s in the new confirmation to have a value it will also need to have the getSubmitButton method called, and the action listener triggered.

I don't really see any reason for confirmation to extend customer_order in this case. Normally you extend a class to specialise - for example, if you had certain orders which were treated differently because they repeated every week you may make a new class repeating_customer_order which adds extra fields to deal with the repeating. Accessing data from one class in another isn't really a reason to make one extend the other.

For what you want to do, I'd suggest removing the inheritence (get rid of extends customer_order), and then passing the value of s into the constructor:

customer_order:

          s = LastNameTextField.getText();
                    java.awt.EventQueue.invokeLater(new Runnable() {
                        public void run() {
                            new confirmation(s).setVisible(true);
                        }
                    });

confirmation:

    private final String s;

    public confirmation(final String s) {
        super();
        this.s = s;
        initialize();
    }

As an aside, your code would be much easier to read if you kept to the standard naming schemes used in most Java apps:

  • classes should begin with an uppercase letter and be CamelCased, e.g. CustomerOrder instead of customer_order
  • fields should begin with a lowercase letter, e.g. lastNameTextField or submitButton

This helps people see at a glance what is going on - at the minute a quick glance at LastNameTextField.getText() makes it look like you're calling a static method in a class called LastNameTextField!

Chris Smith
A: 

good stuff delo

nubme