开发者

Java: How can I detect the user has finalised their save name without using an actionListener?

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,

Ba开发者_开发问答sically 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.showInputDialog method. Something like this?

public class GetUserInput {

    public static String getUserInput() {
        return JOptionPane.showInputDialog("Type Something");
    }

    public static void main(String[] args) {
        System.out.println("User Input: " + getUserInput());
    }
}

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().


Honestly I'm not sure of having fully understood your problem. Anyway, here's a tutorial on how to make dialogs properly in Swing.

If you use

int ret = JOptionPane.showOptionDialog(new JDialog(), ...);

Your application main frame input is blocked until the JDialog showed is closed. If you don't want to use an ActionListener or something similare (DocumentListener, ...) you can force the user to insert a value in the JTextField, press the ok button and the when showOptionDialog return, manually retrieve the text of the JTextField with getText().

EDIT: I try to extend my answer a little bit. Extends a JDialog to create the desired dialog:

public class CustomDialog extends JDialog{
    private JPanel panel;
    private JTextField field;
    public CustomDialog(){
        panel = new JPanel(); //create a panel possibly with a LayoutManager
        field = new JTextField();
    }
    public JTextField getField(){
        return this.field;
    }

}

Then show the dialog where you need it, and check the field text when it returns:

CustomDialog dialog = new CustomDialog();
int ret = JOptionPane.showOptionDialog(dialog, ...);

String text = dialog.getField().getText();


        public String getSaveName()
        {
            boolean textGot = false;
            String returnText = null;
            while (!textGot)
                {
                    returnText = (String) JOptionPane.showInputDialog(hub.frame, "Enter the name of your save:\n", "Save Box",
                            JOptionPane.PLAIN_MESSAGE, null, null, save == null ? "new save" : save.saveName);

                    if ((returnText != null) && (returnText.length() > 0))
                        {
                            textGot = true;
                        }
                }
            return returnText;
        }

Here is the final method I am using, much cleaner than the old setup I had which involved creating a tiny frame, and adding a textfield and a button with a listener!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜