permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes Is it possible to enable a button at load time? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Is it possible to enable a button at load time?" Watch "Is it possible to enable a button at load time?" New topic
Author

Is it possible to enable a button at load time?

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear All,
In the below code is it possible to enable(selected state) the button "AnnotationEnabled" initially when the applet is loaded, because when the applet is loaded neither of the two buttons are selected.
I want atleast one of them to be active initially preferably "AnnotationEnabled"
How that can be done?
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Thanks all,
If use the doClick() of the button it's working!(Courtesy: Swastik Dey)
See the last line of the init() method.
Complete code:


Thanks Swastik Dey!
My Best Regards!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

ramya narayanan wrote:Thanks all,
If use the doClick() of the button it's working!(Courtesy: Swastik Dey)
See the last line of the init() method.
Thanks Swastik Dey!


doClick() might work for now, but if there is any action associated, that too will be triggered.
The usual way to do it is pass the selection parameter in the constructor, or call the setSelected(boolean) which does not trigger the associated action.

Please choose your forums carefully.
Proper forums also benefit future users with similar problems.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Getting bouncers Manish can you brief it patiently whatever you're saying !
Regards
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Ok.
Your requirement is to select some radio button by default. i.e. when your applet loads, you want to to appear selected.
What you are doing right now is calling doClick() on it. However, doClick is as good as the user pressing the button, which will trigger off the associated action. You already have an action listener for the button. So what you are effectively doing is to select the button and also call the associated action (setting some text in the text field).

This might be your requirement for now. However, in cases where you want the button to appear selected, but not trigger off any associated action, you should call the setSelected() on the radio button as it does not fire the action for the button.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear All,
Consider now I'm using a toggle button(RadioButton) to select or deselect .
1) Once I select the button a mouse event is generated & that toggle button is selected.
Once I de-select the same button a mouse event is generated & that toggle button is de-selected.
I think so correspondingly the mouseclicked(MouseEvent me) method will be called.

Now when I initially want to select the togglebutton without selecting it(i.e at load time it should display as selected) & to be more specific I want the control to go the mouseClicked(MouseEvent me) method where it should be notified that the button is selected (because I want to set some other state based on whether the button is selected or not) .

2) Once in mouseClicked(MouseEvent me) method how we should get the button state i.e selected or de-selected.

Ex:


Likewise how can we get the button state through the mouseEvent.

My best Regards!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

I think you should drop the mouse approach.
You can attach an ActionListener to the toggle button which is easier to implement.
You can get the button state by myButton.isSelected();


Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Action listener should work fine, but if you wish to get the state of the button you probably need MouseListener, the following code might help you to some extent

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



[Edit MG. added code tags]


Swastik
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
First of all Thanks Manish & Swastik for your valuable opinions.
The important point is I need to use only java.awt.* classes & not any javax.swing.* classes.
So in that case I cannot use JRadioButtons.
I need to use their built-in ButtonBar class which is used for creation of these buttons which implements MouseListener.
That's my situation friends right now!
So I request you to keep this in mind & go through my initial post & offer your expert suggestions.
MyBestRegards!

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

If you wish to use only awt classes, I believe Checkbox and CheckboxGroup class should solve your problem, remaining code should be same.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

ramya narayanan wrote:..
The important point is I need to use only java.awt.* classes & not any javax.swing.* classes.
I need to use their built-in ButtonBar class


The code you have posted extends JApplet which is a Swing component.
Check out this article which states why it is a bad idea to mix AWT and Swing in one UI.
What is this "ButtonBar"? Is it some third party component?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Swastik,
Please use code tags.
I have edited your original post. As you can see it is much more readable and easier to understand.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear Swastik & Others,
Consider I have only one Button which can be selected or deselected.


If i give in my init() or someother method
will it call the mouseClicked(...) method along with its state i.e. DragAnnnotation is selected because I need to set some other state based on this value( i.e enabled or diabled).


Can we use the same me.getButton()==MouseEvent.BUTTON1 to check whether the checkbox button is enabled or disabled?
what is this BUTTON1 ?
MyBestRegards

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

It should work with Checkbox also. As far as BUTTON1 is concerned, just see how the code is written MouseEvent.BUTTON1, means its a static variable declared in MouseEvent class which denotes which button has been clicked. Similarly it has BUTTON2 as another static variable.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear Swastik,
Thanks.
How about using this to get button state?
You can get the button state by myButton.isSelected();

Correct me if I'm wrong?
Regards
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear Swastik,
Please reply for my previous post in which I had asked you that when
we give



will it
call the mouseClicked(..) method along with its state i.e whether enabled or disabled.
Regards
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Right, for AWT based checkbox you need the getState() method to use.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

buttontwo.setSelected(true);

no it wont trigger the mouseClicked event.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear All,

no it wont trigger the mouseClicked event.

Swastik my requirement is initially at the time of loading itself the one & only button should be selected, for which we use the
setSelected(true) method.
Great!
But my requirement also needs to call the mouseClicked(mouseEvent me) method after the button is initially selected.
As you said setSelected(true) won't trigger the mouseEvent & hence mouseClicked(..) method won't be called.
How can I resolve my requirement?
Is it possible to use the doClick() method on the button?
Regards

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

doClick will trigger the action event not the mouseEvent, trying to have a look on the issue.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

just add the following lines in the init method at last, and see if it works

long currentTime=java.util.Calendar.getInstance().getTimeInMillis();

//constructor signature of MouseEvent

/*public MouseEvent(Component source, int id, long when, int modifiers,
int x, int y, int clickCount, boolean popupTrigger,
int button) */

MouseEvent me=new MouseEvent(buttontwo,1,currentTime,0,buttontwo.getX(),buttontwo.getY(),1,false,MouseEvent.BUTTON1);
mouseClicked(me);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Swastik Dey wrote:long currentTime=java.util.Calendar.getInstance().getTimeInMillis();

Or just use System.currentTimeMillis().


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Thanks Swastik,Manish & others who've contributed in this discussion.
Finally I was able to fulfill my requirements through the use of checkbox & itemListeners.


In itemlistener's itemStateChanged(...) method



Now it's working!
Thanks All!

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Has anyone tried this:


Read previous post by swastik.
What is the result?
Regards
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear Swastik & others,
Yes it is possible to generate events manually & call the listener methods.
Only thing is before that we should use the button's setSelected(true) button to make the button looks like enabled or clicked.
See the last 4lines in init() method:


Thanks for all!
My Best Regards!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to enable a button at load time?
 
Similar Threads
Explicitly throwing events & calling listener methods?
Doubt in ternary operator
why doesn't this code work?
Why am I getting NumberFormatException?
need help checking this code