radioButton without radio

Hi,

I want to create a group of buttons with radiobutton functionality (exclusive to each other) but I don't want that radio in front of each button. I want those buttons look like normal JButton. In addition, I specify 2 colors for the button when selected and unselected. But I don't know how to make one button use unselected color when another button is pressed and this button got unselected by the radio button logic.

I've add a group of JButton instances to the ButtonGroup and addActionListener for user mouse press on one button and it got selected, it runs but when another button is pressed, the previous selected button still use the SELECT_COLOR, it can't change to UNSELECT_COLOR.

How to do that?extend the buttonModel or radioButton?

Thanks

[791 byte] By [angelflareea] at [2007-11-27 10:24:48]
# 1

Check out JToogleButton and the ButtonGroup classes under the javax.swing package

Also read the info on How To Use Buttons from the sun tutorial: http://java.sun.com/docs/books/tutorial/uiswing/components/button.html

ICE

icewalker2ga at 2007-7-28 17:30:02 > top of Java-index,Desktop,Core GUI APIs...
# 2

of course I've read them all. If this's a simple entry level question I won't post at here. I've tried to google about but no one seem to have the same question before.

There's nowhere that seems to me that can be extended to specify customized look for radioButton.

The buttonGroup only responsible for arranging the group of button in a logical exclusive group, not deal with L&F.

Maybe I should extends buttonModel for radioButton, but how?

I actually have achieved what I want by simply define a group of buttons and addListener to each, so when one button is selected, use:

buttonGroups[prevSelected].setSelected(false);buttonGroups[curSelected].setBackground(COLOR_Unselected);

...

But I want to use radio button class which more close to what I want to achieve in concept and customize it. That's my question.

Thanks

angelflareea at 2007-7-28 17:30:02 > top of Java-index,Desktop,Core GUI APIs...
# 3

sorry, wrong code

...

buttonGroups[prevSelected].setSelected(false); buttonGroups[prevSelected].setBackground(COLOR_Unselected);

buttonGroups[curSelected].setBackground(COLOR_Selected);

...

angelflareea at 2007-7-28 17:30:02 > top of Java-index,Desktop,Core GUI APIs...
# 4

is this part-way of what you're after (rb2)?

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Testing

{

public void buildGUI()

{

ButtonGroup group = new ButtonGroup();

JRadioButton rb1 = new JRadioButton("RB1",true);

JRadioButton rb2 = new JRadioButton("RB2");

JRadioButton rb3 = new JRadioButton("RB3");

group.add(rb1); group.add(rb2);group.add(rb3);

rb2.setUI(new javax.swing.plaf.basic.BasicButtonUI(){

public void paintIcon(Graphics g,JComponent c,Rectangle iconRect){}

});

JPanel p = new JPanel(new GridLayout(3,1));

p.add(rb1);p.add(rb2);p.add(rb3);

JFrame f = new JFrame();

f.getContentPane().add(p);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-28 17:30:02 > top of Java-index,Desktop,Core GUI APIs...
# 5

Thanks for the code.

I've achieved this by define a new ToggleButtonModel extension class inside my Button class and change the setSelected() code to recolor previous selected button when new button selected.

angelflareea at 2007-7-28 17:30:02 > top of Java-index,Desktop,Core GUI APIs...