CodyBugstein
CodyBugstein

Reputation: 23302

Having an ActionListener in a separate class use data from JTextfield in original class

I'm trying to learn Swing and JFrame, so I'm creating a really simple program that asks for your name and then displays a box telling you what you've entered. I'm trying to use a separate class from the first to act as the ActionListener and display the name that was typed in. However it's not working for me. I tried creating a constructor in the second class that sets an instance variable to the value taken in the JTextfield but it's not showing up as I expected. Please have a look;

The prompt screen

I want it to say "You've Submitted the name Imray"

And here's my code (I've imported all libraries properly but omitted for space's sake)

This is the main class...

public class NamePrompt extends JFrame{


    private static final long serialVersionUID = 1L;

    String name;

    public NamePrompt(){

        setLayout(new BorderLayout());

        JLabel enterYourName = new JLabel("Enter Your Name Here:");
        JTextField textBoxToEnterName = new JTextField(21);
        JPanel panelTop = new JPanel();
        panelTop.add(enterYourName);
        panelTop.add(textBoxToEnterName);

        JButton submit = new JButton("Submit");
        submit.addActionListener(new SubmitButton(textBoxToEnterName.getText()));
        JPanel panelBottom = new JPanel();
        panelBottom.add(submit);

        //Add panelTop to JFrame
        add(panelTop, BorderLayout.NORTH);
        add(panelBottom, BorderLayout.SOUTH);

        //JFrame set-up
        setTitle("Name Prompt Program");
        //setSize(300, 150);
        pack();
        setLocationRelativeTo(null);


    }



public static void main(String[] args) {
    NamePrompt promptForName = new NamePrompt();
    promptForName.setVisible(true); 
}

}

And this is the ActionListener class:

public class SubmitButton implements ActionListener {

    String nameInput;

    public SubmitButton(String textfield){
        nameInput = textfield;
    }

    @Override
    public void actionPerformed(ActionEvent submitClicked) {
        Component frame = new JFrame();
        JOptionPane.showMessageDialog(frame , "You've Submitted the name " + nameInput );
    }
}

Upvotes: 3

Views: 6251

Answers (3)

SamIAm
SamIAm

Reputation: 2491

I think you should have the SubmitButton class as an inner class of the NamePrompt class. This way you can use the text field's variable without having to pass it along to a new class, which might complicate things.

class SubmitButton implements ActionListener {

//     Do not need this
   // public SubmitButton(String textfield){
     //   nameInput = textfield;
   // }

@Override
public void actionPerformed(ActionEvent submitClicked) {
    Component frame = new JFrame();
    JOptionPane.showMessageDialog(frame , "You've Submitted the name " + textBoxToEnterName.getText());
}

But be sure to define the variables outside of the constructor so it can be used by the inner class:

public class NamePrompt extends JFrame{


private static final long serialVersionUID = 1L;

JLabel enterYourName;
JextField textBoxToEnterName;
JPanel panelTop;
JButton submit; 
JPanel panelBottom;

String name;

public NamePrompt(){ ..... (set up the variables here)

The final class would look like:

    public class NamePrompt extends JFrame{


        private static final long serialVersionUID = 1L;

        String name;
        JLabel enterYourName;
        JTextField textBoxToEnterName;
        JPanel panelTop;
        JButton submit;
        JPanel panelBottom;


        public NamePrompt(){

            setLayout(new BorderLayout());

            enterYourName = new JLabel("Enter Your Name Here:");
            textBoxToEnterName = new JTextField(21);
            panelTop = new JPanel();
            panelTop.add(enterYourName);
            panelTop.add(textBoxToEnterName);

            submit = new JButton("Submit");
            submit.addActionListener(new SubmitButton(textBoxToEnterName.getText()));
            panelBottom = new JPanel();
            panelBottom.add(submit);

            //Add panelTop to JFrame
            add(panelTop, BorderLayout.NORTH);
            add(panelBottom, BorderLayout.SOUTH);

            //JFrame set-up
            setTitle("Name Prompt Program");
            //setSize(300, 150);
            pack();
            setLocationRelativeTo(null);
    }

    class SubmitButton implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent submitClicked) {
            Component frame = new JFrame();
            JOptionPane.showMessageDialog(frame , "You've Submitted the name " + textBoxToEnterName.getText());
        }
    }



    public static void main(String[] args) {
        NamePrompt promptForName = new NamePrompt();
        promptForName.setVisible(true); 
    }
}

Upvotes: 2

Reimeus
Reimeus

Reputation: 159754

You are passing in an empty String into the ActionListener class SubmitButton when it is created and it is never updated once the text changes in the JTextField textBoxToEnterName so nothing is ever displayed.

You could pass the textBoxToEnterName JTextField to gain access to the value when required:

class SubmitButtonListener implements ActionListener {

    private JTextField textfield;

    public SubmitButtonListener(JTextField textfield) {
        this.textfield = textfield;
    }

    @Override
    public void actionPerformed(ActionEvent submitClicked) {
        Component frame = new JFrame();
        JOptionPane.showMessageDialog(frame, "You've Submitted the name "
                + textfield.getText());
    }
}

Upvotes: 5

user529543
user529543

Reputation:

There is the problem:

submit.addActionListener(new SubmitButton(textBoxToEnterName.getText()))

To the listener you are passing a String instead of the textfield, change it to accept textfield :)

And at

public void actionPerformed(ActionEvent submitClicked) {
        Component frame = new JFrame();
        JOptionPane.showMessageDialog(frame , "You've Submitted the name " + nameInput );
    }

ask the textfield current value

Upvotes: 1

Related Questions