简体   繁体   中英

How to display input text in JOptionPane in java swing

I'm trying to get some input from the user through JTextFields and when an event happens on any of the textfields, a JOptionPane message dialog containing the components name and message pops up. For example, if user enters 'Hello' in textField1, the JOptionPane should pop up like below

带有文本“testField1:Hello”、“确定”按钮的 JOption 弹出窗口

This is what I have as code ..

package testing;
import javax.swing.*;
import java.awt.*;
public class TextFieldFrame extends JFrame{
    
    private JTextField textField1; private JTextField textField2;
    private JTextField textField3; private JPasswordField passwordField;
    
    public TextFieldFrame(){
        setTitle("Testing JTextField and JPasswordField");
        setLayout(new FlowLayout());  
        textField1 = new JTextField(10); add(textField1);
        textField2 = new JTextField("Enter text here"); add(textField2);
        textField3 = new JTextField("Uneditable text field", 21);
        textField3.setEditable(false); add(textField3);
        passwordField = new JPasswordField("Hidden Text"); add(passwordField);
        
        TextFieldHandler handler = new TextFieldHandler();
        
        textField1.addActionListener(handler);
        textField2.addActionListener(handler);
        textField3.addActionListener(handler);
        passwordField.addActionListener(handler);
    }
    
    private class TextFieldHandler extends JOptionPane{
        public void TextFieldHandler(TextFieldHandler handler) {
        String string = ""; // declare string to display
        if(handler.getInputValue() == textField1)
            string = String.format("textField1: %s", textField1.getText());
        else if(handler.getInputValue() == textField2)
            string = String.format("textField2: %s", textField2.getText());
        else if(textField3 == textField3)
            string = String.format("textField3: %s", textField3.getText());
        else if(handler.getInputValue() == passwordField)
            string = String.format("paswordField: %s", passwordField.getPassword());
        JOptionPane.showMessageDialog(null, string);
    }
    }
    
    public static void main(String[] args){
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TextFieldFrame().setVisible(true);
                new TextFieldFrame().setSize(400,100);
            }
        });
    }
}

And I get these errors

error: incompatible types: TextFieldFrame.TextFieldHandler cannot be converted to ActionListener
                textField1.addActionListener(handler);
error: incompatible types: TextFieldFrame.TextFieldHandler cannot be converted to ActionListener
            textField2.addActionListener(handler);
error: incompatible types: TextFieldFrame.TextFieldHandler cannot be converted to ActionListener
            textField3.addActionListener(handler);
error: incompatible types: TextFieldFrame.TextFieldHandler cannot be converted to ActionListener
            passwordField.addActionListener(handler);

You can add a listener to your text fields separately. And for each listener, perform a specific action.

DocumentListener is a good choice. But the problem is that it will call by any change that the user made to the text field. You can not expect it to be called just after typing hello world! It will call the listener for each letter: h, he, hel, hell, ...

JTextField textField1 = new JTextField();
textField.getDocument().addDocumentListener(new DocumentListener() {
    @Override
    public void removeUpdate(DocumentEvent e) {}

    @Override
    public void insertUpdate(DocumentEvent e) {
        JOptionPane.showMessageDialog( null, String.format("textField1: %s", textField1.getText()));
    }

    @Override
    public void changedUpdate(DocumentEvent e) {}
});

But If I were you, I would use the MouseListener and the mouse exit event. So when the user finishes typing and exits its mouse outside of textField, then JOptionPane will be called.

For each of your text fields or password fields, you can add a listener this way:

JTextField textField1 = new JTextField();
textField.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseExited(MouseEvent e) {
        JOptionPane.showMessageDialog( null, String.format("textField1: %s", textField1.getText()));
    }
});

UPDATE

As @Abra mentioned in the comment you can also use FocusListener and focusLost event:

textField.addFocusListener(new FocusAdapter() {
    @Override
    public void focusLost(FocusEvent e) {
        JOptionPane.showMessageDialog( null, String.format("textField1: %s", textField1.getText()));
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM