Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

java - Trying to add an event listener to a JTextField

I'm trying to add an event listener so when you press enter in the JTextField something happens. So far I've got this

SendingHandler sendingHandler;
...
JTextField draftMessage = new JTextField("field");
draftMessage.addActionListener(sendingHandler);

...

private class SendingHandler implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        System.out.println(command);
    }
}

command however is never printed out. Shouldn't this be working?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since you edited the question and clarified that you are using a JTextField I will reorder the answer:

You don't see any output because the action command has not been set so there is nothing to display.

Try using the following in your ActionListener:

JTextField textField = (JTextField)e.getSource();
System.out.println( textField.getText() );

Of course this will only display something if the user typed something into the text field. The point is you will only see output if there is something to display. You can always verify that a piece of code is being executed by displaying a hard coded string.

However, if you question was about a JTextArea, then default Action for using the Enter key on a JTextArea is to insert a newline in the text area.

If you want to invoke an action then you need to replace the default Action:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextAreaEnter extends JPanel
{
    private JTextArea message = new JTextArea(5, 20);
    private JTextArea display = new JTextArea(5, 20);

    public TextAreaEnter()
    {
        display.setEditable( false );

        add( new JScrollPane(message) );
        add( new JScrollPane(display) );

        Action enter = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                display.append( message.getText() + "
" );
                message.setText("");
            }
        };

        message.getActionMap().put("insert-break", enter);
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("TextAreaEnter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TextAreaEnter() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

See: Key Bindings for a list of the default bindings for each Swing component.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...