Basically I wan’t to obtain a string from the user, I have created a class called “frames” in which I have a load of methods such as exitChoice(), infoPop(), ect… I wish to create one called getText(), and this is what I have so far:
public String getText()
{
JDialog textBox = new JDialog(frame, "Save Name", true);
JTextField inputField = new JTextField(18);
inputField.setText(save == null ? "new save" : save.saveName);
textBox.setBounds(width, height, 275, 70);
textBox.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
textBox.setLayout(new FlowLayout());
textBox.setAlwaysOnTop(true);
textBox.setResizable(false);
textBox.add(inputField);
textBox.setVisible(true);
return inputField.getText();
}
now I know this won’t work, It simply gets the game stuck and I have to terminate it externally, I also understand why it doesn’t work, that isn’t the problem, I also know how to add a JButton, an action listener and work it from there,
Basically I am trying to create a clean simple method which obtains a String from the user which is ALL contained within the method.
Ideally I would like to write a line which reads along the lines of
//EDIT: I know the method getText() does exist, sorry if it is misleading, I will ammend it
//String saveName = new JTextField.getText();
String saveName = new JTextInputGetterBoxThing();
but as far as I have found so far this doesn’t appear to exist, does anybody have any ideas? or ideally know of a one liner that I have missed?
I think what you want is the
JOptionPane.showInputDialogmethod. Something like this?This shows a dialog with the prompt “Type Something” and a text field for entry. Whatever the user types in the text field is returned by
getUserInput().