How can I make a JTextField fire it's ActionEvent when KeyEvent.VK_ENTER is redispatched to it?

本秂侑毒 提交于 2019-12-24 03:31:59

问题


I am playing around with the KeyboardFocusManager and my own custom KeyEventDispatcher that redispathes all KeyEvents to one particular JTextField, regardless of focus within the JFrame. This works like a charm as far as textual input is concerned but I also want the JTextField to post its text to a JTextArea when a KeyEvent.VK_ENTER is redispatched to it. For some reason it just won't do this. I've set an actionListener on the JTextField which will be fired on if I have the cursor in the text field and press ENTER, however it's not fired on if the ENTER event comes from a KeyboardFocusManager.redispatchEvent(keyEvent).

I've also tried redispathing an ActionEvent rather than the unchanged KeyEvent in the case ENTER is pressed, but to no avail :( You would think that dispathing an ActionEvent to a component would fire it's ActionListeners but nope.

Can someone explain why this is? And maybe propose a neat way around it?

SSCCE:

package viewlayer.guiutil.focus;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Runnable;import java.lang.String;
import java.util.Date;

public class Test extends JFrame
{
    private JTextField m_chatInput;
    private JTextArea m_textArea;

    public static void main(String... args)
    {
        Test test1 = new Test();
        test1.run(test1);
    }

    public void run(final Test test)
    {
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.setSize(250, 400);

        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new MyKeyEventDispatcher());

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                test.init();
            }
        });

        test.setVisible(true);
    }

    public void init()
    {
        JPanel panel = new JPanel (new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2,5,1,1);
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.gridwidth = GridBagConstraints.REMAINDER;

        JLabel chatLabel = new JLabel("Chat input field:");
        panel.add(chatLabel,gbc);

        m_chatInput = new JTextField(15);
        m_chatInput.setActionCommand(MyActionListener.ACTION_PERFORMED);
        m_chatInput.addActionListener(new MyActionListener());
        panel.add(m_chatInput,gbc);

        JTextField chatInput = new JTextField(15);
        panel.add(chatInput,gbc);

        JLabel text = new JLabel("chat history:");
        panel.add(text,gbc);

        m_textArea = new JTextArea(5, 15);
        m_textArea.setFocusable(false);
        panel.add(m_textArea,gbc);

        JButton postButton = new JButton("Post");
        postButton.setActionCommand(MyActionListener.ACTION_PERFORMED);
        postButton.addActionListener(new MyActionListener());
        panel.add(postButton,gbc);
        gbc.weighty = 1.0;
        gbc.anchor = gbc.NORTHWEST;

        setLayout(new FlowLayout(FlowLayout.LEFT));
        add(panel);
    }

    private class MyKeyEventDispatcher implements KeyEventDispatcher
    {
        public boolean dispatchKeyEvent(KeyEvent keyEvent)
        {
            KeyboardFocusManager.getCurrentKeyboardFocusManager().redispatchEvent(m_chatInput, keyEvent);

            return false;
        }
    }

    private class MyActionListener implements ActionListener
    {
        private static final String ACTION_PERFORMED = "ACTION_PERFORMED";

        public void actionPerformed(ActionEvent actionEvent)
        {
            if(actionEvent.getActionCommand().equals(ACTION_PERFORMED))
            {
                Date date = new Date(System.currentTimeMillis());
                m_textArea.append(date.getHours() +":"+ date.getMinutes() +":"+ date.getSeconds() + " - " + m_chatInput.getText() + "\n");
                m_chatInput.setText("");
            }
        }
    }
}

回答1:


The text field code has a test to make sure the component has focus before invoking the listener code:

    public void actionPerformed(ActionEvent e) {
        JTextComponent target = getFocusedComponent();
        if (target instanceof JTextField) {
            JTextField field = (JTextField) target;
            field.postActionEvent();
        }
    }

However, you should be able to invoke the postActionEvent() method directly from your KeyEventDispatcher:

if (enter key)
   m_chatInput.postActonEvent();
else
   // redispatch the event



回答2:


You should remove this line:

m_chatInput.setFocusable(false);

And above the line:

Date date = new Date(System.currentTimeMillis());

you should insert:

requestFocusInWindow(m_chatInput);

Please post if this worked for you or if we have to tweak a little more.




回答3:


I think i found a work-around you can use to keep the focus on the chatInput:

requestFocusInWindow(m_chatInput);
KeyboardFocusManager focusManager =
KeyboardFocusManager.getCurrentKeyboardFocusManager();
focusManager.addPropertyChangeListener(
    new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) {
            String prop = e.getPropertyName();
            if ("focusOwner".equals(prop)) {
            requestFocusInWindow(m_chatInput);
            }
        }
    }
 );

I modified this based on some code found at:

http://download.oracle.com/javase/tutorial/uiswing/misc/focus.html



来源:https://stackoverflow.com/questions/5078653/how-can-i-make-a-jtextfield-fire-its-actionevent-when-keyevent-vk-enter-is-redi

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