重点组件参考

时间:2012-06-26 08:23:54

标签: java swing focus components textfield

我正在研究一个简单的基于java swing的应用程序。如何获取和设置表单当前聚焦的textfield / textarea的文本?

我知道如何确定哪个组件具有焦点,但我无法弄清楚如何获取组件的选定文本。我使用getFocusOwner()但它返回一个Component,因此不实现方法getSelectedText()。我是否需要进行类型转换?

3 个答案:

答案 0 :(得分:3)

是:

Component focusOwner = ...
if(focusOwner instanceof JTextComponent) { // a textfield or textarea is focused
    JTextComponent textComp = (JTextComponent) focusOwner;
    String s = textComp.getText();
}

答案 1 :(得分:3)

根据您的确切上下文,您可能会考虑使用自定义TextAction:其方法getTextComponent(ActionEvent)返回最近的焦点文本组件。代码段:

    Action logSelected = new TextAction("log selected") {

        @Override
        public void actionPerformed(ActionEvent e) {
            JTextComponent text = getTextComponent(e);
            System.out.println("selected: " + text.getSelectedText());
        }

    };

    JComponent content = new JPanel();
    content.add(new JTextField("sometext", 20));
    content.add(new JTextField("other content", 20));
    content.add(new JCheckBox("just some focusable comp"));
    content.add(new JButton(logSelected));

答案 2 :(得分:2)

  

我使用getFocusOwner()但它返回一个Component,因此没有实现方法getSelectedText()。我是否需要进行类型转换?

其中一种方法是测试instanceof @ user1235867 +1

另一个也是最有效的是帮助J/Components的数组,并且可以简单地确定J/Component

KeyboardFocusManager#getFocusOwner()中哪个WindowFocusOwner

通知切换invokeLater两个Top-Level Containers之间非常异步,需要将事件包装到import javax.swing.*; import java.awt.*; import java.awt.event.*; //http://www.coderanch.com/t/342205/GUI/java/Tab-order-swing-components public class Testing { private static final long serialVersionUID = 1L; private Component[] focusList; private int focusNumber = 0; private JFrame frame; public Testing() { JTextField tf1 = new JTextField(5); JTextField tf2 = new JTextField(5); JTextField tf3 = new JTextField(5); JButton b1 = new JButton("B1"); JButton b2 = new JButton("B2"); tf2.setEnabled(false); focusList = new Component[]{tf1, b1, tf2, b2, tf3}; JPanel panel = new JPanel(new GridLayout(5, 1)); panel.add(tf1); panel.add(b1); panel.add(tf2); panel.add(b2); panel.add(tf3); frame = new JFrame(); frame.setFocusTraversalPolicy(new MyFocusTraversalPolicy()); frame.add(panel); frame.pack(); frame.setLocation(150, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() { public boolean dispatchKeyEvent(KeyEvent ke) { if (ke.getID() == KeyEvent.KEY_PRESSED) { if (ke.getKeyCode() == KeyEvent.VK_TAB) { Component comp = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); if (comp.isEnabled() == false) { if (ke.isShiftDown()) { KeyboardFocusManager.getCurrentKeyboardFocusManager().focusPreviousComponent(); } else { KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent(); } } } } return false; } }); } private class MyFocusTraversalPolicy extends FocusTraversalPolicy { public Component getComponentAfter(Container focusCycleRoot, Component aComponent) { focusNumber = (focusNumber + 1) % focusList.length; return focusList[focusNumber]; } public Component getComponentBefore(Container focusCycleRoot, Component aComponent) { focusNumber = (focusList.length + focusNumber - 1) % focusList.length; return focusList[focusNumber]; } public Component getDefaultComponent(Container focusCycleRoot) { return focusList[0]; } public Component getLastComponent(Container focusCycleRoot) { return focusList[focusList.length - 1]; } public Component getFirstComponent(Container focusCycleRoot) { return focusList[0]; } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { Testing testing = new Testing(); } }); } }

{{1}}