I started to look into using GWT in combination with UiBuilder. I'm a bit puzzled about how you can use the #UiHandler(..) directive to make simple event handle code as written down in the GWT documentation:
#UiHandler("button")
void handleClick(ClickEvent e) {
Window.alert("Hello, AJAX");
}
In this case the method handleClick is used.
How do you know for each GWT widget what methods can be created with #UiHandler? For some you can also create a doClose() method.
But what can you use with, for instance, a ListBox to get an event an item is selected? Where in the documentation can I see this?
The parameter you pass to the #UiHandler annotation is the name of the appropriate field you want to assign that *Handler. So, in this case you are assigning a ClickHandler to a Button button (actually, we just know the field's name).
As for how this exactly works - it's part of GWT magic :) My guess is that, just like any other UiBinder related code (I think there was a presentation on Google IO, that showed the code that UiBinder generates), at compilation time the compiler figures out what goes where. In this example: we have a Button button, and we have a #UiHandler annotated method that has a ClickEvent parameter -> that must mean it's a ClickHandler (notice that the method's name doesn't matter). So let's add some code at compile time (in the constructor, probably) that adds that handler to the button. If you are interested in a more comprehensive answer - check out the source :D
But what can you use with, for
instance, a ListBox to get an event
an item is selected? Where in the
documentation can I see this?
In the GWT API reference. In this case, you are probably looking for ListBox.addChangeHandler. But you usually won't find #UiHandler related code there - that's because it would be redundant - you always construct the #UiHandler methods the same way:
You check the *Handler that you want to add, say ChangeHandler
It has a void onChange(ChangeEvent event) - so, your method needs a ChangeEvent parameter and should look like this:
#UiHandler("listBox")
void whateverName(ChangeEvent event) {
// ...
}
Probably your problem is in your onModuleLoad method:
public void onModuleLoad()
{
HelloWorld helloWorld = new HelloWorld("BOTAO");
// Using this way #UiHandler will not work
//Document.get().getBody().appendChild(helloWorld.getElement());
// correct way
RootPanel.get().add(helloWorld);
}
Related
The question is more or less what the title says: I have a CustomView class that implements the View interface (Thus, the enter function, which takes a ViewChangeEvent parameter).
I'm trying to identify when the view change is triggered by an user clicking the browser "back" button (as opossite as an user clicking a link to enter that view on the application), but I've been unable to get where (if it is possible) get this information.
Am I looking into the right component? Should I look anywhere else? There's any way to find about this?
EDIT: Huh, maybe I could add a custom parameter in every, single, call to the ViewChange so I can "mark" the "normal clicks" and identify "go back/go forward" by the absence of it, but it seems like a dirty, ugly trick...
Ok, this will go with a bit of story, so:
I found a way to identify the "user has pressed the back button" on Vaadin, as it seems the framework manages the location change through the Page.updateLocation(String location, boolean fireEvents) function. I haven't, though, being able to modify the location parameter (This could be either due Vaadin's own architecture or due my own application specifics), so I've ended up using the function "changeVariables" on the UI class (on a inherited, own, UI class) to send a new ViewChangedEvent.
It feels a bit like a hack, but as I said, I haven't been able to make it better.
Going a bit into the specifics of my problem, in case someone finds it useful, I needed to identify somehow that the user was "coming back" from a different view through the "back" browser button or the "backspace" key, so I decided to used a flag on the params to indicate that the view is "retornable." This flag will have two values: "retornable" (as in "I just got into the view") and "returning" (as in "I just came back to a retornable view"). For those who will only look at code, this is what the url will show:
#!viewName/retflag=retornable&otherStuff=maybe
#!viewName/retflag=returning&otherStuff=perhaps
As I said, my "entry point" is located on "OurCustomUI" which extends UI, and had this little function:
#Override
public void changeVariables(Object source, Map<String, Object> variables) {
super.changeVariables(source, variables);
}
Here, the idea is getting the "location" variable from the, huh, variables Map and update it. It would be something like this:
myServer.myCompany.com/myApp/#viewName/retflag=retornable
So the code would be finding the "retflag" value, changing it to "returning", creating the ViewChangedEvent and firing it. I need to keep the super.changeVariables() call at the beginning because reasons, but it seems to work just fine as it is.
I am working on a GUI java program for class where there are 10 numbered buttons in a grid layout. The user is trying to guess a 3 digit number where each digit is unique. When they click a number the corresponding number should be stored as one of the digits in the guess and then the button should be disabled. This is done in the actionPerformed method.
My problem is how to tell which button is disabled.
Currently I am trying to successfully read the value and disable the button for one digit and my code looks like this:
private class NumberListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent click){
Object source = click.getSource();
keyTry1 = getButtonNumber(source);
source.setEnabled(false); //error
}
However at the line I marked error NetBeans complains that source has no method setEnabled, presumably because in the method it is declared as type Object. However if I try to declare it as JButton I cannot use click.getsource();
I know I could go the brute force approach and have a long string of if/else statements or even another method which disables the button based on the number it represents, but I was wondering if there is a way to access source as a JButton, since it obviously is referencing a JButton.
source is an Object and does not have an setEnabled method, so it makes no sense to the compiler to allow you to make such a call. You need to first case the instance of source to it's appropriate class type.
Assuming you can guarantee that the source of the action is the button, you can use something like...
((JButton)click.getSource()).setEnabled(false);
If you can't guarantee that source is JButton, but might be component of some kind, you might even be able to use something like...
((Component)click.getSource()).setEnabled(false);
If you can't guarantee that, then you need to make appropriate checks (which you should do anyway)...
Object source = click.getSource();
if (source instanceof JButton) {
((JButton)source).setEnabled(false);
}
This concept is an example of Polymorphism, where one instance of an Object can act like one it's parents
I'd like to add an extra feature to the debug mode of the programme I've taken over.
What I want to be able to do add tooltips to all the buttons, tables on the GUI so that they display something like this in them:
Class: JButton
Name: myShineyButton
Method: myShineyButtonActivateMethod
I know the general form for using reflection of this is like so:
for(Fields bits: this.GetClass().getDeclaredFields){
if(bits instanceof Component){
String methodName = bits.getMethod().toString();
....
}
}
But then how do I add the tooltip to each item on the gui?
You may want to look into an aspect-oriented programming (AOP) tool, such as AspectJ. You can examine/modify an object after returning from a call to its constructor. As a concrete example, this aspect, cited here, detects all EDT rule violations. More examples may be found here.
errorPopup= popFactory.getPopup(this, errorBox,
(verifierTopComponent.super.getX()+verifierTopComponent.super.getWidth()/2),
(verifierTopComponent.super.getY()+verifierTopComponent.super.getHeight()/2));
The code above works, and properly centers the popup... but only if the window is fullscreen, on my main monitor.
How do I make it more robust? I'd like to center it in the middle of the current RCP instance.
(verifierTopComponent is my incorrectly named TopComponent in the module).
After the comment below, I'm wondering if maybe y'all typically use a vastly different method to create a popup? I'm just trying to put something in the user's face to let them know why things won't work as they have done them.
When using the NetBeans RCP you should rather use DialogDisplayer and DialogDescriptor
Something like this:
DialogDescriptor dd = new DialogDescriptor(errorBox, "Error message");
Object result = DialogDisplayer.getDefault().notify(dd);
It will automatically take care of calculating the correct position.
I'm unsure how to solve your specific issue but in my experience you can/should use NetBeans' org.openide.NotifyDescriptor class to show notifications to the user. You will need to add a dependency for the Dialog API to your module to use the following.
NotifyDescriptor nd = new NotifyDescriptor(
"This is the message that will go in the main body of the message. This could also be a custom JPanel",
"Title of Dialog",
NotifyDescriptor.DEFAULT_OPTION,
NotifyDescriptor.ERROR_MESSAGE,
null, // this could be an array of JButtons that will replace the dialog's built-in buttons
NotifyDescriptor.OK_OPTION);
Object returnedValue = DialogDisplayer.getDefault().notify(nd);
if (returnedValue == NotifyDescriptor.OK_OPTION) {
// user pressed OK button
}
As always, see the javadoc for NotifyDescriptor for more info
Edit As described in another answer you could use the DialogDescriptor class which extends the NotifyDescriptor class and adds the ability to set the dialog to modal along with a couple of other useful features.
There are also a couple of other useful classes that extend the NotifyDescriptor class that may be useful for other situations. See the javadoc for NotifyDescriptor for a list of subclasses.
How to know which commands are available? I tried looking up in the Java SDK, but didn't find a thing about it.
The only way I can think of is this way
class ButtonListener implements ActionListener {
ButtonListener() {
}
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
}
but I guess those values must be hardcoded in some part of the framework. Where can I find them?
Thanks
EDIT
Take as example the following code taken from here:
public void actionPerformed(ActionEvent e) {
if ("disable".equals(e.getActionCommand())) {
...
}
...
}
The site's author knew there was an action command that was "disable". Where did he get that information from?
You may be thinking of the Action interface, named instances of which are used to establish default key bindings in various L&Fs. The article Key Bindings includes a convenient utility to examine them.
Addendum: As a concrete example, the JButton in ClickCount takes its name and action command from the nested ClickHandler because the hideActionText property of Action is false by default. As another example, Key Bindings shows that buttons have a named action for "pressed" and "released" that is created by createButtonListener() in BasicButtonUI. BasicButtonListener in turn uses a nested UIAction to handle the two commands, as shown here.
You do it by using setActionCommand()
JButton jb = new JButton("MyButton");
jb.setActionCommand("MyButtonCommand");
EDIT:
The site's author got "disable" from this line of code
b1.setActionCommand("disable");
It's whatever you set.
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Button.html#setActionCommand%28java.lang.String%29
My guess is the default is the name of the button.
Your edit:
It could have come from two places.
It was the name of the button (new JButton("disable")), which by default is the action command.
It was the command given by setActionCommand("disable")
It is very important to illustrate that by design it doesn't matter what the source was. We don't care if it's a button, drop down, menu, or anything. All that matters is the "disable" command was given. It's the command pattern. :-)
There are no 'valid' commands. The command is intended for use by your application and has no effect on how the button is handled by Swing.
In the second code example you posted it is being setin the code at the following line
b1.setActionCommand("disable");
For buttons the default is to use the text of the button, if the action command is not set