JavaFX 2 Drag ImageView to a Button - java

All I want is to capture when I drop ImageView on Button:
I tried to the following:
button.setOnDragOver(event -> {
System.out.println("drop");
});
button.setOnMouseDragReleased(event -> {
System.out.println("drop");
});
And nothing seems to work, any ideas?

Try The following code to create an event handler for handling event of dragging the image over the button:
button.setOnDragOver(new EventHandler() {
#Override
public void handle(final DragEvent event) {
mouseDragOver(event);
}
});
Then declare the event method which needs to be executed when event occurs
private void mouseDragDropped(final DragEvent e) {
System.out.println("drop");
}

Related

2 OnClickListeners for one Button cannot resolve the OnClick Listener

I added the following lines of Code into my OnCreate method.My goal is to assign a button to two functions and to call them up alternately. With the first click the text of the button should be changed and the EditText should be editable. At the second click, the fields should no longer be editable and the button text should change to the first alternative. I have implemented two OnClickListeners and the program structure seems logical to me. Nevertheless, I get an error message; "Cannot resolve symbol onClickListener". What can I do to get the setup described above up and running? Thanks for all responses!
private Button ProfilUpdate;
ProfilUpdate=findViewById(R.id.buttonProfilUpdate);
.
.
.
.
final ProfilUpdate.OnClickListener listener2 = new View.OnClickListener() {
#Override
public void onClick(View v) {
ProfilUpdate.setText("Profil bearbeiten");
profilVorname.setFocusable(false);
}
};
ProfilUpdate.OnClickListener listener1 = new View.OnClickListener() {
#Override
public void onClick(View v) {
ProfilUpdate.setText("Ă„nderungen speichern");
profilVorname.setFocusable(true);
v.setOnClickListener(listener2);
}
};
ProfilUpdate.setOnClickListener(listener1);
why don't you create a boolean isFirstClick = true , and then check it in the same listener
ProfilUpdate.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isFirstClick){
//Do the job for the first click process
isFirstClick= false;
}else {
//Do the job for the second click process
isFirstClick= true;
}
}
};
ProfilUpdate.setOnClickListener(listener);
There can only be one click listener on one view at a time. Use ProfileUpdate.setOnClickListener(listener object). To get the alternate functionality, you can define a Boolean to keep track of the state, for the example, define a class variable at the top Boolean shouldChangeText = true, and in the onClick body in the listener, do something like:
If (shouldChangeText) { // change the text
}
else { // clear the text
}
shouldChangeText = !shouldChangeText

Programming a Button In Android Java to simulate HW Keyboard

I am writing an Android app in Java and want navigation button at the bottom (up, down, left, right, enter, escape (back), and brightness control). My button presses look like this
public void upButtonPressed(View view)
{
BaseInputConnection bic = new BaseInputConnection(view, true);
bic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP));
}
I have also tried
public void upButtonPressed(View view)
{
new Thread(new Runnable() {
#Override
public void run() {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);
}
});
}
Any idea where I am going wrong? Also, yes I need this, and yes the program works on a VM with my HW keyboard.
You can use dispatchKeyEvent() to dispatch a KeyEvent to a View for it to handle.
So, if you want the view to handle the KeyEvent, then,
public void upButtonPressed(View view)
{
view.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP));
}
Else, if you want the Activity to handle the KeyEvent, then,
public void upButtonPressed(View view)
{
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP));
}

How To Change The Bitmap Resource On TouchEvent

This Should be easy and simple right?!
So, i have a Gameobject as a Button i reference it on a GamePanel that extents SurfaceView like this :
bby = new Buttons(BitmapFactory.decodeResource(getResources(), R.drawable.babybutton),123 ,114 ,1);
bby is drawn and work fine,
my problem is How can i change the Resource (R.drawble.babybutton) to a different resource for e.g (R.drawble.babybutton2) on Touch?
just like pressing buttons animations!
Thanks in advance
(If my question look stupid please don't dislike! i'm very new to this).
if you want to change the button's drawable on touch on the surfaceview. than you can override this method in your surfaceview class.
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//here you can set an another drawable to your button or can do something else
break;
}
return true;
}
In your activity set a touchListener on the surfaceview.
after initialize the surfaceView. pass this surfaceview to the
surfaceView.setOnTouchListener(surfaceView)
I may have misunderstood you, but if you want to change the button's rescource, this is how you should do it:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
runOnUiThread(new Runnable() {
#Override
public void run() {
button.setBackgroundResource(R.drawable.icon);
}
});
}
});

How to disable setOnMouseClicked while scrolling in JavaFX ListView

I developed a small Javafx application and deployed in my Android device, I have a ListView which is configured something like this:
stuboutList.setOnMouseClicked(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent event) {
Dialog.show("You click the ListView!");
}
});
Here's the problem: Every time I scroll the ListView the Dialog will keep on popping.
QUESTION: How to disable setOnMouseClicked while SCROLLING?
When you scroll a ListView the swipe gesture triggers a Mouse Drag Event. You can set a flag, when the drag event is detected, and consume the following Mouse Clicked Event.
public class ScrollListener {
private BooleanProperty scrolling;
public ScrollListener(Node observableNode) {
scrolling = new ReadOnlyBooleanWrapper(false);
observableNode.addEventHandler(MouseEvent.DRAG_DETECTED, e -> scrolling.set(true));
observableNode.addEventFilter(MouseEvent.MOUSE_CLICKED, evt -> {
if (scrolling.get()) {
scrolling.set(false);
evt.consume();
}
});
observableNode.addEventHandler(MouseEvent.MOUSE_EXITED, e -> scrolling.set(false));
}
public ReadOnlyBooleanProperty scrollingProperty() {
return scrolling;
}
public boolean isScrolling() {
return scrolling.get();
}
}
Another possiblity is to you use Gluon's CharmListView, which takes care of the Mouse Click Event on its own, but (until now) is not as conveniend to use as the standard ListView, e.g. when you need to access the SelectionModel, as you can see in this question: CharmListView SelectedItemProperty?

How can I make a piece of code loop while a Button is pressed and stop it once the Button is no longer Pressed?

I am using the android developer tools in Eclipse, programming in Java, and I need to make an object move across the screen as long as a button is pressed. I've been doing research for hours, and I cannot find any methods to accomplish this. I've tried running threads, which often crash or seemingly don't execute. I've also tried an onClickListener which reads the button state and uses it to determine whether or not the button is still pressed. I'm currently using a while loop, but this just freezes the program. I believe that this is the best method, and I've tried to use Thread.sleep in order to limit the number of iterations per second, as I believe that this is the reason it is freezing. Am I on the right track or am I way off in left field? Here is a snippet of code:
rightButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
while(arg0.isPressed())
{
mover.updateCoordinates(1, 0);
}
}
});
Would you try this another method?
Firstly declare your button as class variable, declare a Handler and a Runnable:
private Button rightButton; // You will assign this in onCreate() method
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
if(rightButton.isPressed())
{
// If press state is pressed, move your item and recall the runnable in 100 milliseconds.
mover.updateCoordinates(1, 0);
mHandler.postDelayed(mRunnable, 100);
}
}
};
Then your button's onClickListener will looks like this:
rightButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0)
{
// Instead of performing a loop here, just call a runnable, do simple press state checking there.
mHandler.postDelayed(mRunnable, 100);
}
});
Create a loop that updates the views, waits, and calls itself after it finishes waiting. Within the loop, have an animation if statement with a boolean field that move on true and does not move on false. Have the onClick method toggle the boolean field's value.
Try:
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
// Start your animation here
} else if (e.getAction() == MotionEvent.ACTION_UP) {
// Stop your animation here
}
return false;
}
});
References:
MotionEvent
OnTouchListener
Alternatively, override onKeyUp and onKeyDown of the View class and check for the KEYCODE_ENTER KeyEvent

Categories