2010-11-19 58 views
0

我有一個JToolTip附加到JTextField的onclick事件。JToolTip對齊問題

我希望JToolTip彈出與JTextField的右邊緣對齊,而不是它的左邊緣,因爲它默認是這樣。

有什麼方法可以設置它嗎?

回答

0

覆蓋您點擊組件的getTooltipLocation(...)

public static void main(String... args) throws IOException { 
    final JFrame frame = new JFrame("Test"); 

    JTextField field = new JTextField() { 
     @Override 
     public Point getToolTipLocation(MouseEvent event) { 
      Rectangle position = frame.getBounds(); 
      return new Point(position.x, position.y); 
     } 
    }; 

    field.setToolTipText("test"); 
    frame.add(field); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(400, 300); 
    frame.setVisible(true); 
}