By using doClick() method, the action of a button can be triggered. Is there such a way to change combobox value when a button is clicked in java? Thank You.
As you didn't state the graphical framework I assume you use Swing, as it contains a doClick function.
The doClick() function is only used to emulate a button click programmatically, for example in testing. This will fire all actions that are normally associated with that button, however you need to register those yourself, using a listener.
Related
I'm trying to find a way to tell what object fired my onClick event. I have a checkbox that currently fires once when the checkbox is clicked and fires twice when the label is clicked. I want to be able to ignore when the label is clicked.
I have found some code that is similar to what I want here which explains how to use a gxt FieldEvent, but i'm using a gxt BaseEvent and i'm not able to change it to a field event. Is there an equivalent function to .getTarget() for BaseEvents?
TLDR; Does anyone know how to do an equivalent of what's here but using gxt BaseEvent rather than FieldEvent?
According to the api here:
http://dev.sencha.com/deploy/gxt-2.2.5/docs/api/com/extjs/gxt/ui/client/event/BaseEvent.html
you should be able to call the getSource() method and then extract the name. Was this what you were looking for?
Is it possible to simulate a click on a button, similar to doClick() but just graphical simulate it, not generate any ActionEvent´s. If know that i can extend the class, do my own doClick with a simple if-statement. But is it any other possibility?
I want to this because I have a button that the user can press, but sometimes the computer (it s in a game) "presses" the button. All the logic is done in another thread, I just wanna display it for the user.
if you dig into the details in the look and feel you're using, you might be able to see how it detects and paints the button in its "down" state, and then simulate that?
or you could extend/implement ButtonModel, and mess with the setPressed/isPressed state?
I think the simplest solutions is to just check the model in the ActionListener.
I don't see a way to distinguish between the two; the model is oblivious as to who calls its methods. Instead, you could save all the listeners, invoke doClick(), and restore the listeners. It looks like you would have to check action, change and item listeners.
If you want to click on the button try this:
try
{
robot = new Robot();
robot.mouseMove(0,500);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}catch(Exception e){}
But maybe you will prefer to simply disable the button when the user shouldn't click it
Button.setEnabled(false);
You can set whether it's enabled or not, so use "buttonName.setEnabled(false);"
I'm developing a SWT app and in one particular form there are 14 pairs of Yes-No radio buttons. Each of these pairs have a text box associated with them. So if a user selects Yes, the associated textbox should be editable else uneditable. I find writing 28 listeners for the radio buttons really daunting. Since the radio buttons have nothing much to do than just rendering the textbox editable/uneditable I was hoping if there were some generic type of listeners in SWT that would be applicable to a set of radio buttons specified in an array or like that. Are there any frameworks or shall I have to write individual listeners?
Edit
I'm trying to fire an event only when the radio button is selected
rdoExperience.addListener(SWT.CHECK, new RadioButtonSelection(
txtExperience));
but SWT.CHECK is causing the event to be fired on mouse hover over radio button too. I've tried using SWT.SELECTED too but it's not working either and I can't find other suitable SWT constants. W;hat should I use?
Good point. Sorry I don't know such thing.
However, you create one yourself: Instead of writing an anonymous listener for every button, you could write one - say MyButtonListener - and give it the button text box as an argument. Than you instantiate MyButtonListener with the appropriate text box as an argument. Than in the Listeners appropriate callback method you enable or disable the text box.
Edit: My bad. Of course I meant you could give it your text box like radioBtn.addListener(SWT.SELECTED, new MyButtonListener(textfield1));
You could create one SelectionListener and add it to each of the radio buttons. Then you can ascertain which button was pressed from the selection event and map that to a text box. For the mapping you could use an array or hashtable.
Here is the scenario. I have an swing applet with tons of checkboxes. some of them are disabled/unchecked when checking another. Each ItemStateChange() event executes a method to parse the entire form for changes. Is there a way to tell if an ItemStateChange() event was triggered due to a mouse click or from a setSelected() call?
The ItemStateChange() for each checkbox has the standard parameter java.awt.event.ItemEvent evt
I'd like to only call the processOrder() method once when a box is clicked. Right now it fires for each change thats made, regardless of whether the change happened from setSelected(). Sometimes there are 10+ parseForm(); calls from a single click.
You can't tell whether the source of the event is a mouse click or a setSelected call from the ItemEvent.
It sounds like you have a loop in your check box logic. You might want to add a controller that handles the events and sets each checkbox yet ignores events that occur due to calling setSelected on other check boxes.
Is there a way to tell if an ItemStateChange() event was triggered due to a mouse click or from a setSelected() call?
If your application manually invokes the setSelected() method then you can use code like:
checkBox.removeItemListener(...);
checkBox.setSelected(...);
checkBox.addItemListener(...);
If you are able to change to use a MouseListener instead of an ItemListener and respond to the mouseClicked() event you will only receive the events for the checkbox selected by the user.
For example, say I have a series of toolbar-style buttons across the top of my application's main window. I want to attach a mnemonic to one of these that's just a single keypress, like F3.
When you set the mnemonic to KeyEvent.VK_F3, the user has to press Alt+F3.
If you have a menu item, you can set an accelerator, rather than a mnemonic, and choose whether to use a meta key. Buttons don't let you set an accelerator, however.
Is there a way to turn of the meta-key for button mnemonics?
Actions can bind a chunk of code to a menu item, a keystroke, a button and anything else you are interested.
In general, don't think of your code as tied to a specific keypress/event--and don't use anonymous inner classes. Instead use real classes where your code can be reused for different types of things. That pattern used by the Action class gives some good examples of this.
Well behind the scenes, whether you use an accelerator or a mnemonic on a component, the method will create a Key Binding for you.
So there is nothing to prevent you from binding a KeyStroke and Action to whatever component you want and manually create the Key Binding. It can even be a component that doesn't have the setMNemonic(...) method.
Are you sure that accelerators cannot be defined on buttons if the button was configured using an Action? (I know this was true at one point, but I thought this may be different in later versions of Java.)
In any event, here is another method to set it on a button:
button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(myKeyStroke, "actionName");
button.getActionMap().put("actionName", myAction);
Where myKeyStroke is a keystroke such as F3, "actionName" is a label (String), and myAction is the action it invokes.