Reputation: 15
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
}
}
Upvotes: 1
Views: 981
Reputation: 15
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;
}
Upvotes: 0
Reputation: 285405
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:
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);
}
Upvotes: 4