Integrating Grid With Components: Tooltip
Updated: Dec 31, 2007
Views: 814
Description: This tutorial will show you how integrate the Grid with the Tooltip Component so that the Tooltip shows an grid item's description from the xml document as they are rolled over.
Components Used in this Tutorial: Grid AS3.0 and Tooltip AS3.0
The Completed Project:
Complete Code:
Set-Up and Add Content Using through XML
Before you begin make sure that you have successfully installed the component inspector have been able to load content into the Grid component using XML. Also make sure that you give your Grid an instance name of myGrid.
Setting up Tooltip
After you have the Tooltip component installed, drag an instance of the Tooltip onto the stage from the components panel (Window > Components). Make sure that you give your Tooltip an instance name of myTooltip.
Plan for ActionScript
Now, we just need to write a little ActionScript to connect the Tooltip to the items in the Grid. We will add the ActionScript in the frame in actions layer in the time.
Event listeners for items in the Grid can only be loaded through ActionScript after the component has initialized. We do this by creating an event listener and calling a function in which we will use to add our content.
Roll Over Function
Next, we add the function that roll over event listener calls, which shows our Tooltip and updates the text to our target’s description in the XML document:
Roll Out Function
Now, we just need to write the function that our roll out event listener calls, which hides our Tooltip.
Hide the Tooltip Initially
Lastly we just need to set a property of the Tooltip so that it hides initially:
The Completed Project:
Complete Code:
import flash.events.MouseEvent;
import com.afcomponents.events.GridEvent;
myTooltip.autoShow = false;
//After XML Loads add events for All Items in Grid
function loadComplete(event:Event){
myGrid.addGenericItemEventListener(MouseEvent.ROLL_OVER, showTooltip);
myGrid.addEventListener(MouseEvent.ROLL_OUT, hideTooltip);
}
myGrid.addEventListener(GridEvent.XML_LOAD_COMPLETE, loadComplete);
//Shows the Tooltip
function showTooltip(event:MouseEvent){
myTooltip.show();
myTooltip.content = event.target.description;
}
//Hides the Tooltip
function hideTooltip(event:MouseEvent){
myTooltip.hide();
}
import com.afcomponents.events.GridEvent;
myTooltip.autoShow = false;
//After XML Loads add events for All Items in Grid
function loadComplete(event:Event){
myGrid.addGenericItemEventListener(MouseEvent.ROLL_OVER, showTooltip);
myGrid.addEventListener(MouseEvent.ROLL_OUT, hideTooltip);
}
myGrid.addEventListener(GridEvent.XML_LOAD_COMPLETE, loadComplete);
//Shows the Tooltip
function showTooltip(event:MouseEvent){
myTooltip.show();
myTooltip.content = event.target.description;
}
//Hides the Tooltip
function hideTooltip(event:MouseEvent){
myTooltip.hide();
}
Set-Up and Add Content Using through XML
Before you begin make sure that you have successfully installed the component inspector have been able to load content into the Grid component using XML. Also make sure that you give your Grid an instance name of myGrid.
Setting up Tooltip
After you have the Tooltip component installed, drag an instance of the Tooltip onto the stage from the components panel (Window > Components). Make sure that you give your Tooltip an instance name of myTooltip.
Plan for ActionScript
Now, we just need to write a little ActionScript to connect the Tooltip to the items in the Grid. We will add the ActionScript in the frame in actions layer in the time.
- First, we need to add event listeners to the Grid to know when the user rolls the mouse over and off an item in the Grid.
- The roll over event listener will call a function that shows and updates the text in the Tooltip.
- The roll out event listener calls a function that hides the Tooltip.
- Lastly, we need to set the initial state of the Tooltip to hidden.
Event listeners for items in the Grid can only be loaded through ActionScript after the component has initialized. We do this by creating an event listener and calling a function in which we will use to add our content.
import flash.events.MouseEvent;
import com.afcomponents.events.GridEvent;
function onGridInit(event:GridEvent) {
myGrid.addGenericItemEventListener(MouseEvent.ROLL_OVER, showTooltip);
myGrid.addEventListener(MouseEvent.ROLL_OUT, hideTooltip);
}
myGrid.addEventListener(GridEvent.INITIALIZE, onGridInit);
import com.afcomponents.events.GridEvent;
function onGridInit(event:GridEvent) {
myGrid.addGenericItemEventListener(MouseEvent.ROLL_OVER, showTooltip);
myGrid.addEventListener(MouseEvent.ROLL_OUT, hideTooltip);
}
myGrid.addEventListener(GridEvent.INITIALIZE, onGridInit);
Roll Over Function
Next, we add the function that roll over event listener calls, which shows our Tooltip and updates the text to our target’s description in the XML document:
//Shows the Tooltip
function showTooltip(event:MouseEvent){
myTooltip.show();
myTooltip.content = event.target.description;
}
function showTooltip(event:MouseEvent){
myTooltip.show();
myTooltip.content = event.target.description;
}
Roll Out Function
Now, we just need to write the function that our roll out event listener calls, which hides our Tooltip.
//Hides the Tooltip
function hideTooltip(event:MouseEvent){
myTooltip.hide();
}
function hideTooltip(event:MouseEvent){
myTooltip.hide();
}
Hide the Tooltip Initially
Lastly we just need to set a property of the Tooltip so that it hides initially:
myTooltip.autoShow = false;