![](/img/trans.png)
[英]How to capture trayicon.displayMessage() mouse click on the tooltip baloon
[英]How to show a tooltip on a mouse click
我有一個JTreeTable
並成功實現了MouseMotionListener
,只要鼠標位於其中一個單元格上,就會顯示工具提示。 但是,單擊單元格時,工具提示不會顯示。 我嘗試過幾樣的事情,比如在mouseClicked
和mouseReleased
事件上設置文本,但這不起作用。 我發現這個代碼 -
Action toolTipAction = treeTable.getActionMap().get("postTip");
if(toolTipAction != null){
ActionEvent postTip = new ActionEvent(treeTable,ActionEvent.ACTION_PERFORMED, "");
toolTipAction.actionPerformed(postTip);
}
在mouseReleased
方法中使用,它確實使tooltip popup
,但它在錯誤的位置。 所以接下來我嘗試覆蓋JTreeTable
上的getTooltipLocation
方法,這對於mouseMoved
事件很有效,但是沒有用上面的方法調用。 任何人都可以闡明如何做到這一點?
謝謝安迪
您可以使用以下方法顯示工具提示(稍有延遲)。 然后您可以覆蓋getToolTipLocation()方法,因為現在將生成MouseEvent:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ToolTipOnRelease extends JPanel
{
public ToolTipOnRelease()
{
JLabel label = new JLabel( "First Name:" );
add( label );
JTextField textField = new JTextField(15);
add( textField );
MouseListener ml = new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
JComponent component = (JComponent)e.getSource();
component.setToolTipText("Mouse released on: " + component.getClass().toString());
MouseEvent phantom = new MouseEvent(
component,
MouseEvent.MOUSE_MOVED,
System.currentTimeMillis(),
0,
0,
0,
0,
false);
ToolTipManager.sharedInstance().mouseMoved(phantom);
}
};
label.addMouseListener( ml );
textField.addMouseListener( ml );
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("ToolTipOnRelease");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new ToolTipOnRelease() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
org.apache.jorphan.gui.JTreeTable擴展javax.swing.JComponent javax.swing.JComponent #setToopTipText()不起作用? 我確實意識到你想使用Action而不是工具提示? 當多個UI操作需要共享時,我會使用Action。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.