Tuesday, April 7, 2009

Wednesday, April 8: Tooltips and rollovers and Togglers, O my!

OK, now for the second phase of today's adventure: tooltips! This is actually pretty simple to do: to the first JButton you made, add this line of code:

Smile.setToolTipText("Always look on the bright side of life!");



then re-compile and run the program. You should get a message that pops up each time you leave the mouse over any particular JButton.



Speaking of rollovers, if you add this line of code:

Utool.setRollover(true);

after this liner of code

Utool.setFloatable(true);



you will get another effect showing which JButton you are about to select





OK, now for our other component, the JToggleButton. This component acts like a kind of light switch: it has an on and off position, and you can make things happen in your program based on that. It can be added to your JToolbar like any other JButton, and it can make use of another type of Listener, an "ItemListener" (This type of listener can also be used by radio buttons, but we'll get to that some other time) So, first things first, lets add an ItemListener to your program:

add this , ItemListener
after this implements ActionListener
put 'em both on the same line


then, declare this:
JToggleButton JTB;


and then add it into memory like so
JTB = new JToggleButton(new ImageIcon("gearHead.jpg") );
JTB.setToolTipText("Gear Up, Mates!");
JTB.addItemListener(this);
Utool.add(JTB);




Finally, you need to add an ItemStateChanged method to your program. You can probably do this just below your actionPerformed method, that's where I did it:


public void itemStateChanged(ItemEvent evt)
{
if (evt.getStateChange() == ItemEvent.SELECTED)
{
Utool.setFloatable(false);
}
else
{
Utool.setFloatable(true);
}
}//end itemStateChanged




Cheers,

Uncle Paulie

No comments:

Post a Comment