1

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

        }

    }
mets1993
  • 15
  • 1
  • 4
  • What is your question? Is it how to get value form JTextField? – Nabin Aug 09 '14 at 00:36
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Aug 09 '14 at 02:16
  • Using the code i have written i want help getting the value from the JTextField in the method that called the constructor for the MyFrame2 ojbect. I tried making a get method and it did not work. The problem i think is that the program tries to get input from the textfield before the textfield is even filled. – mets1993 Aug 09 '14 at 02:59
  • 1
    Chit, I've already told you how to solve this. – Hovercraft Full Of Eels Aug 09 '14 at 03:42

2 Answers2

4

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);
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • sorry the line [many = Integer.parseInt(name);] was supposed to be written like that. i am new to java and don't really understand a lot of the gui classes and how to use them. I did suspect that was the case as to why i was getting an error when trying to get the input from the Jtextfield. I did try using a get function to see if i could get the text to return but it didn't work. How would i go about making my Jdialog modal because i think this is the best solution? – mets1993 Aug 09 '14 at 01:12
  • @rominesvn: the [JDialog API](http://docs.oracle.com/javase/8/docs/api/javax/swing/JDialog.html) will show you constructors available that can create a modal JDialog. I usually use the constructor that accepts a JFrame (a Window actually), a String, and a ModalityType in that order. – Hovercraft Full Of Eels Aug 09 '14 at 01:56
0

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;
    }
mets1993
  • 15
  • 1
  • 4