Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to create buttons in my dataGrid in Flash. However, I am a begginer and I don't now how to make my function which generates button column take parameters which I need to use to each button. I need that every button has a specific Label and if I click on that button it copy googleURL to clipboard.

I don't know how to pass variables "buttonLabel" and "googleURL" to that function...

Here is the .fla part of code:

        googleURL = "www.someadress.com";
        buttonLabel = "someLabel";

        var btnCol:DataGridColumn = new DataGridColumn("buttonColumnTitle");

        btnCol.cellRenderer = ButtonCell;
        btnCol.editable = false;
        tabulka.addColumn(btnCol);

and here is ButtonCell class:

package 
{
import flash.desktop.Clipboard;
import fl.controls.Button;
import fl.controls.listClasses.ICellRenderer;
import fl.controls.listClasses.ListData;
import flash.events.Event;
import flash.events.MouseEvent;


public class ButtonCell extends Button implements ICellRenderer
{
    private var _listData:ListData;
    private var _data:Object;

    public var _buttonLabel:String;
    public var _googleURL:String;


    public function ButtonCell()
    {
        super();

        //_buttonLabel = buttonLabel;
        //_googleURL = googleURL;

        this.label = _buttonLabel;

        addEventListener(MouseEvent.CLICK, onButtonClick);
    }

    public function set data(d:Object):void
    {
        _data = d;
    }

    public function get data():Object
    {
        return _data;
    }

    public function get listData():ListData
    {
        return _listData;
    }

    public function set listData(value:ListData):void
    {
        _listData = value;
    }

    override public function get selected():Boolean
    {
        return _selected;
    }

    override public function set selected(value:Boolean):void
    {

    }

    public function onButtonClick(event:MouseEvent)
    {
        System.setClipboard(_googleURL);
    }
}
}
#######################################

whow, this work perfectly, that was the problem. Thank you so much!! However there is one more thing I am not sure about... When I want to add more buttons it only displays the last one... I am not sure what's wrong. This is sample sript which draws 5 items but buttons - only the last one...

import ButtonCell;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.controls.listClasses.CellRenderer;
import fl.controls.DataGrid;
import fl.data.DataProvider;

var googleURL:String = "www";
var buttonLabel:String = "bt";

var provide = new DataProvider;

var tabulka:DataGrid = new DataGrid();
tabulka.x = 50;
tabulka.y = 50;
addChild(tabulka);
tabulka.setSize(400, 300);
tabulka.columns = ["Parcela","Odkaz"];
tabulka.columns[0].width = 50;
tabulka.columns[1].width = 350;
tabulka.selectable = false;
tabulka.dataProvider = provide;

var btnCol:DataGridColumn = new DataGridColumn("Copy");
tabulka.addColumn(btnCol);
for (var i = 0; i<5; i++){
    provide.addItem({Parcela:"Item "+i});
    btnCol.cellRenderer = new ButtonCell(buttonLabel = "bt"+i, googleURL = "www" + i);
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

First you need to make it so that the parameters get passed to the constructor

public function ButtonCell(buttonLabel:String, googleURL:String)
{
    super();

    _buttonLabel = buttonLabel;
    _googleURL = googleURL;

    this.label = _buttonLabel;

    addEventListener(MouseEvent.CLICK, onButtonClick);
}

Then when creating a new ButtonCell you would just need to pass in the arguments like so:

btnCol.cellRenderer = new ButtonCell(googleURL,buttonLabel);
share|improve this answer
    
Thanks for answer, I alredy tryied this one, but (I don't know how is this possible??) it gives me this error: Scene 1, Layer 'Layer 1', Frame 1, Line 485 1137: Incorrect number of arguments. Expected no more than 1. –  user2567998 Jul 12 '13 at 15:42
    
Is it possible that you only have ButtonCell taking in one parameter? That's the error you will get if that is the case. –  Jnatalzia Jul 12 '13 at 16:49
    
I am not sure: I have written there this: btnCol.cellRenderer = ButtonCell(googleURL,buttonLabel); but i don't know why its giving this error. I am not also sure if it is necessary to put buttons in dataGrid thru class, maybe there is some solution without using class? I am absolutely lost, I dont know how to run my aplication and this is the last step I need to have done. I only need to put the button next to the item in dataGrid... :/ –  user2567998 Jul 12 '13 at 18:46
    
it needs to be new ButtonCell –  Jnatalzia Jul 12 '13 at 18:49
    
whow, this work perfectly, that was the problem. Thank you so much!! However there is one more thing I am not sure about... –  user2567998 Jul 13 '13 at 12:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.