提问者:小点点

Java摆动:如何停止不必要的移位标签击键动作


当我在JPanel中有一个JTextField并且它具有焦点时,按“tab”不会做任何事情…但是按“shift-tab”会导致焦点丢失(Focus usEvent. getOppositeComponent()为null)。

如果JPanel上有其他可聚焦组件(或者更确切地说,在“聚焦循环根”下),则不会发生这种情况:相反,它们将焦点放在shift-tab上。

在下面的SSCCE中,我演示了这一点…每次在搜索框中按返回时,您都会向JTable添加一行,这会使其变得可聚焦。您还可以取消注释使JRadioButton不可聚焦的行。

我也查看了InputMaps,看看shift-tab是否以某种方式参与其中…一点也不。

我还尝试了使用Focus usTraversalPolicy进行实验,看看我是否能理解这个问题。没有乐趣。

我的目标:当焦点循环根的范围内有一个单一的可聚焦组件时,停止“移位标签”导致焦点丢失(焦点消失)。

后来

解决方法是添加行

if( oppComp == null ){ impl.searchBox.requestFocus(); }

在搜索框的焦点侦听器的焦点丢失方法的末尾…但对我来说,这只是一个解决方法…1)它不能通过理解焦点遍历机制来解决问题;2)可能会有一段时间你需要从搜索框中丢失焦点…

import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

class BackTabProb {

    JFrame mainFrame;
    JTextField searchBox;
    JTable resultsTable;

    public static void print(String msg) {
        System.out.println(msg);
    }

    public static void main(String[] a_args) throws InvocationTargetException, InterruptedException {
        final BackTabProb impl = new BackTabProb();

        class Show implements Runnable {
            public void run() {
                impl.mainFrame = new JFrame("Back Tab problem");
                impl.mainFrame.addWindowListener(new WindowAdapter() {
                    public void windowClosing(WindowEvent windowEvent) {
                        impl.mainFrame.dispose();
                    }

                });

                impl.resultsTable = new JTable();
                impl.resultsTable.setFocusable(false);
                Vector<Object> dataVector = new Vector<Object>();
                Vector<Object> colIdentifiers = new Vector<Object>(Arrays.asList(new String[] { "ONE", "TWO" }));
                ((DefaultTableModel) impl.resultsTable.getModel()).setDataVector(dataVector, colIdentifiers);
                JScrollPane jsp = new JScrollPane(impl.resultsTable);
                JPanel northPanel = new JPanel();
                northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.X_AXIS));
                impl.searchBox = new JTextField("Enter search text", 10);
                JLabel label = new JLabel("Search:");
                label.setLabelFor(impl.searchBox);
                northPanel.add(label);
                northPanel.add(impl.searchBox);
                ButtonGroup buttonGroup = new ButtonGroup();
                ArrayList<JRadioButton> indexButtons = new ArrayList<JRadioButton>();
                for (int i = 0; i < 2; i++) {
                    JRadioButton indexButton = new JRadioButton(i == 0 ? "Stemmer" : "Simple");

                    // commenting this out means back-tabbing from search box does not result
                    // in focus going "nowhere"
                    indexButton.setFocusable(false);

                    buttonGroup.add(indexButton);
                    northPanel.add(indexButton);

                }
                impl.mainFrame.getContentPane().setLayout(new BorderLayout());
                impl.mainFrame.getContentPane().add(northPanel, BorderLayout.NORTH);
                impl.mainFrame.getContentPane().add(jsp, BorderLayout.CENTER);
                impl.mainFrame.pack();
                impl.mainFrame.setVisible(true);
                print("=== visible");
            }
        }

        EventQueue.invokeAndWait(new Show());

        class AddMore implements Runnable {
            public void run() {
                impl.mainFrame.setFocusTraversalPolicyProvider(true);
                class SearchBoxFocusListener implements FocusListener {
                    public void focusGained(FocusEvent focusEvent) {
                        print("=== search box got focus");
                        impl.searchBox.selectAll();

                    }

                    public void focusLost(FocusEvent focusEvent) {
                        Component oppComp = focusEvent.getOppositeComponent();
                        print(String.format("=== search box lost focus to %s",
                                oppComp == null ? "nowhere" : oppComp.getClass()));
                    }
                }
                impl.searchBox.addFocusListener(new SearchBoxFocusListener());

                class SearchBoxActionListener implements ActionListener {
                    public void actionPerformed( ActionEvent actionEvent ){
                        if( actionEvent.getSource() != null ){
                            ((DefaultTableModel)impl.resultsTable.getModel()).insertRow( 0, new Object[]{ "blip", "blap" });

                            // as soon as the table has at least one row it is set to "focusable"

                            // commenting this out means back-tabbing from search box results
                            // in focus going "nowhere"
                            impl.resultsTable.setFocusable( true );
                        }
                    }
                }
                impl.searchBox.addActionListener( new SearchBoxActionListener() );

                ActionMap am = impl.searchBox.getActionMap();
                print("=== ActionMap");
                for (Object key : am.allKeys()) {
                    print(String.format("  === action key %s", key));
                }
                for (int i = 0; i < 3; i++) {
                    print(String.format("=== InputMap type %d", i));
                    InputMap im = impl.searchBox.getInputMap(i);
                    KeyStroke[] allKeys = im.allKeys();
                    if (allKeys != null) {
                        for (KeyStroke ks : allKeys) {
                            print(String.format("  === keystroke %s object %s", ks, im.get(ks)));
                        }
                    }
                }

                // various experiments with FocusTraversalPolicy... NB LayoutTraversalPolicy
                // is what the framework uses here by default

                class MainFrameFocusTraversalPolicy extends LayoutTraversalPolicy {
                    public Component getComponentAfter(Container arg0, Component arg1) {
                        Component comp = super.getComponentAfter(arg0, arg1);
                        print(String.format("=== comp after %s", comp == null ? "Null" : comp.getClass()));
                        return comp;
                    }

                    public Component getComponentBefore(Container arg0, Component arg1) {
                        Component comp = super.getComponentBefore(arg0, arg1);
                        print(String.format("=== comp before %s", comp == null ? "Null" : comp.getClass()));
                        return comp;
                    }

                    public Component getDefaultComponent(Container arg0) {
                        Component comp = super.getDefaultComponent(arg0);
                        print(String.format("=== default comp %s", comp == null ? "Null" : comp.getClass()));
                        return comp;
                    }

                    public Component getFirstComponent(Container arg0) {
                        Component comp = super.getFirstComponent(arg0);
                        print(String.format("=== first comp %s", comp == null ? "Null" : comp.getClass()));

                        return comp;
                        // return impl.searchBox;
                    }

                    public Component getLastComponent(Container arg0) {
                        Component comp = super.getLastComponent(arg0);
                        print(String.format("=== last comp %s", comp == null ? "Null" : comp.getClass()));
                        return comp;
                    }

                    protected boolean accept(Component comp) {
                        boolean accept = super.accept(comp);
                        print(String.format("=== accept %s? %s", comp == null ? "Null" : comp.getClass(), accept));
                        return accept;
                    }
                }
                impl.mainFrame.setFocusTraversalPolicy(new MainFrameFocusTraversalPolicy());
            }
        }
        EventQueue.invokeAndWait(new AddMore());

    }

}

共2个答案

匿名用户

也许你可以使用Container#setFocus usTraversalKeys(…)方法:

Set<AWTKeyStroke> backwardKeys = Collections.emptySet();
//alone JTextField(pointed out by @mKorbel): impl.mainFrame.setFocusTraversalKeys(
impl.searchBox.setFocusTraversalKeys(
    KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backwardKeys);

匿名用户

这仍然属于我的变通方法…但是它比我在“稍后”下给出的变通方法更有用

if( oppComp == null ){
    final Component srcComp = (Component)focusEvent.getSource();
    FocusTraversalPolicy ftp = impl.mainFrame.getFocusTraversalPolicy();
    Component lastComp = ftp.getLastComponent( impl.mainFrame );
    Component beforeComp = ftp.getComponentBefore( impl.mainFrame, srcComp );

    if( lastComp == beforeComp ){
        EventQueue.invokeLater( new Runnable(){
            public void run() {
                if( impl.mainFrame.isFocused()){
                    srcComp.requestFocus();
                };
            }});
    }
}

事实上,这就是单个可聚焦组件所面临的情况:在Shift-Tab上,焦点遍历策略发现“之前”的comp与“最后”的comp相同。在这些情况下,组件焦点(与窗口焦点相反)似乎消失了。奇怪的是,在Tab(向前遍历)上,当“之后”comp与“第一个”comp相同时,它没有。这是bug吗?

无论如何,在确定了这一点之后,您必须异步检查窗口的焦点…以便允许焦点转移到另一个窗口。导致Shift-Tab上轻微闪烁(嘘!)。