timefortea
29 Oct 2008, 7:47 AM
Hi,
I am trying to simulate radio buttons, except that I want it to be for two text labels on a toolbar. Here is how I have attempted to code this:
ToolBar toolbar = new ToolBar();
ToggleToolItem first= new ToggleToolItem("First");
toolbar.add(first);
toolbar.add(new SeparatorToolItem());
ToggleToolItem second = new ToggleToolItem("Second");
toolbar.add(second);
first.toggle(true);
first.addListener(Events.Toggle, new Listener<ToolBarEvent>()
{
public void handleEvent(ToolBarEvent event)
{
if (first.isPressed())
{
second.toggle(false);
}
}
});
second.addListener(Events.Toggle, new Listener<ToolBarEvent>()
{
public void handleEvent(ToolBarEvent event)
{
if (second.isPressed())
{
first.toggle(false);
}
}
});
The code works to an extent, in that clicking on one toggles the other one off. The problem is that I can have neither on, which I do not want - hence the "first.toggle(true)" line - but this does not work. Also, I'd like to prevent someone "un-toggling" the button that is toggled on, in other words I want to prevent there being zero buttons toggled on, there should always be exactly one. I haven't found out how to make this work properly and I think I may have chosen the wrong way to do it.
Is there an easier way to do this? It has to be on the toolbar, not in a menu, hence I didn't use Radio buttons.
Thanks.
I am trying to simulate radio buttons, except that I want it to be for two text labels on a toolbar. Here is how I have attempted to code this:
ToolBar toolbar = new ToolBar();
ToggleToolItem first= new ToggleToolItem("First");
toolbar.add(first);
toolbar.add(new SeparatorToolItem());
ToggleToolItem second = new ToggleToolItem("Second");
toolbar.add(second);
first.toggle(true);
first.addListener(Events.Toggle, new Listener<ToolBarEvent>()
{
public void handleEvent(ToolBarEvent event)
{
if (first.isPressed())
{
second.toggle(false);
}
}
});
second.addListener(Events.Toggle, new Listener<ToolBarEvent>()
{
public void handleEvent(ToolBarEvent event)
{
if (second.isPressed())
{
first.toggle(false);
}
}
});
The code works to an extent, in that clicking on one toggles the other one off. The problem is that I can have neither on, which I do not want - hence the "first.toggle(true)" line - but this does not work. Also, I'd like to prevent someone "un-toggling" the button that is toggled on, in other words I want to prevent there being zero buttons toggled on, there should always be exactly one. I haven't found out how to make this work properly and I think I may have chosen the wrong way to do it.
Is there an easier way to do this? It has to be on the toolbar, not in a menu, hence I didn't use Radio buttons.
Thanks.