====== MessageBox and tooltips ====== {{tag>aswing tutorial}} This tutorial will use the MessageBox tutorial example to show you how to add tooltips to your components. We first set the tooltip for the okBtn instance by simply calling the setToolTipText method. okBtn.setToolTipText("Click Yes \nThis is correct answer."); If we run our example now you can see a tooltip generated when you do a mouse rollover on the okBtn. Now lets create a JToolTip instance for the cancelBtn and create a action listener to call a method when the button is clicked. tooltip = new JToolTip(); tooltip.setComponent(cancelBtn); tooltip.setTipText("Click No"); cancelBtn.addActionListener(changeToolTip, this); We will change the tooltip text in this method. private function changeToolTip():Void{ tooltip.setTipText("You Clicked No " +count++); } Make sure you added the tooltip instance and the count variable to the class. private var tooltip:JToolTip; private var count:Number; Compile and run the example. Everytime you click the cancelBtn the count variable will be incremented and the value will change on the tooltip. {{ :messageboxtt.swf?550x400 }} MessageBox.as ------------------------- /** * @author firdosh */ import org.aswing. *; class MessageBox extends JFrame { private var buttonsPanel : JPanel; private var labelPanel : JPanel; private var okBtn : JButton; private var cancelBtn : JButton; private var title : String; private var message : String; private var messageDisplay : JLabel; private var icon : MCIcon; private var tooltip : JToolTip; private var count : Number; public function MessageBox (title : String , message : String) { super (_root, true); this.title = title; this.message = message; count = 1; buttonsPanel = new JPanel (); okBtn = new JButton ("Yes"); cancelBtn = new JButton ("No"); okBtn.setPreferredSize (75, 22); cancelBtn.setPreferredSize (75, 22); okBtn.setToolTipText ("Click Yes \nThis is correct answer."); buttonsPanel.setLayout (new FlowLayout (FlowLayout.RIGHT, 20, 8)); buttonsPanel.append (okBtn); buttonsPanel.append (cancelBtn); labelPanel = new JPanel (); messageDisplay = new JLabel (message); labelPanel.setLayout (new FlowLayout (FlowLayout.CENTER)); labelPanel.append (messageDisplay); tooltip = new JToolTip (); tooltip.setComponent (cancelBtn); tooltip.setTipText ("Click No"); cancelBtn.addActionListener (changeToolTip, this); getContentPane ().append (labelPanel, BorderLayout.CENTER); getContentPane ().append (buttonsPanel, BorderLayout.SOUTH); setTitle (title); } private function changeToolTip () : Void { tooltip.setTipText ("You Clicked No " + count ++); } public function setDisplayIcon (icon : Icon) : Void { messageDisplay.setIcon (icon); } public static function main (Void) : Void { var myWindow : MessageBox = new MessageBox ("Information", "ASWing ROCKS!!.\nDon`t you agree ;)."); var info : MCIcon = new MCIcon ("INFO", 51, 43); myWindow.setDisplayIcon (info); myWindow.setLocation (50, 50); myWindow.setSize (300, 150); myWindow.setResizable (false); myWindow.show (); } }