Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2007

Tip: Looking for answers? Try searching our database.

radioButton without radio

Thread view: 
Wenj.Ma@gmail.com - 12 Jul 2007 17:36 GMT
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 / toggleButton?

Thanks
Roedy Green - 12 Jul 2007 19:00 GMT
On Thu, 12 Jul 2007 16:36:50 -0000, "Wenj.Ma@gmail.com"
<Wenj.Ma@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>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.

Have a look at the source code for Swing  JRadioButton in src.zip.

Also look at the code for JButton.  Possibly you will have to create a
subclass of JRadioButton that has a listener for ButtonGroup events to
do the colour changes.

--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Wenj.Ma@gmail.com - 12 Jul 2007 19:36 GMT
Thanks Roedy, I've read the source of JRButton and ButtonGroup, I find
that I can change the uiClassID from "JRadioButtonUI"
to "JButtonUI" to let the RadioButton look like normal JButton. But
I'm not very familiar with define my own event and listener,
like you suggest to add listener for ButtonGroup events. Can you
please brief a little more about what kind of event and event
method I may be used ?
Many thanks.

Ray

> Have a look at the source code for Swing  JRadioButton in src.zip.
>
[quoted text clipped - 5 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
Roedy Green - 12 Jul 2007 23:44 GMT
On Thu, 12 Jul 2007 18:36:42 -0000, "Wenj.Ma@gmail.com"
<Wenj.Ma@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>Thanks Roedy, I've read the source of JRButton and ButtonGroup, I find
>that I can change the uiClassID from "JRadioButtonUI"
>to "JButtonUI" to let the RadioButton look like normal JButton

To learn about the synthetic event approach, see
http://mindprod.com/jgloss/event11.html
Also have a look at the code for JRadioButton to see how the buttons
intercommunicate.

I had not thought of that. Why is that not a suitable solution?
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Wenj.Ma@gmail.com - 13 Jul 2007 00:26 GMT
On Jul 12, 3:44 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:

> I had not thought of that. Why is that not a suitable solution?
> --
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

What do you refer to by "that not a suitable solution"? What's "that"?
As I see it, RadioButtons only intercommunicate when you add them into
one ButtonGroup.
The source code of RButton doesn't show obvious clue for supporting
the inter commu. in its member methods.
All are handled in the ButtonGroup class, the
getSelection(); setSelected(Model,bool) may be responsible.

Ray
Thomas A. Russ - 12 Jul 2007 20:02 GMT
> 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.

Of course, the most fundamental question here is why you want to take a
familiar interface that all users understand and replace it with a new
one that works differently than they might expect.  Are you trying to
make your interface harder to use than it has to be?

Signature

Thomas A. Russ,  USC/Information Sciences Institute

Wenj.Ma@gmail.com - 12 Jul 2007 22:29 GMT
> Of course, the most fundamental question here is why you want to take a
> familiar interface that all users understand and replace it with a new
[quoted text clipped - 3 lines]
> --
> Thomas A. Russ,  USC/Information Sciences Institute

You should never try to guess the customers of a design. You assume
the software is used by public users while
it's always possible that the special user group want some feature
which may against the common
accepting standard. I suggest never try to type such question if you
don't know how to answer my technical question.

I've come up with a solution here.
define my new class:
ChangedRadioButton extends JRadioButton{

in constructor:
public ChangedRadioButton (){
  super();
  setModel(new ChangedRBModel());//force to use new ButtonModel
}

//define an inner class that extends that JToggleButtonModel used by
the JRadioButton
private class ChangedRBModel extends JToggleButtonModel{
  public void setSelected(boolean b){//try to override this method
       ....//copy code from JToggleButtonModel
        if(b)
            ChangedRadioButton.this.setBackground(colorSelected);
        else
            ChangedRadioButton.this.setBackground(colorUnselected);
  }
}

}//ends ChangedRadioButton class

This works because I guess when one radioButton of a group get
selected, rest buttons will call their ButtonModel.setSelected(false),
so the setBackground(color) line is called also.
However, if you try to add simple ActionListner or ItemListener, they
will listen to user input.
They won't get executed if another button in the group get selected
which require the previous selected button on which these Listener is
added get unselected.
Andrew Thompson - 13 Jul 2007 02:54 GMT
>> Of course, the most fundamental question here is why you want to take a
>> familiar interface that all users understand and replace it with a new
...
>You should never try to guess the customers of a design.

That would not be necessary if you had mentioned the
user base in your initial post in this discussion forum.

>...You assume
>the software is used by public users while
>it's always possible that the special ...

'Special'?  What are they, autistic?

>...user group want some feature
>which may against the common
>accepting standard. ..

Note that users, 'special' or otherwise, have been known
for there "wouldn't it be a good idea if.." solutions that,
when they see them implemented, realise how unworkable/
ugly/trite they are.

>...I suggest never try to type such question if you
>don't know how to answer my technical question.

I suggest you never approach this discussion forum
as if it were a help desk.  FTR - the technical goal is
every bit up for discussion - always.  As well as (AFAIC)
the morality of the goal (but that is another story).

>I've come up with a solution here.

'This weeks' solution?  ;-)

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Wenj.Ma@gmail.com - 13 Jul 2007 22:21 GMT
> I suggest you never approach this discussion forum
> as if it were a help desk.  FTR - the technical goal is
[quoted text clipped - 4 lines]
>
> 'This weeks' solution?  ;-)

It is like a help desk, isn't it? All kinds of people ask and answer,
you have to prepare answer for hurried answer seeker without
questioning a lot.
If it's a more prefessional consulting desk, I won't spend time listen
to people questioning my purpose.

A day or week solution is better than nothing or trying to brag about
your ego trip of design to the customer and to force them follow
yours,yea : P

-Ray
Andrew Thompson - 14 Jul 2007 04:25 GMT
>> I suggest you never approach this discussion forum
>> as if it were a help desk.  FTR - the technical goal is
[quoted text clipped - 5 lines]
>you have to prepare answer for hurried answer seeker without
>questioning a lot.

It is also 'like a help desk' in the same way it is 'like a book',
all three contain words.  OTOH It is neither a book nor a help
desk, it is a discussion forum.  That a great deal of 'help' is
dispensed here, freely, is simply a happy side effect.

Note how your eventual solution involved JToggleButton.
I doubt you would have arrived at that solution without the
initial challenge.

And as an aside..
>If it's a more prefessional consulting desk, ..

It is a discussion forum - like I said.

>...I won't spend time listen
>to people questioning my purpose.

Fine.  When your paying, you can take that immature
attitude, and end up paying twice as much (when the
first "don't question my requirement" solution is rejected
by the users.)  I'm sure some of those who run the
commercial help desks & consultancise would love that
type of customer.  Lots of return business!

OTOH - when your *not* paying, what should motivate
us to.. *spend* *time* *listenning* *to* *you* telling us
details are *none* of our *business*?

>A day or week solution is better than nothing or trying to brag about
>your ego trip of design to the customer and to force them follow
>yours,yea : P

I do not unserstand why you are suddenly beating
yourself up - but then - I do not understand that
sentence, or your generally defensive attitude.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Wenj.Ma@gmail.com - 15 Jul 2007 00:35 GMT
> Fine.  When your paying, you can take that immature
> attitude, and end up paying twice as much

Does it make any difference that I tell you "My customer is such such
and I'm requested for an unusual design... " or "I just want a design
that's unusual", if the turn-out is you know nothing about how to
answer it?

>I do not understand that
> sentence, or your generally defensive attitude.

As I indicate repeatedly, this's exactly my customer's require and has
proved effective. Trying to persuade them to use standard LaF is
beating yourself up. Or maybe not for generally quisitive and wordy
person.
Lew - 15 Jul 2007 02:43 GMT
>> Fine.  When your paying, you can take that immature
>> attitude, and end up paying twice as much
[quoted text clipped - 11 lines]
> beating yourself up. Or maybe not for generally quisitive [sic] and wordy
> person.

Gee, whillikers, Wenj, take a chill pill.  This is a discussion group and
we're not in your customer's pay.  You introduce a topic, we discuss it.
Sometimes we ask questions.  We aren't limited to the narrow view that your
customer demands.  That's Usenet.  If you don't like it, this is the wrong
forum for you.

Signature

Lew

Roedy Green - 13 Jul 2007 03:23 GMT
On Thu, 12 Jul 2007 16:36:50 -0000, "Wenj.Ma@gmail.com"
<Wenj.Ma@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>I want to create a group of buttons with radiobutton functionality

Have you considered a set of JToggleButtons and a ButtonGroup?
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Wenj.Ma@gmail.com - 13 Jul 2007 14:13 GMT
> >I want to create a group of buttons with radiobutton functionality
>
> Have you considered a set of JToggleButtons and a ButtonGroup?
> --
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

The reason I don't use JToggleButton is that the toggle has two states
and associate with two appearances.
When click and selected, it's hightlighted, second click will
disselect and unhighlight. But our need is when
click it's always selected, acting like a normal button.

Ray
Roedy Green - 13 Jul 2007 17:14 GMT
On Fri, 13 Jul 2007 13:13:37 -0000, "Wenj.Ma@gmail.com"
<Wenj.Ma@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>The reason I don't use JToggleButton is that the toggle has two states
>and associate with two appearances.
>When click and selected, it's hightlighted, second click will
>disselect and unhighlight. But our need is when
>click it's always selected, acting like a normal button.

You can provide almost a dozen variant icons to use to represent a
button to indicate its state.  That might be helpful.  I have not
followed exactly how this system is supposed to behave.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Wenj.Ma@gmail.com - 13 Jul 2007 19:47 GMT
On Jul 13, 9:14 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
I have not
> followed exactly how this system is supposed to behave.
> --
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

It's just a grid of buttons, next to each other, look like normal
buttons but function like RadioButton. i.e. when clicking on one
button, it's selected and highlighted. When the 2nd button clicked,
the 1st one is unselected and unhighlight but the 2nd button now does.
Click only select a button, if it has been selected then re-click
simply has no effect, unlike ToggleButton that second click will
disselect the button.
In fact, I've change my button class to extends the JButton but set
buttonModel to my changed ToggleButtonModel class. It works.
Roedy Green - 13 Jul 2007 20:34 GMT
On Fri, 13 Jul 2007 18:47:16 -0000, "Wenj.Ma@gmail.com"
<Wenj.Ma@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>In fact, I've change my button class to extends the JButton but set
>buttonModel to my changed ToggleButtonModel class. It works.

Good going.  That sounds exactly the sort of thing Sun had in mind
with all its Model classes.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.