extends JPanel implements MouseListener, MouseMotionListener
when I use repaint(), the method mousePressed(MouseEvent e) begins to sense two clicks even though I only pressed one time.
Does anyone happen to know how to fix this?
#Override
public void mousePressed(MouseEvent e) {
repaint();
}
Related
I have a Jbutton added on a frame automaticaly by netbeans.
I want to add this Actionlistener to a button.
public class MyActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"hello");
}
My problem is that when I add this code
jButton2.addActionListener(MyActionListener);
in order to add the listener to the button I get a "non-static variable jButton2 cannot be referenced from a static context" message.
Since jButton2 is automatically created by netbeans as non static, how can overcome this problem and set the actionlistener as I want?
The error lies in that you are sending an object that has never been instantiated. When you add an action listener to any component you need to send an object that implements the ActionListener interface. Is like adding an instance of the listener to the object.
try this:
jButton2.addActionListener(new MyActionListener());
if you want to use an Anonymous implementation then you will do:
jButton2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//some code
}
});
An easy way to add a listener is just to right-click on the button from the design view, select
Events -> Action -> actionPerformed
and the code will be auto-generated for you
public void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// put what you want to happen, here
}
Ok, here is my problem. I have 2 presenters: FirstPresenter (ex: abc.com#first) & SecondPresenter (ex: abc.com#second). There is a button on SecondPresenter & when user clicks on that button then the FirstPresenter will popup a message.
So, here is what I did, I used eClipse to create an event name MyEvent, the eclipse generated a class MyEvent.java
On the SecondPresenter, I got:
private EventBus eventBus;
#Inject
public SecondPresenter(final EventBus eventBus, final MyView view,
final MyProxy proxy) {
super(eventBus, view, proxy);
this.eventBus=eventBus;
}
#Override
protected void onBind() {
super.onBind();
passMsgButton.addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
MyEvent myEvent =new MyEvent();
myEvent.setMsg("hello");
SecondPresenter.this.eventBus.fireEvent(myEvent);
}
});
}
On the FirstPresenter, I got:
private final MyHandler myHandler=new MyHandler(){
#Override
public void onMy(
MyEvent event) {
// TODO Auto-generated method stub
Window.alert(event.getMsg());
}};
#Override
protected void onBind() {
super.onBind();
registerHandler(getEventBus().addHandler(MyEvent.getType(), myHandler));
}
If the FirstPresenter is the nested presenter that is embedded inside the SecondPresenter then that above code works fine. But if they are 2 separated Presenters then the above code didn't work.
Why? I checked Google doc & they just say that EventBus can call a Presenter, they didn't say it must be the nested presenter so I assumed that EventBus can call any separated presenter.
I changed private EventBus to public EventBus, but it didn't help.
What am I missing?
EDIT:
I changed my code a bit, it works partially. I am not sure i'm doing right thing since it still has some issues.
Ok, on the FirstPresenter I implements MyHandler & Override onMy method
public class FirstPresenter extends
Presenter<FirstPresenter.MyView, FirstPresenter.MyProxy> implements MyHandler{
private final MyHandler myHandler=new MyHandler(){
#Override
public void onMy(
MyEvent event) {
// TODO Auto-generated method stub
Window.alert(event.getMsg());
}};
#ProxyEvent
#Override
public void onMy(MyEvent event) {
Window.alert(event.getMsg());
getView().getHtmlPanel().add(new Label("test"));
}
#Override
protected void onBind() {
super.onBind();
registerHandler(getEventBus().addHandler(MyEvent.getType(), myHandler));
}
}
When I click the button on SecondPresenter (a page on a 2nd tab of Webbrowser) then I saw the message popup on the 2nd tab (i.e. the browser didn't make the 1st tab on focus), but when I click on the FirstPresenter (a page on 1st tab) I didn't see the Label("test")?
Why it can call the Window.alert but didn't add the Label onto the FirstPresenter?
Also, how can I get the Browser to set focus on the FirstPresenter (i.e. show the the first tab)?
Am I missing something?
The problem is that if you have two top level presenters only one is active at any time.
I guess that the onBind() method of the FirstPresenter hasn't been called and thus the handler hasn't been attached to the Event on the EventBus.
It works with nested presenters because there both Presenters are "active" at the same time.
You have to rely on ProxyEvent to "wake up the FirstPresenter
I have implemented the suggestion in this post and disable my button after the the first click. How to prevent DoubleSubmit in a GWT application?
What i want to know is with my button reenabled in both my fail and success methods will it always get reenabled. Also is there any place i can put some code that i always want executed when the server replies as opposed to duplicating it in both fail and success methods
I do think that either onSuccess() or onFailure() will be called every time.
As for having a place where you can put code that will always run when getting a response to the server you could just create an AsyncCallback which has the code in it's onFailure() and onSuccess() methods. Then you can just extend that AsyncCallback everytime you create an AsyncCallback.
public MyAsyncCallback<T> extends AsyncCallback<T>(){
#Override
public void onFailure(Throwable caught){
//Do something
onResponse()
failed(caught);
}
#Override
public void onSuccess(T result){
//Do something
onResponse()
succeeded(result);
}
public void onResponse(){
// Do something or nothing by default
}
public abstract void failed(Throwable caught);
public abstract void succeeded(T result);
};
Whenever you want to create an AsyncCallback just use MyAsyncCallback:
AsyncCallback callback = new MyAsyncCallback(){
#Override
public void failed(Throwable caught){
//Do something
}
#Override
public void succeeded(T result){
//Do something
}
// Optionally override onResponse() if needed
#Override
public void onResponse(){
//Do something
}
}
I have a composite widget that contains many buttons (eg. Button1, Button2, etc). I can't figure how to expose button click events on the composite widget. I'm trying to avoid creating custom events such as Button1ClickEvent and Button2ClickEvent and instead reuse the existing GWT ClickEvent for both of them. The following code snippet can give an idea what I'm trying to do:
public class WidgetWithTwoButtons extends Composite {
...
#UiField Button button1;
#UiField Button button2;
#UiHandler("button1")
void onButton1Click(ClickEvent event) {
// TODO fire click event on Button1ClickHandler
}
public HandlerRegistration addButton1ClickHandler(ClickHandler handler) {
return addDomHandler(handler, ClickEvent.getType());
}
#UiHandler("button2")
void onButton1Click(ClickEvent event) {
// TODO fire click event on Button2ClickHandler
}
public HandlerRegistration addButton2ClickHandler(ClickHandler handler) {
return addDomHandler(handler, ClickEvent.getType());
}
}
I think this is not the best practice. I'd appreciate if you give pointers to recommended solutions/examples to this problem in your answers. Thanks!
I started thinking about your problem and first noticed instead of using addDomHandler you can change your current addButton1ClickHandler(ClickHandler handler) simply to :
public HandlerRegistration addButton1ClickHandler(ClickHandler handler) {
return button1.addClickHandler(handler);
}
If you had only one button, you could expose it by making your Composite implement HasClickHandlers and implement the below method to pass it through to your button:
#Override
public HandlerRegistration addClickHandler(ClickHandler handler) {
return button1.addClickHandler(handler);
}
But since you have many buttons, you need a method that takes the button you want to add your clickHandler to as a parameter such as :
public HandlerRegistration addClickHandlerToButton(ClickHandler handler, Button target){
return target.addClickHandler(handler);
}
The problem with this approach is, to use this you need to have a reference to your buttons which means you need to define getter methods for your buttons in your composite such as public Button getButton1() . When you expose your buttons like this, the problem is one does not need a passthrough method on the composite anymore since he/she can directly access buttons anyway making the above approach that takes a target button as parameter obsolete. Worst part is he/she can even change the styles and even detach those buttons.
To solve this you can expose your buttons over the HasClickHandlers interface.
So IMHO this is how i beleive it should be done:
public class ComplexComposite extends Composite {
private Button button1 = new Button("btn1");
private Button button2 = new Button("btn2");
public ComplexComposite(){
HorizontalPanel panel = new HorizontalPanel();
panel.add(button1);
panel.add(button2);
initWidget(panel);
}
public HasClickHandlers getButton1(){
return button1;
}
public HasClickHandlers getButton2(){
return button2;
}
}
With this approach you expose your buttons only over the desired interface and one can add
click handlers such as :
ComplexComposite composite = new ComplexComposite();
composite.getButton1().addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
...
}
});
I can't make a custom event be received in the Activity. Can anyone please tell me what am I missing? I am using GWT 2.1, MVP pattern and UiBinder.
Here's a sample of what I wrote:
Let's say I have MyCustomEvent class and its handler interface MyCustomEventHandler with its onMyCustomEvent(MyCustomEvent event) method.
I implement the handler interface in the Activity:
class MyActivity extends AbstractActivity implements MyCustomEventHandler {
....
public void onMyCustomEvent(MyCustomEvent event) {
doWhatYouKnow();
}
//EventBus is injected with GIN
public void start(AcceptsOneWidget container, EventBus eventBus) {
...
eventBus.addHandler(MyEvent.TYPE, this);
}
}
Now, the sending part in the view:
public class MyWidget extends Composite {
final PopUpPanel myPopUp;
public MyWidget() {
initWidget(uiBinder.createAndBindUi(this));
myPopUp.addCloseHandler(new CloseHandler<PopupPanel>() {
#Override
public void onClose(CloseEvent<PopupPanel> event) {
MyEvent event = new MyEvent();
fireEvent(event);
}
});
}
}
No exception are thrown and unfortunately onMyCustomEvent is never called in the MyActivity class. Any idea? Thanks a million.
#MyWidget
you can make the constructor take parameter ( eventBus )
which you can pass this from class MyActivity
so when you fire the event #MyActivity
the action will be executed #MyWidget
try this , i think it will work .
I think one of your comments is pointing you in the right direction here. What I'm going to guess is going on is that you have more than one EventBus floating around (there should usually only be one event bus per application).
First of all, make sure the EventBus in your Gin module is bound in the Singleton scope. Also, make sure this is the event bus that you pass in to your PlaceController, and not one you're constructing on your own.
Also, I wouldn't be too worried about the fact that your object is a ResettableEventBus in one place. I believe that's just an object that's created by the Activities/Places framework that just wraps the EventBus object you give it.