Flex Problem Enabling and Disabling Button in List - apache-flex

I have a list with a dataprovider, it lists out buttons encapsulated in an item renderer. All I want it to do is have a skin that it changes to when it is clicked. I can get this to happen, but then it just goes back to its up state. I want it to stick on the down state, which I have to do by disabling the button.
So I tried this:
buttonList.selectedItem.enabled = false;
for(var i:Number = 0; i< buttonList.numChildren; i++)
{
var loopBtn = buttonList.getChildAt(i);
if(loopBtn != buttonList.selectedItem)
{
loopBtn.enabled = true;
}
}
But this doesn't seem to work. What am I doing wrong here?

Maybe you want to use a toggle button here?
<mx:Button toggle="true" ... />
At least it would stay in the down state after being pressed.

You need a static variable 'selectedButton' in the class those buttons that retains the latest selected button.
on click you set the selectedButton back to non-selected before selecting the new one.
HTH

Related

how to stop getting mouse click events in flex

I have made a hierarchy in which there is a main page, using add element i have attached a component mxml of type group. There is a single button on main page when clicked it should add children of type group in that group type mxml component along with two buttons. Now using one of buttons i am attaching another component mxml type group. the problem is even they overlap i can still excess the children groups of first group component mxml. how can i stop this mouse events to happen.
I think those kind of events usually bubble up to parent components.
You can try using the following code in your mouse click event listener to stop further propagation:
private function onMouseClicked(event: MouseEvent): void {
event.stopPropagation();
... do whatever you wanted when smth was clicked ...
}
By setting enabled, mouseChildren, mouseEnabled to false, you will disable the entire component and it's children. example below
private var myPreviousGroupComponent:Group = null;
function addNewGroup():void
{
if(myPreviousGroupComponent != null)
{
myPreviousGroupComponent.enabled = false;
myPreviousGroupComponent.mouseChildren = false;
myPreviousGroupComponent.mouseEnabled = false;
}
var newGroup:Group = new Group();
addElement(newGroup);
myPreviousGroupComponent = newGroup;
}

Use actionscript to uncheck a checkbox in a component?

I have a component with checkboxes, the checkboxes are bound to booleans in the main code:
<mx:CheckBox id="LyrClearAll" selected="{Application.application.bLyrClearAll}"/>
This works fine for the checkboxes that don’t change unless a user interacts with them again. My problem appears because I want to “uncheck” one of the boxes everytime the component is closed. (I know something other than a checkbox would work better, but I’m trying to keep things consistent in this component.)
I have tried setting the bound Boolean variable to false, and I’ve tried setting the checkbox.selected value to false. Neither are working, everytime I open the component the checkbox is still checked.
private function makeLyrsPopUp(evt:MouseEvent):void
{
var panelLyr:popUpLayers = PopUpManager.createPopUp(this, popUpLayers, false) as popUpLayers;
panelLyr.LyrClearAll.selected == false; //?? set checkbox back to unchecked
panelLyr["cancelButton"].addEventListener("click", removeMe);
panelLyr["okButton"].addEventListener("click", submitData);
PopUpManager.centerPopUp(panelLyr);
function submitData(event:Event):void //change layer visibility based on check boxes in popupLayer
{
bLyrStreet = panelLyr.LyrStreet.selected;
bLyrParcel = panelLyr.LyrParcel.selected;
bLyrClearAll = panelLyr.LyrClearAll.selected;
if (bLyrClearAll)
{
clearLayers();
bLyrClearAll == false; //?? set checkbox back to unchecked
}
removeMe(event);
}
}
Needed to change == false to = false
bLyrClearAll should be declared bindable:
[Bindable]
var bLyrClearAll: Boolean;

Silverlight DataGrid with vertical Scrollbar problem

I have a Datagrid filled with a table. Now the vertical scrollbar shows up because the table doesn't fit. That's fine so far. Now in the last column I have defined a Button in the xaml file. All these buttons have the same callback, but I can distinguish from the selectedIndex of the table what this callback should do. Because clicking the button automatically also selects the line in the DataGrid where this button lives. That's fine so far. Now in my app, for some rows I want to disable the Button, because it has no meaning for that specific row. So what I did is take a subscription on event Load of each Button and let the callback set the MaxWidth = 0, if the button has no meaning. This works fine too, but only initially. As soon as I start dragging the scrollbar, at random places in the Button column buttons show up, or wrong buttons get MaxWidth = 0. I have the strong feeling that cells that scrolled out at the top are being reused at the bottom, but I don't get an event, or at least I don't know which event I should subscribe on. I don't know how to identify the scrollbar. Has anyone a suggestion to tackle this problem?
I finally found a solution to this problem myself, and I will post it for the record.
The event you should subscribe on is LoadingRow (generated by the DataGrid).
In the callback
void TableLoadingRow(object sender, DataGridRowEventArgs e)
you can identify an element in a cell by using VisualTreeHelper for instance as follows:
private void ButtonSetMaxWidth(DependencyObject reference, int maxWidth)
{
if (reference != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(reference); i++)
{
var child = VisualTreeHelper.GetChild(reference, i);
if (child.GetType() == typeof(Button))
{
Button b = (Button)child;
if (b.Name == "TheNameOfTheButtonInTheXAML")
{
b.MaxWidth = maxWidth;
return;
}
}
ButtonSetMaxWidth(child, maxWidth);
}
}
return;
}

How do i know that which button is clicked in flex?

for (iss = 0; iss < listOfProductIds2.length; iss++)
{
// Alert.show(listOfProductIds2[iss]);
var productMain:VBox=new VBox();
var p1:HBox=new HBox();
var l1:Label=new Label();
var b1:Button=new Button();
var spacer:Spacer=new Spacer();
spacer.width=300;
b1.label="Remove";
b1.setConstraintValue("id","");
b1.addEventListener(MouseEvent.CLICK,removeProduct);
l1.text="Product "+iss;
p1.setActualSize(500,500);
p1.addChild(l1);
p1.addChild(spacer);
p1.addChild(b1);
productMain.addChild(p1);
}
function removeProduct(event:MouseEvent):void
{
// How do i know which button is clicked
}
Use event.currentTarget (instead of event.target) because event.target might be the Label component or some styling component within the button, but currentTarget is assured to be the object with which the listener was registered.
To get a handle to the button that was clicked you can just cast the currentTarget to a button.
function removeProduct(event:MouseEvent):void
{
var b1:Button = Button(event.currentTarget);
}
The method setConstraintValue is for setting layout constraints, not setting id. The id property is used by mxml for creating variable names for objects. You can get/set id as you would get/set any other property (say width) - but neither have I seen anyone doing that nor do I see any need to do that in the first place.
event.target should point to the Button you clicked on, shouldn't it ? However you should probably give ids to the buttons to be able to differenciate them (since you create them dynamically.)
Look at event.target.
If ids are assigned dynamically as in the example given b1.id = "button_" + listOfProductIds2[iss]
Then the function that processes the click event would look at the currenttarget, and what I usually do is do a string replace on the part of the id that you know is not dynamic like "button_" with "", which leaves you with the name of the product.

Is there a way to have all radion buttons be unchecked

I have a QGroupBox with a couple of QRadioButtons inside of it and in certain cases I want all radio buttons to be unchecked. Seems that this is not possible when a selection has been made. Do you know of a way I could do this or should I add a hidden radiobutton and check that onen to get the desired result.
You can achieve this effect by temporarily turning off auto exclusivity for all your radio buttons, unchecking them, and then turning them back on:
QRadioButton* rbutton1 = new QRadioButton("Option 1", parent);
// ... other code ...
rbutton1->setAutoExclusive(false);
rbutton1->setChecked(false);
rbutton1->setAutoExclusive(true);
You might want to look at using QButtonGroup to keep things tidier, it'll let you turn exclusivity on and off for an entire group of buttons instead of iterating through them yourself:
// where rbuttons are QRadioButtons with appropriate parent widgets
// (QButtonGroup doesn't draw or layout anything, it's just a container class)
QButtonGroup* group = new QButtonGroup(parent);
group->addButton(rbutton1);
group->addButton(rbutton2);
group->addButton(rbutton3);
// ... other code ...
QAbstractButton* checked = group->checkedButton();
if (checked)
{
group->setExclusive(false);
checked->setChecked(false);
group->setExclusive(true);
}
However, as the other answers have stated, you might want to consider using checkboxes instead, since radio buttons aren't really meant for this sort of thing.
If you're using QGroupBox to group buttons, you can't use the setExclusive(false) function to uncheck the checked RadioButton. You can read about it in QRadioButton section of QT docs. So if you want to reset your buttons, you can try something like this:
QButtonGroup *buttonGroup = new QButtonGroup;
QRadioButton *radioButton1 = new QRadioButton("button1");
QRadioButton *radioButton2 = new QRadioButton("button2");
QRadioButton *radioButton3 = new QRadioButton("button3");
buttonGroup->addButton(radioButton1);
buttonGroup->addButton(radioButton2);
buttonGroup->addButton(radioButton3);
if(buttonGroup->checkedButton() != 0)
{
// Disable the exclusive property of the Button Group
buttonGroup->setExclusive(false);
// Get the checked button and uncheck it
buttonGroup->checkedButton()->setChecked(false);
// Enable the exclusive property of the Button Group
buttonGroup->setExclusive(true);
}
You can disable the exclusive property of the ButtonGroup to reset all the buttons associated with the ButtonGroup, then you can enable the Exclusive property so that multiple button checks won't be possible.

Resources