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 / First Aid / September 2004

Tip: Looking for answers? Try searching our database.

Emulating component operation?

Thread view: 
Kiwi - 17 Sep 2004 21:08 GMT
This is repost of news:dc95c98e.0409160631.99d23f1@posting.google.com

Hello.

Excuse my poor Engli

I have a stupid program that has a three JRadioButton named R, G, and B.
When it is launched, no button is selected. Window is made of one JPanel.
First time, the JPanel's background color is default.

Next, when R button is selected, the background color of the JPanel turns into
red, when G is selected the color turns into green, and when Y, Yellow.

For such a program, normal requirements are that one JRadioButton is selected
when it is launched and that there is at least one another feedback of button
selection (e.g. background color).

My stupid program does not meet these requirement. But if there is a way to
emulate JRadioButton's clicking from code, I can meet them.

Is there such a way?
Am I thinking in a wrong way?

Thank you in advance.

///Hiroki Horiuchi
Paul Lutus - 17 Sep 2004 21:21 GMT
/ ...

> My stupid program does not meet these requirement. But if there is a way
> to emulate JRadioButton's clicking from code, I can meet them.
>
> Is there such a way?
> Am I thinking in a wrong way?

The problem is in your code, the code you did not post.

Signature

Paul Lutus
http://www.arachnoid.com

Kiwi - 18 Sep 2004 07:20 GMT
Thank you Paul.

My very first program was.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class RGBBackgroundNG {
   public static void main(String[] args) {
       JFrame frame = new JFrame();
       final JPanel panel = new JPanel();
       frame.getContentPane().add(panel);
       final ButtonGroup buttonGroup = new ButtonGroup();

       class XRadioButton extends JRadioButton {
           XRadioButton(final Color color, String label) {
               super(label);
               addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                       panel.setBackground(color);
                       panel.repaint();
                   }
               });
               panel.add(this);
               buttonGroup.add(this);
           }
       }

       //These constructors add "this" to some collections.
       new XRadioButton(Color.RED, "R");
       new XRadioButton(Color.GREEN, "G");
       new XRadioButton(Color.BLUE, "B");

       frame.setSize(200, 200);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.show();
   }
}

I read your message, thought and now I can improve(?) the program as below.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class RGBBackground {
   public static void main(String[] args) {
       JFrame frame = new JFrame();
       final JPanel panel = new JPanel();
       frame.getContentPane().add(panel);
       final ButtonGroup buttonGroup = new ButtonGroup();

       class XRadioButton extends JRadioButton {
           private Color color;

           XRadioButton(Color color, String label) {
               super(label);
               this.color = color;
               addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                       panel.setBackground(XRadioButton.this.color);
                       panel.repaint();
                   }
               });
               panel.add(this);
               buttonGroup.add(this);
           }

           void select() {
               setSelected(true);
               getParent().setBackground(color);
           }
       }

       //These constructors add "this" to some collections.
       XRadioButton deFault = new XRadioButton(Color.RED, "R");
       new XRadioButton(Color.GREEN, "G");
       new XRadioButton(Color.BLUE, "B");

       frame.setSize(200, 200);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       deFault.select();
       frame.show();
   }
}

Thank you again.

Kiwi
Tor Iver Wilhelmsen - 17 Sep 2004 22:55 GMT
> My stupid program does not meet these requirement. But if there is a way to
> emulate JRadioButton's clicking from code, I can meet them.

You mean hack around a missing call to setSelected(true)? The rest of
the associated initial state probably also needs to be set.

You can do wonders using java.awt.Robot, but why?
Oscar kind - 18 Sep 2004 08:41 GMT
> I have a stupid program that has a three JRadioButton named R, G, and B.
> When it is launched, no button is selected. Window is made of one JPanel.
> First time, the JPanel's background color is default.

> For such a program, normal requirements are that one JRadioButton is selected
> when it is launched and that there is at least one another feedback of button
> selection (e.g. background color).
>
> My stupid program does not meet these requirement. But if there is a way to
> emulate JRadioButton's clicking from code, I can meet them.

First, I don't think it is such a stupid program. Granted, it hardly does
anything. But if it is a homework assignment, it meets all requirements
but one. That's quite good.

You identified the missing part to be in the initialization: you want one
of the JRadioButton objects be "selected" at startup.

A short trip to the API documentation reveals:
   JRadioButton(Icon icon, boolean selected)
   JRadioButton(String text, boolean selected)
   JRadioButton(String text, Icon icon, boolean selected)

Just use "true" as the last parameter, and set the initial background
color accordingly.

A slightly longer trip to the API reveals:
   AbstractButton#doClick()

Calling this method has the same effect as pressing and releasing the
JRadioButton (which is a subclass of AbstractButton).

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2



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.