重点组件参考
我正在研究一个简单的基于java swing的应用程序。 如何获取和设置表单当前聚焦的textfield / textarea的文本?
我知道如何确定哪个组件具有焦点,但我无法弄清楚如何获取组件的选定文本。 我使用getFocusOwner()但它返回一个Component,因此不实现方法getSelectedText()。 我是否需要进行类型转换?
是:
Component focusOwner = ... if(focusOwner instanceof JTextComponent) { // a textfield or textarea is focused JTextComponent textComp = (JTextComponent) focusOwner; String s = textComp.getText(); }
我使用getFocusOwner()但它返回一个Component,因此不实现方法getSelectedText()。 我是否需要进行类型转换?
其中一种方法是测试instanceof
@ user1235867 +1
另一个也是最有效的是帮助J/Components
数组,并且可以简单地确定哪个J/Component
在Window
有当前的KeyboardFocusManager#getFocusOwner()
使用FocusOwner
切换两个顶级容器之间的通知是非常异步的,需要将事件包装到invokeLater
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(); } }); } }
- 可以利用Android API在呼叫期间修改呼叫者的语音吗?
- Android:通过RTP(G.711 PCMU)将.wav发送到SIP-Phone非常嘈杂,基于SipDroid / MjSIP的噼啪声
- 如何在不重新启动速度的情况下编辑velocimacro?
- 您是否在Java中使用默认(包)可见性?
- 如何通过代理通过ant下载文件
- 用于Twitter Bootstrap等前端技术的后端技术
- 如何使用Jsoup填写表单?
- NoSuchMethodError:org.springframework.beans.factory.xml.XmlReaderContext.getEnvironment()Lorg / springframework / core / env / Environment; 随着zkoss
- 禁用PDFBox上的日志记录
- 写阿拉伯语和英语之间的文本文件混合
- 将hex字符串解析为int值时出现意外的NumberFormatException