0

I am currently working on a JFrame that needs to accept user input and add that to an object inside of an arrayList. The issue is, its simple to make the JFrame with JButtons, but when I press one of the buttons to open the JTextField, it just has a blank JFrame that doesn't accept any user input.

Here is my code:

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Scanner;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;



    public class SwingObserver {

        Scanner scan = new Scanner(System.in);
        JFrame frame;
        //ArrayList<Customer>cust = new ArrayList<Customer>();
        ArrayList<Magazine>natGeo = new ArrayList<Magazine>();

        public static void main(String[]args){

            SwingObserver example = new SwingObserver();
            example.go();

        }
        public void go(){

            frame = new JFrame("Subscriber Hub");
            frame.setVisible(true);

            JButton button1 = new JButton("Add Subscriber");
            JButton button2 = new JButton("Delete Subscriber");
            JButton button3 = new JButton("Send message");

            button1.addActionListener(new addListener());
            //button2.addActionListener(new removeListener());
            //button3.addActionListener(new messageListener());

            frame.getContentPane().add(BorderLayout.WEST, button1);
            frame.getContentPane().add(BorderLayout.CENTER, button2);
            frame.getContentPane().add(BorderLayout.EAST, button3);
            frame.setSize(500, 500);


        }

        class addListener implements ActionListener{

            @Override
            public void actionPerformed(ActionEvent event) {
                // TODO Auto-generated method stub
                frame.setVisible(false); //sets main menu frame to invisible

                Magazine x = new Magazine();

                JFrame frameAdd = new JFrame("Add Subscriber");
                JTextField text = new JTextField(15);

                frameAdd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frameAdd.add(text);
                frameAdd.setSize(500,500);
                frameAdd.setVisible(true);

                String address = text.getText();
                x.setAddress(address);

                natGeo.add(x);

                System.out.println(x.getAddress());


                //frame.setVisible(true);


            }


        }

    }

Please let me know what I'm doing wrong, I've been stuck for quite a while. Thank you

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • You're trying to retrieve text from the JTextField **when its created** which is before the user has had any opportunity to enter information. You can't do that. Instead get the text in response to some event, such as a button's ActionListener. Or if the JTextField is displayed in a modal dialog, then when the dialog is no longer visible. – Hovercraft Full Of Eels Feb 28 '17 at 01:32
  • Also have a look at [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) to see why you'll want to change your GUI's structure. – Hovercraft Full Of Eels Feb 28 '17 at 01:35

0 Answers0