Retrieving JTextField Contents Like Scanner

流过昼夜 提交于 2019-12-20 02:52:55

问题


I'm trying to set up a GUI for a program of mine, and have got it mostly working. However, I would like to be able to create a method that works a lot like Scanner's nextLine(); it waits for the input from my JTextField, and then returns it. It seemed like this question was very similar to mine but did not wait for the user's input. Here is my GUI's current code:

package util;

import java.awt.Font;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import com.jgoodies.forms.factories.DefaultComponentFactory;

public class Gui {

    public JFrame frame;
    private JTextField textField;
    private final JLabel lblVector = DefaultComponentFactory.getInstance().createTitle("Please type commands below.");
    private JScrollPane scrollPane;
    private JTextArea textArea;

    /**
     * Create the application.
     */
    public Gui() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    public void print(String text){
        textArea.append(text+"\n");
    }
    public String getInput()
    {
        String input = textField.getText();
        textField.setCaretPosition(0);
        return input;
    }
    private void initialize() {
        frame = new JFrame("Vector");
        frame.setBounds(100, 100, 720, 720);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        textField = new JTextField();
        frame.getContentPane().add(textField, BorderLayout.SOUTH);
        textField.setColumns(10);
        frame.getContentPane().add(lblVector, BorderLayout.NORTH);

        scrollPane = new JScrollPane();
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

        textArea = new JTextArea();
        textArea.setFont(new Font("Monospaced", Font.PLAIN, 15));
        textArea.setEditable(false);
        scrollPane.setViewportView(textArea);
    }
}

I'd like to be able to call it like so:

String menus = gui.getInput();

or such; I've already set up the gui variable as a new Gui().

From my searches, I've gathered it would probably involve either a DocumentListener, or an ActionListener, or both.


回答1:


Add an ActionListener to the text field. When the text field has focus and the user presses Enter an event will be fired. See How to Write an Action Listener for more details.




回答2:


After a little bit of searching, I found this question which answered mine:

public String getInput() throws InterruptedException
{
    final CountDownLatch latch = new CountDownLatch(1);
    KeyEventDispatcher dispatcher = new KeyEventDispatcher() {
        // Anonymous class invoked from EDT
        public boolean dispatchKeyEvent(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER)
                latch.countDown();
            return false;
        }
    };
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(dispatcher);
    latch.await();  // current thread waits here until countDown() is called
    KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(dispatcher);
    String input = textField.getText();
    textField.setCaretPosition(0);
    return input;
}

Thanks for everybody's help!



来源:https://stackoverflow.com/questions/17641301/retrieving-jtextfield-contents-like-scanner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!