Java Swing:如何阻止不必要的shift-tab按键动作

时间:2014-07-15 17:27:38

标签: java swing focus

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

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

在下面的SSCCE中,我演示了这一点......每次在搜索框中按Return键都会向JTable添加一行,这会使其变为可聚焦。您还可以取消注释使JRadioButtons无法对焦的行。

我还看了一下InputMaps,看看shift-tab是否以某种方式参与其中......根本不是。

我也尝试过使用FocusTraversalPolicy来看看我是否能理解这个问题。没有快乐。

我的目标:当焦点循环根目录中有一个可聚焦组件时,停止“shift-tab”导致焦点丢失(焦点消失)。

解决方法是添加行

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

在搜索框的FocusListener的focusLost方法的末尾...但对我来说这只是一种解决方法... 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 个答案:

答案 0 :(得分:3)

也许您可以使用Container#setFocusTraversalKeys(...)方法:

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

答案 1 :(得分:0)

这仍然属于我的解决方案...但它比我在#34;后来&#34;

下提供的解决方案更有用。
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上,焦点遍历策略然后发现&#34;之前&#34; comp与&#34; last&#34;相同可比。在这些圈子下,焦点(与窗口焦点相对)似乎消失了。奇怪的是,在Tab上(横向前进),当&#34;之后&#34; comp与&#34; first&#34;相同comp,它没有。这是一个错误吗?

无论如何,确定了这一点后,你必须异步检查窗口的焦点......这样才能让焦点转到另一个窗口。在Shift-Tab上略微闪烁(嘘!)。