Please join us at the new JavaWorld Q&A Forums. Your existing login will work there. The discussions here are now read-only.


JavaWorld Talkback >> 958545

Pages: 1
Pit
Unregistered




Correction
      #2131 - 09/19/03 06:18 AM

I think you have to set instance variable "selectedButton" in Method "setSelected(ButtonModel, boolean)" and then refactor to remove duplicated code. Thanks for the article.

Post Extras: Print Post   Remind Me!   Notify Moderator  
Daniel Tofan
Unregistered




Re: Correction [Re: Pit]
      #2193 - 09/23/03 12:50 AM

That's right, this one slipped through. As a matter of fact, here is how both methods should look like:

public void setSelected(AbstractButton button, boolean selected)
{
if (button != null) setSelected(button.getModel(), selected);
}

public void setSelected(ButtonModel model, boolean selected)
{
AbstractButton button = getButton(model);
if (buttons.contains(button))
{
super.setSelected(model, selected);
if (getSelection() == button.getModel()) selectedButton = button;
}
}

Thanks for noticing DT


Post Extras: Print Post   Remind Me!   Notify Moderator  
Mark Thomas
Unregistered




Adding unselectAll method [Re: Daniel Tofan]
      #5341 - 01/15/04 11:39 AM

Currently, neither ButtonGroup or JButtonGroup allows you to unselect all buttons. I have modified Daniel's code to provide this functionality. Here's the new class:

public class JButtonGroup extends ButtonGroup {
/**
* Used to deselect all buttons in the group
*/
private static final AbstractButton dummyButton = new JButton();
/**
* Stores a reference to the currently selected button in the group
*/
private AbstractButton selectedButton;

/**
* Creates an empty <code>JButtonGroup</code>
*/
public JButtonGroup() {
super();
add(dummyButton);
}

/**
* Creates a <code>JButtonGroup</code> object from an array of buttons and adds the buttons to the group
* No button will be selected initially.
* @param buttons an array of <code>AbstractButton</code>s
*/
public JButtonGroup(AbstractButton[] buttons) {
add(buttons);
add(dummyButton);
}

/**
* Adds a button to the group
* @param button an <code>AbstractButton</code> reference
*/
public void add(AbstractButton button) {
if (button == null || buttons.contains(button)) {
return;
}
super.add(button);
if (getSelection() == button.getModel()) {
selectedButton = button;
}
}

/**
* Adds an array of buttons to the group
* @param buttons an array of <code>AbstractButton</code>s
*/
public void add(AbstractButton[] buttons) {
if (buttons == null) {
return;
}
for (int i=0; i<buttons.length; i++) {
add(buttons);
}
}

/**
* Removes a button from the group
* @param button the button to be removed
*/
public void remove(AbstractButton button) {
if (button != null) {
if (selectedButton == button) {
selectedButton = null;
}
super.remove(button);
}
}

/**
* Removes all the buttons in the array from the group
* @param buttons an array of <code>AbstractButton</code>s
*/
public void remove(AbstractButton[] buttons) {
if (buttons == null) {
return;
}
for (int i=0; i<buttons.length; i++) {
remove(buttons);
}
}

/**
* Sets the selected button in the group
* Only one button in the group can be selected
* @param button an <code>AbstractButton</code> reference
* @param selected an <code>boolean</code> representing the selection state of the button
*/
public void setSelected(AbstractButton button, boolean selected) {
if (button != null && buttons.contains(button)) {
setSelected(button.getModel(), selected);
if (getSelection() == button.getModel()) {
selectedButton = button;
}
}
}

/**
* Sets the selected button model in the group
* @param model a <code>ButtonModel</code> reference
* @param selected an <code>boolean</code> representing the selection state of the button
*/
public void setSelected(ButtonModel model, boolean selected) {
AbstractButton button = getButton(model);
if (buttons.contains(button)) {
super.setSelected(model, selected);
if(model != dummyButton.getModel()) {
selectedButton = button;
}
}
}

/**
* Returns the <code>AbstractButton</code> whose <code>ButtonModel</code> is given.
* If the model does not belong to a button in the group, returns null.
* @param model a <code>ButtonModel</code> that should belong to a button in the group
* @return an <code>AbstractButton</code> reference whose model is <code>model</code> if the button belongs to the group, <code>null</code>otherwise
*/
public AbstractButton getButton(ButtonModel model) {
for(Iterator i = buttons.iterator(); i.hasNext(); ) {
AbstractButton ab = (AbstractButton)i.next();
if (ab.getModel() == model) {
return ab;
}
}
return null;
}

/**
* Returns the selected button in the group.
* @return a reference to the currently selected button in the group or <code>null</code> if no button is selected
*/
public AbstractButton getSelected() {
if(selectedButton == dummyButton) {
return null;
} else {
return selectedButton;
}
}

public ButtonModel getSelection() {
if(selectedButton == dummyButton) {
return null;
} else {
return super.getSelection();
}
}

/**
* Returns whether the button is selected
* @param button an <code>AbstractButton</code> reference
* @return <code>true</code> if the button is selected, <code>false</code> otherwise
*/
public boolean isSelected(AbstractButton button) {
if(button == dummyButton) {
return false;
}
return button == selectedButton;
}

/**
* Returns the buttons in the group as a <code>List</code>
* @return a <code>List</code> containing the buttons in the group, in the order they were added to the group
*/
public List getButtons() {
List allButtons = new LinkedList(buttons);
allButtons.remove(dummyButton);
return Collections.unmodifiableList(allButtons);
}

/**
* Checks whether the group contains the given button
* @return <code>true</code> if the button is contained in the group, <code>false</code> otherwise
*/
public boolean contains(AbstractButton button) {
if(button == dummyButton) {
return false;
}
return buttons.contains(button);
}

/**
* unselects all buttons
*/
public void unselectAll() {
setSelected(dummyButton, true);
}
}


Post Extras: Print Post   Remind Me!   Notify Moderator  
Anonymous
Unregistered




Re: Adding unselectAll method [Re: Mark Thomas]
      #9597 - 07/23/04 08:55 AM

Because there's an extra dummy button, I'd have thought that the getButtonCount() method would have to be overidden to return buttons' length-- ?

Post Extras: Print Post   Remind Me!   Notify Moderator  

Unregistered




Re: Adding unselectAll method [Re: Mark Thomas]
      #46512 - 04/25/07 06:56 AM

@Mark: Your add-method for button-arrays contains a bug, write add(buttons); instead of add(buttons);

Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 1 anonymous users are browsing this forum.

Moderator:   

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 8157

Rate this topic

Jump to

Contact us JavaWorld

Powered by UBB.threads™ 6.5.5