如何将键绑定附加到JSplitPane分频器的向上和向下箭头?

时间:2013-11-01 14:28:23

标签: java swing key-bindings jsplitpane

我有一个将oneTouchExpandable设置为true的JSplitPane。

JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        splitPane.setDividerSize(10);
        splitPane.setOneTouchExpandable(true);

问题是我不知道如何将键绑定附加到JSplitPane的分隔符上的向上和向下箭头。对于向上箭头,我想要Ctrl + U和向下 - Ctrl + D.

谢谢!

2 个答案:

答案 0 :(得分:3)

OneTouchExpandable显示的箭头按钮的实现是UI标签,并且将不必要地额外工作来绑定它们。您可以在JSplitPane上轻松使用密钥绑定来使用JSplitPane控制setDividerLocation(int)分隔符位置。增加Ctrl + U并减少Ctrl + D。例如:

    Action incrDividerLoc = new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        JSplitPane srcSplitPan = (JSplitPane) e.getSource();
        (srcSplitPan).setDividerLocation(srcSplitPan.getDividerLocation()+10);
     }
   };

  Action decrDividerLoc = new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        JSplitPane srcSplitPan = (JSplitPane) e.getSource();
        (srcSplitPan).setDividerLocation(srcSplitPan.getDividerLocation()-10);
    }
   };

    jSplitPane1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_U, KeyEvent.CTRL_DOWN_MASK),
                                "increaseDivider");
    jSplitPane1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_DOWN_MASK),
                                "decreaseDivider");
    jSplitPane1.getActionMap().put("increaseDivider", incrDividerLoc);
    jSplitPane1.getActionMap().put("decreaseDivider", decrDividerLoc);

注意:方法传递给setDividerLocation(int)的小于0的值意味着应将分隔符重置为试图遵循左/顶部组件的首选大小的值。在通知听众之后,通过setLastDividerLocation更新最后一个分频器位置。

答案 1 :(得分:3)

  

问题是我不知道如何将键绑定附加到JSplitPane的分隔符上的向上和向下箭头。

通常您会尝试访问按钮的操作。在许多情况下,组件已经定义了可以使用的Action。有关JSplitPane的默认绑定列表,请参阅Key Bindings。不幸的是,没有动作来支持单触式点击选项。

所以我们需要直接从UI访问按钮:

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

public class SplitPaneDividerAction extends AbstractAction
{
    private boolean leading;

    public SplitPaneDividerAction(boolean leading)
    {
        this.leading = leading;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        JSplitPane splitPane = (JSplitPane)e.getSource();
        BasicSplitPaneUI ui = (BasicSplitPaneUI)splitPane.getUI();
        BasicSplitPaneDivider divider = ui.getDivider();

        if (leading)
            ((JButton)divider.getComponent(0)).doClick();
        else
            ((JButton)divider.getComponent(1)).doClick();
    }

    private static void createAndShowUI()
    {
        JPanel leading = new JPanel();
        leading.setPreferredSize( new Dimension(200, 100) );
        leading.setBackground( Color.BLUE );
        leading.setFocusable(true);

        JPanel trailing = new JPanel();
        trailing.setPreferredSize( new Dimension(200, 100) );
        trailing.setBackground( Color.RED );
        trailing.setFocusable(true);

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leading, trailing);
        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerLocation(100);

        InputMap im = splitPane.getInputMap(JSplitPane.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        ActionMap am = splitPane.getActionMap();
        im.put(KeyStroke.getKeyStroke("control U"), "leading");
        im.put(KeyStroke.getKeyStroke("control D"), "trailing");
        am.put("leading", new SplitPaneDividerAction(true));
        am.put("trailing", new SplitPaneDividerAction(false));

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( splitPane );
        frame.setSize(200, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

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

当然,只有当您的LAF从BasicSplitPaneUI延伸时,此方法才有效。