简体   繁体   中英

Get input from JTextfield

I was able to make the code do what i want except for the fact i can't get input from this now. What i want to do now is still get text from the JTextfield and return it to the main method that creates the frame object. Can somebody help me get the input from the following code?

 package assignment5;

    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class Frame2 extends JDialog implements ActionListener 
    {

        private String a;
        private JTextField field = new JTextField(30); 

        public Frame2(JFrame parent, String title, String message) 
        {
            super(parent, title, true);

            if (parent != null) 
            {
                this.setSize(500,150);
                this.setLocation(400,200);
            }

            JPanel messagePane = new JPanel();
            messagePane.add(new JLabel(message));
            getContentPane().add(messagePane, BorderLayout.PAGE_START);

            JPanel buttonPane = new JPanel();
            JPanel button2Pane = new JPanel();

            JButton button = new JButton("OK"); 
            JButton button2 = new JButton("Cancel");

            buttonPane.add(button); 
            button2Pane.add(button2);

            getContentPane().add(buttonPane, BorderLayout.PAGE_END);
            getContentPane().add(button2Pane,BorderLayout.EAST);

            //button.addActionListener(this);
            button.addActionListener(new ActionListener()
            {
                @Override 
                public void actionPerformed( ActionEvent event)
                {
                    parent.dispose();
                }
            });

            button2.addActionListener(new ActionListener()
            {
                @Override 
                public void actionPerformed( ActionEvent event)
                {
                    parent.dispose();
                }
            });

            JPanel textPane = new JPanel();
            JTextField field = new JTextField(30); 
            textPane.add(field);
            getContentPane().add(textPane, BorderLayout.CENTER);

            field.addActionListener(this);


            setDefaultCloseOperation(DISPOSE_ON_CLOSE);

            //pack(); 
            setVisible(true);
}


        public void b ()
        {
            a = field.getText();
        }

        public String g ()
        {
            System.out.println("wasdfsdf" + a);
            return a;
        }

        @Override
        public void actionPerformed(ActionEvent event) 
        {
            a = field.getText();
            System.out.println("wasdfsdfsafddsfsdfsdfsafdsggsdfsdf" + a);
            // TODO Auto-generated method stub

        }

    }

You're creating a non-modal JFrame when you really want to instead use a modal JDialog. Change the MyFrame2 to a modal JDialog and your problems are solved.

Details as to why:

  • When you create and display a non-modal JFrame, program flow continues immediately in the calling code below the code where you display the JFrame.
  • This means that you'll be trying to extract data from the JTextField before the user's had nary a chance to add information to it.
  • With a modal JDialog on the other hand, code flow halts in the calling code immediately when you display the JDialog, and it does not resume until the JDialog is no longer visible.
  • Thus the code below where you display the dialog won't get called until the dialog has been dealt with by the user, and now hopefully the JTextField in the dialog will have useful information in it.

Edit
I'm surprised that this will even compile:

many = Integer.parseInt()

You're parsing nothing on this line, and that won't fly.

You would want to give your MyFrame2 a public method that extracts the String from its JTextField and then call the method after displaying it. Something like:

public String getMyTextFieldText() {
    return myTextField.getText();
}

And then in the calling code:

MyFrame2 myFrame2 = new MyFrame2();
myFrame2.setVisible(true);

String text = myFrame2.getMyTextFieldText();
if (text != null && !text.trim().isEmpty()) {
  int someNumber = Integer.parseInt(text);
}

while modality did help to prevent the dialogs from appearing at the same time however that did not solve my problem I had to add the following lines to make sure that it did what I wanted:

public void actionPerformed(ActionEvent event) 
    {
        setTexts(field.getText());
    }

    public String getTexts() {
        return texts;
    }

    public void setTexts(String text) {
        this.texts = text;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM