2
我想监视文本到JTextArea的选择。我不知道什么事件选择了文本触发器。哪个事件在Java JTextArea中选择了文本触发器?
我只是想在启用JTextArea之外的某些文本时启用某些菜单项,如复制和剪切选项到菜单中。我该怎么监控?
我想监视文本到JTextArea的选择。我不知道什么事件选择了文本触发器。哪个事件在Java JTextArea中选择了文本触发器?
我只是想在启用JTextArea之外的某些文本时启用某些菜单项,如复制和剪切选项到菜单中。我该怎么监控?
我不知道任何“选择侦听器”文本组件(尽管它们可能是有用的),但你可以使用一个CaretListener
监视更改插入符位置,并检查选择状态...
public class TestSelectionMonitor {
public static void main(String[] args) {
new TestSelectionMonitor();
}
public TestSelectionMonitor() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final JTextArea ta = new JTextArea();
ta.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
int length = ta.getSelectionEnd() - ta.getSelectionStart();
System.out.println(length);
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(ta));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
YUP ...正常工作... 非常感谢好友...... !!! – 2013-02-28 22:55:44