获取当前显示的工具提示 [英] Get currently displaying tooltip
问题描述
是否可以通过某种方式将当前显示的工具提示作为字符串复制到剪贴板,而无需进行复杂的XY坐标计算即可映射到工具提示文本区域?这在带有一定角度显示的工具提示的图表上尤其具有挑战性,并且仅在显示时才能捕获。例如,要获取 ctl-c 来将显示的工具提示复制到剪贴板:
PlotThisDaysData扩展了JFrame实现... KeyListener {
@Override
public void keyTyped(KeyEvent e){
char typed = e.getKeyChar();
if(输入== KeyEvent.VK_C)/ * VK_C?* / {
字符串tooltipStr = myChart。???(); //<<<<<<<获得显示工具提示<<<<
StringSelection选择= new StringSelection(tooltipStr);
剪贴板剪贴板= Toolkit.getDefaultToolkit()。getSystemClipboard();
Clipboard.setContents(selection,selection);
}
}
也许在显示工具提示时发生了一些事件,所以我可以存储String指针并在输入 ctl-c 时使用吗?
。要在鼠标移动时将当前显示的工具提示复制到剪贴板,
-
添加
ChartMouseListener
到图表面板,如此处所示。 -
当听众看到所需的
ChartEntity
时,向ChartPanel
询问相关文本并复制它到剪贴板。Toolkit toolkit = Toolkit.getDefaultToolkit();
剪贴板剪贴板= toolkit.getSystemClipboard();
…
@Override
public void chartMouseMoved(ChartMouseEvent cme){
…
字符串t = chartPanel.getToolTipText(cme.getTrigger());
Clipboard.setContents(new StringSelection(t),null);
}
类似的方法可以用于密钥绑定,如此处。使用图表面板的 getMousePosition()
来构造必需的 MouseEvent
触发器。
-
获取图表面板的
InputMap
,ActionMap
和InputMap im = chartPanel.getInputMap();
ActionMap am = chartPanel.getActionMap();
int mask = Toolkit.getDefaultToolkit()。getMenuShortcutKeyMask();
-
将所需的
KeyStroke
放入图表面板的InputMap
im.put(KeyStroke.getKeyStroke(KeyEvent .VK_C,掩码), copytip);
-
将相应的
Action
放入图表面板的ActionMap
am.put( copytip,new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e){
Point p = chartPanel.getMousePosition();
String t = chartPanel.getToolTipText(new MouseEvent(chartPanel) ,
0,System.currentTimeMillis(),0,px,py,0,false));
剪贴板.setContents(new StringSelection(t),null);
}
});
避免 KeyListener
,因为它是需要键盘焦点。
Is there some way to copy the currently displayed tooltip to the clipboard as a string without complex XY-coord calculation that maps to the tooltip text area? This is especially challenging on a chart with tooltip displayed at an angle, also to only capture if being displayed. For example to get ctl-c to copy the displaying tooltip to clipboard:
PlotThisDaysData extends JFrame implements ... KeyListener{
@Override
public void keyTyped( KeyEvent e ) {
char typed = e.getKeyChar();
if ( typed == KeyEvent.VK_C ) /*VK_C?*/ {
String tooltipStr = myChart.???(); // <<<<<<<<<<<<< get displaying tooltip <<<<
StringSelection selection = new StringSelection( tooltipStr );
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents( selection, selection );
}
}
Perhaps there is some event when a tooltip gets displayed so I can store a String pointer and use when ctl-c is entered?
Tooltips are displayed in response to mouse events received by the chart's enclosing ChartPanel
. To copy the currently displayed tooltip to the clipboard as the mouse moves,
Add a
ChartMouseListener
to the chart panel, as shown here.When your listener sees a desired
ChartEntity
, ask theChartPanel
for the relevant text and copy it to the clipboard.Toolkit toolkit = Toolkit.getDefaultToolkit(); Clipboard clipboard = toolkit.getSystemClipboard(); … @Override public void chartMouseMoved(ChartMouseEvent cme) { … String t = chartPanel.getToolTipText(cme.getTrigger()); clipboard.setContents(new StringSelection(t), null); }
A similar approach can be used in a key binding, as shown here. Use the chart panel's getMousePosition()
to construct the required MouseEvent
trigger.
Get the chart panel's
InputMap
,ActionMap
, and the platform's shortcut mask.InputMap im = chartPanel.getInputMap(); ActionMap am = chartPanel.getActionMap(); int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
Put the desired
KeyStroke
in the chart panel'sInputMap
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, mask), "copytip");
Put the corresponding
Action
in the chart panel'sActionMap
am.put("copytip", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { Point p = chartPanel.getMousePosition(); String t = chartPanel.getToolTipText(new MouseEvent(chartPanel, 0, System.currentTimeMillis(), 0, p.x, p.y, 0, false)); clipboard.setContents(new StringSelection(t), null); } });
Avoid KeyListener
, as it requires keyboard focus.
这篇关于获取当前显示的工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!