GWT Button onClick() return a value - java

I need to create 2 generic button called yes and no with 2 return 0 if no 1 if yes. I see the onclick method is void and not return int, how can i do?
YesButton.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
??? result ??
}
});

That's not the way to do it. Not knowing your specific requirement makes it a little hard, but I'll venture a suggestion. First define a controller/mediator/whachamacallit with the operations that the view can perform:
public interface MyListener
{
void onYesClick();
void onNoClick();
}
(Could be a concrete class also, but yes and no clicking seems very generic, so we could reuse that elsewhere)
In your view class you would then have
public class MyView
{
private MyListener listener;
private Button yesButton = new Button( "yessir!" );
private Button noButton = new Button( "no way!" );
public MyView( MyListener listener ) { this.listener = listener; }
yesButton.addClickHandler( new ClickHandler()
{
#Override
public void onClick( ClickEvent event )
{
listener.onYesClick(); // similarly .onNoClick() for the "No" button
}
} );
// etc
...
}
Hope that helps you a bit further.

Related

Handaling single and double taps android kotlin

I want to display some toast when my button is clicked once and other toast when my button is clicked twice. Is there any built in library or do I need to implement custom logic?
You can Gesture Detector for detecting double taps.
final GestureDetector mDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener {
#Override
public boolean onDown(MotionEvent e) {
return true;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}
});
For more details you can check this documentation.
Answer given by #TaranmeetSingh is absolutely right but there is a better solution. You can use this library to do it.
How to implement
Add this library -> implementation 'com.github.pedromassango:doubleClick:CURRENT-VERSION'(Version is given above).
Instead of using an onClickListenerObject for onClickListener, you can use DoubleClickListener Check the example below
Button btn = new Button(this);
btn.setOnClickListener( new DoubleClick(new DoubleClickListener() {
#Override
public void onSingleClick(View view) {
// Single tap here.
}
#Override
public void onDoubleClick(View view) {
// Double tap here.
}
});

Implementing callback in extendable button class

I have a very simple 'CustomButton' class which extends the default 'Button' class. My CustomButton uses onTouchEvent and I want to pass down a function from my Activity to the CustomButton and get it executed on touch down.
The CustomButton class is working fine, but I can't seem to figure out how to pass down a function to it.
Activity:
public class mainActivity extends Activity
{
#Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
Context context = getApplicationContext();
setContentView( R.layout.main );
LinearLayout root = (LinearLayout) findViewById( R.id.myLayout );
View child1 = getLayoutInflater().inflate( R.layout.child, null );
// Define the button
final CustomButtom myCustomButton = (CustomButtom)child1.findViewById( R.id.button_id );
myCustomButtom.setCallback( test ); // <-- I want to pass my 'test' function to CustomButton class,
// so it can get executed by the onTouchEvent
root.addView( myCustomButton );
super.onCreate( savedInstanceState );
}
private int test()
{
Log.d( "test", "Callback executed!" );
}
}
And this is my CustomButton class:
public class CustomButtom extends Button
{
private Function callback;
public CustomButtom(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnTouchListener
(
new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
executeCallback(); // <-- My callback would get executed from here
}
return true;
}
}
);
}
public void setCallback(Function function)
{
callbackFunction = function; // Save the callback in a local variable
}
private boolean executeCallback()
{
return callbackFunction.execute(); // execute the callback
}
}
Is there a 'data type' such as 'Function' which I can use for this purpose or is there different way how to accomplish this? Thank you!
You usually need an object and a function when you want to perform an action in Java. This is what Interfaces are for. You declare an interface with the name of your function. Your Activity implements this interface and the function and then you pass to the button the instance of your activity. In your case you are probably looking for OnTouchListener or OnClickListener? If you want to have a special interface, you can declare it the same way.
public class mainActivity extends Activity implements OnClickListener
{
#Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
// I do not see any reason to use the Application Context here. Your Activity has the right context for your UI
// Context context = getApplicationContext();
setContentView( R.layout.main );
LinearLayout root = (LinearLayout) findViewById( R.id.myLayout );
View child1 = getLayoutInflater().inflate( R.layout.child, null );
// Define the button
final CustomButtom myCustomButton = (CustomButtom)child1.findViewById( R.id.button_id );
myCustomButtom.setOnClickListener(this);
root.addView( myCustomButton );
// you did this already
//super.onCreate( savedInstanceState );
}
public void onClick(View v)
{
Log.d( "test", "Callback executed!" );
}
}
EDIT:
to keep the logic inside the button you can create your own interface (OnClickListener is an interface) like this:
public interface OnCustomActionListener {
// you can remove the button as parameter if you do not care which button the action came from
void onCustomAction(CustomButton button);
}
public class CustomButtom extends Button {
OnCustomActionListener onCustomActionListener;
public void setOnCustomActionListener(OnCustomActionListener listener) {
this.onCustomActionListener = listener;
}
/* Creator like in your question mentioned */
private boolean executeCallback() {
if (this.onCustomActionListener != null) {
this.onCustomActionListener.onCustomAction(this);
}
}
}
in your Activity:
public class mainActivity extends Activity implements OnCustomActionListener {
...
myCustomButtom.setOnCustomActionListener(this);
...
public void onCustomAction(CustomButton button) {
// do something
}

GWT Mvp and handlers

I've been learning GWT the past couple months and found out that the Mvp is one of the best ways to design your project. I've read google's tutorial MVP part 1
and in their tutorial they put the clickHandlers ( for example) in the presenter.
Now I had problems with that when constructing many view class that has many buttons with the same HTML id, and then the user interacts with these buttons... so if I have one button for every view, total 6 button. and the user clicks on one of them, the button will work 6 times for the same object...
So I read and found out that it is better to put the handlers on the view class and create an event to the presenter.
So that what I did :
View Class :
rb0.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
selectHandler.onEvent(1);
System.out.print("rate 1");
}
});
rb1.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
selectHandler.onEvent(2);
System.out.print("rate 2");
}
});
rb1.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
selectHandler.onEvent(3);
System.out.print("rate 3");
}
});
rb1.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
selectHandler.onEvent(4);
System.out.print("rate 4");
}
});
rb1.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
selectHandler.onEvent(5);
System.out.print("rate 5");
}
});
Presenter class : (event handler)
private void bind() {
.
.
.
DoEvent selectHandler = new DoEvent(){
public void onEvent(int select) {
fetchRating(select, user.getUserId());
}
};
display.setSelectHandler(selectHandler);
The call for the Presenter with it's view, it is called from the MainPagePresenter class :
presenter = new AssetViewPresenter(rpcService,eventBus,new AssetView(),result.get(i));
now my problem is that when I click the buttons from the view nothing happens... like the presenter and the view are not connected, what could be the problem ?
Sorry it was a stupid bug... my button are rb0, rb1 ,rb2 ... and I copied the click handlers with the same button id !!! YUP I'm ashamed of myself

Jface Dialog, How to retrieve correctly what button pressed the user?

I'm having troubles with a custom Dialog in Eclipse.
in the first place, I created a Class that extend Dialog.
public class ModificarGrupoBCDialog extends Dialog {
private static final int CANCELAR = 999;
private static final int MODIFICAR = 1;
...
somewhere I create the buttons...
protected void createButtonsForButtonBar(Composite parent) {
this.createButton(parent, MODIFICAR, "Modificar", true);
this.getButton(MODIFICAR).setEnabled(puedeAltaGrupoBC());
this.bt_ok = this.getButton(MODIFICAR);
this.createButton(parent, CANCELAR, "Cancelar", false);
Display display = window.getShell().getDisplay();
Image image = new Image(display, ModificarGrupoBCDialog.class.getResourceAsStream("/icons/modificar.png"));
this.getButton(MODIFICAR).setImage(image);
image = new Image(display, ModificarGrupoBCDialog.class.getResourceAsStream("/icons/cancelar.png"));
this.getButton(CANCELAR).setImage(image);
}
and when the user clicks...
protected void buttonPressed(int buttonId) {
switch (buttonId) {
case MODIFICAR:
// Some Code, for Change Button
break;
case CANCELAR:
setReturnCode(CANCELAR);
close();
break;
}
Finally, this is how I open and get the returnCode, in the caller object.
...
ModificarGrupoBCDialog modificarGrupoBC = new ModificarGrupoBCDialog(window.getShell(), window, gr_bc);
if (modificarGrupoBC.getReturnCode() == Window.OK) {
//... Some code on OK
} else {
//another code when cancel pressed.
}
;
as you can see, after trying a while, I have to write setReturnCode() in CANCELAR switch block, is that OK ?
I spect that Dialog class automatically asign the correct return code.
May be someone could point me to a good sample.
I'm reading Vogela's blog, and may be the solution is to override okPressed() method ?
Best Regards.
The standard dialog sets the return code in two places:
protected void okPressed() {
setReturnCode(OK);
close();
}
protected void cancelPressed() {
setReturnCode(CANCEL);
close();
}
so your code doing:
setReturnCode(xxxx);
close();
should be fine as long as the button id you are using does not match the Cancel or OK button ids.
You could also use the approach used by MessageDialog which simply does this:
protected void buttonPressed(int buttonId) {
setReturnCode(buttonId);
close();
}

Java SWT: widgetSelected vs widgetDefaultSelected

For example I want to execute something when user clicks on a button. Which do I use? The documentation didn't appear to make it very clear
UPDATE
A quick test shows that Widget Selected is triggered but not Default Selected.
In TasksView.main()
TasksView view = new TasksView(shell, SWT.None);
TasksController controller = new TasksController(view);
In TasksController
public class TasksController extends ControllerAbstract {
protected TasksView view;
public TasksController(TasksView view) {
this.view = view;
view.addTaskListener(new AddTaskListener());
}
protected class AddTaskListener implements SelectionListener {
#Override
public void widgetDefaultSelected(SelectionEvent arg0) {
System.out.println("Default Selected");
}
#Override
public void widgetSelected(SelectionEvent arg0) {
System.out.println("Widget Selected");
}
}
}
btw, Did I do MVC correctly?
Use widgetSelected. In fact, all the better is to simply extend SelectionAdapter and only override the widgetSelected method and completely ignore widgetDefaultSelected.
SelectionListener.widgetDefaultSelected(e) has a toolkit dependent behavior. I usually just invoke SelectionListener.widgetSelected(...). (Note that this is not the default in SelectionAdapter.widgetDefaultSelected(e) - you will have to do this yourself.

Categories

Resources