I don't know how to pause an event handler in the middle, I don't even know if it is possible.
Infact, in the code attached, I would like that :
After the player clicked on the cell, the UI updates the cells according to what the player clicked. Then, the UI pauses for like 2 seconds. And the computer plays after that, updating the cells.
Thank you very much for your answer, I am quite desperate ;)
for (Cell cell : cells) {
if (cell.getHouseNumber() >= 6) {
cell.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
cell.setFill(Color.WHITE);
System.out.println(cell.getHouseNumber() - 6);
if(player1.sow(cell.getHouseNumber() - 6)){
for (int i = 0; i < cells.length; i++) {
if (i < 6) {
cells[i].setSeedsNumber(player1.getSeeds(cells[i].getHouseNumber()));
try {
cells[i].setImage();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
cells[i].setSeedsNumber(player1.getSeeds(cells[i - 6].getHouseNumber()));
try {
cells[i].setImage();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
southBoard.setSeedsNumber(player1.getPlayerScore());
playerTurnLabel.setText("Computer playing");
//PAUSE THE UI FOR 2s
player2.computerMove();
for (int i = 0; i < cells.length; i++) {
if (i < 6) {
cells[i].setSeedsNumber(player1.getSeeds(cells[i].getHouseNumber()));
try {
cells[i].setImage();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
cells[i].setSeedsNumber(player1.getSeeds(cells[i - 6].getHouseNumber()));
try {
cells[i].setImage();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
playerTurnLabel.setText("Your turn to play");
}else{
playerTurnLabel.setText("Move prohibited ...");
}
}
});
}
}
You can do something like
southBoard.setSeedsNumber(player1.getPlayerScore());
playerTurnLabel.setText("Computer playing");
playerTurnLabel.getScene().getRoot().setDisable(true);
PauseTransition pause = new PauseTransition(Duration.seconds(2));
pause.setOnFinished(event -> {
player2.computerMove();
// etc etc
playerTurnLabel.getScene().getRoot().setDisable(false);
});
pause.play();
So you basically move all the code you want executed after the pause to the onFinished handler. You might or might not need the UI disabled while it is paused; you can remove the calls to setDisable(...) if you don't need them.
Related
i have multiple items in my array list and i want to set progress for each of these items but each of them have the same progress please help me with this issue
here is how im setting my progress bar in the recycler viwe
Thread thread = new Thread(){
#Override
public void run() {
super.run();
int counter=0;
for(counter=0;counter<=100;counter++) {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
holder.progressBar.setProgress(counter);
counter = counter + 10;
}
}
};
thread.start();
I need my web view to be tappable and scrolable. Once I implement on touch the scroll doesnt work. This way i managed to get it working however now i dont know how to make the web view tappable? the ButtonPress does nothing and if i use Move then i am just scrolling
This my my render in mu droid project
class ExtendedWebViewClient : WebViewClient
{
WebView _webView;
public async override void OnPageFinished(WebView view, string url)
{
try
{
_webView = view;
if (_xwebView != null)
{
view.Settings.JavaScriptEnabled = true;
await Task.Delay(100);
string result = await _xwebView.EvaluateJavaScriptAsync("(function(){return document.body.scrollHeight;})()");
_xwebView.HeightRequest = Convert.ToDouble(result);
}
base.OnPageFinished(view, url);
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}");
}
}
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
{
return true;
}
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
_xwebView = e.NewElement as ExtendedWebView;
_webView = Control;
if (e.OldElement == null)
{
_webView.SetWebViewClient(new ExtendedWebViewClient());
}
if (e.OldElement != null)
{
Control.Touch -= ControlOnTouch;
Control.ScrollChange -= ControlOnScrollChange;
}
if (e.NewElement != null)
{
Control.Touch += ControlOnTouch;
Control.ScrollChange += ControlOnScrollChange;
}
}
private void ControlOnScrollChange(object sender, ScrollChangeEventArgs scrollChangeEventArgs)
{
if (scrollChangeEventArgs.ScrollY > 0 && scrollChangeEventArgs.OldScrollY == 0)
{
Control.Parent.RequestDisallowInterceptTouchEvent(true);
}
}
private void ControlOnTouch(object sender, Android.Views.View.TouchEventArgs e)
{
// Executing this will prevent the Scrolling to be intercepted by parent views
switch (e.Event.Action)
{
case MotionEventActions.Down:
Control.Parent.RequestDisallowInterceptTouchEvent(true);
break;
case MotionEventActions.Up:
Control.Parent.RequestDisallowInterceptTouchEvent(false);
break;
case MotionEventActions.ButtonPress:
Console.WriteLine("press");
break;
case MotionEventActions.Mask:
Console.WriteLine("mask");
break;
}
// Calling this will allow the scrolling event to be executed in the WebView
Control.OnTouchEvent(e.Event);
}
Instead of using the gesture recognizer on your webview, you can use the Focused event like following . It will been invoked when you tap the WebView .
var wb = new WebView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
Source = "xxx",
};
wb.Focused += (sender, event) =>
{
//Handle your logic here!
wb.Unfocus();
};
Unfocus() is used if you want to implement your logic everytime the webview is tapped.
I am trying to bind two separate event to scene in javafx .
scene.addEventHandler(MouseEvent.MOUSE_RELEASED, new EventHandler<Event>() {
#Override
public void handle(Event event) {
......Drag Content Task.......
}
});
So How can get this two Events any Suggestion.
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if(event.getClickCount() == 2){
System.out.println("Call Dbl Click Eve");
Zooming....Task.....
}
});
I wrote some code and it works for me !
Using Current Time calculate time betwwen two click events if it less than dblclick expected then consider double click..
scene.addEventHandler(MouseEvent.ANY, e -> {
if (e.getEventType().equals(MouseEvent.DRAG_DETECTED)) {
System.out.println("Drag Event call ");
drag_Flag=true;
}
if (e.getEventType().equals(MouseEvent.MOUSE_RELEASED) && drag_Flag) {
System.out.println("Rellease Event call");
drag_Flag=false;
System.out.println("Drag Event Flag : "+drag_Flag);
time1=0;
time2=0;
} else if (e.getEventType().equals(MouseEvent.MOUSE_CLICKED) && !drag_Flag) {
long diff = 0;
if(time1==0)
time1=System.currentTimeMillis();
else
time2=System.currentTimeMillis();
if(time1!=0 && time2!=0)
diff=time2-time1;
if((diff/1000)<=215 && diff>0)
{
isdblClicked=true;
}
else
{
isdblClicked=false;
}
System.out.println("IsDblClicked()"+isdblClicked);
if(isdblClicked==true)
{
ZoomInOutPojo z = null;
do_Zoom();
time1=time2=0;
isdblClicked=false;
}
else
{
statement1;
}
}
drag_Flag=false;
}
});
Solution Works for My Case.
static boolean drag_Flag=false
scene.addEventHandler(MouseEvent.ANY, e -> {
if (e.getEventType().equals(MouseEvent.DRAG_DETECTED)) {
System.out.println("Drag Event call ");
drag_Flag=true;
}
if (e.getEventType().equals(MouseEvent.MOUSE_RELEASED) && drag_Flag) {
System.out.println("Rellease Event call");
drag_Flag=false;
System.out.println("Drag Event Flag : "+drag_Flag);
time1=0;
time2=0;
} else if (e.getEventType().equals(MouseEvent.MOUSE_CLICKED) && !drag_Flag) {
long diff = 0;
if(time1==0)
time1=System.currentTimeMillis();
else
time2=System.currentTimeMillis();
if(time1!=0 && time2!=0)
diff=time2-time1;
if((diff/1000)<=215 && diff>0)
{
isdblClicked=true;
}
else
{
isdblClicked=false;
}
System.out.println("IsDblClicked()"+isdblClicked);
if(isdblClicked==true)
{
ZoomInOutPojo z = null;
do_Zoom();
time1=time2=0;
isdblClicked=false;
}
else
{
statement1;
}
}
drag_Flag=false;
}
});
Well the way you posted it should already work, another way to do it would be:
scene.addEventHandler(MouseEvent.ANY, e -> {
if (e.getEventType().equals(MouseEvent.MOUSE_RELEASED)) {
// do something
} else if (e.getEventType().equals(MouseEvent.MOUSE_CLICKED)) {
// do something else
}
});
You can use the following code too. Try it.
EventHandler<MouseEvent> clickEvent = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if(event.getClickCount() == 2){
System.out.println("Call Dbl Click Eve");
}
}
};
EventHandler<MouseEvent> releaseEvent = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println("Rele");
}
};
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, clickEvent);
scene.addEventHandler(MouseEvent.MOUSE_RELEASED, releaseEvent);
I hope it will help you.
I am confused on how use the buttons. I open the theme designer and create the button, then on the commands i choose the action execute and go to the Source (saving before)
On the code I have this method, on the simulator do nothing and in the device neither (on the simulator start an Open file window, i am using w7)
#Override
protected boolean onMainCommand105() {
try {
Display.getInstance().execute("fb://profile/14846274855890");
} catch (Exception e) {
Dialog.show("Error!", "Failed to start. installed?", "OK", null);
e.printStackTrace();
}
return true;
}
is this the correct way?
I saw my mistake thanks to this CN1 tutorial
http://www.codenameone.com/how-do-i---handle-eventsnavigation-in-the-gui-builder--populate-the-form-from-code.html
I should use Action Event, not execute
#Override
protected void onMain_Button1Action(Component c, ActionEvent event) {
try {
Display.getInstance().execute("https://www.facebook.com/CatedralAltaPatagonia/?fref=ts");
} catch (Exception e) {
Dialog.show("Error!", "Failed to start. installed?", "OK", null);
e.printStackTrace();
}
}
Or easier :
button.addActionListener(l -> {
System.out.println("test");
});
I need to read aad text file and show the content in text area but the problem is that my text file is updating with every second . IS that possible to show the content of text file in text area dynamically i.e., with every second new data is added into text file and i need to show the new data also
this is my code in the controller
public void initialize(URL url, ResourceBundle rb) {
try {
Scanner s = new Scanner(new File("E:\\work\\programming\\NetBeansProjects\\FinalProject\\src\\logs\\diskCheck.txt")).useDelimiter("\\s+");
while (s.hasNext()) {
if (s.hasNextInt()) { // check if next token is an int
diskchecktextarea.appendText(s.nextInt() + " "); // display the found integer
} else {
diskchecktextarea.appendText(s.next() + " "); // else read the next token
}
}
} catch (FileNotFoundException ex) {
System.err.println(ex);
}
}
I've wanted to test the watch feature for a while now, so I took the chance with your question and made you an example. Please adapt the watchPath to your needs. The file doesn't have to exist initially. It'll be found once you create it.
/**
* Documentation: https://blogs.oracle.com/thejavatutorials/entry/watching_a_directory_for_changes
*/
public class WatchFileChanges extends Application {
Path watchPath = Paths.get("c:/temp/watch.txt");
TextArea textArea;
#Override
public void start(Stage primaryStage) throws IOException {
BorderPane root = new BorderPane();
textArea = new TextArea();
root.setCenter(textArea);
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
// load file initally
if (Files.exists(watchPath)) {
loadFile();
}
// watch file
WatchThread watchThread = new WatchThread(watchPath);
watchThread.setDaemon( true);
watchThread.start();
}
private void loadFile() {
try {
String stringFromFile = Files.lines(watchPath).collect(Collectors.joining("\n"));
textArea.setText(stringFromFile);
} catch (Exception e) {
e.printStackTrace();
}
}
private class WatchThread extends Thread {
Path watchPath;
public WatchThread(Path watchPath) {
this.watchPath = watchPath;
}
public void run() {
try {
WatchService watcher = FileSystems.getDefault().newWatchService();
WatchKey key = watchPath.getParent().register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY);
while (true) {
// wait for key to be signaled
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path path = ev.context();
if (!path.getFileName().equals(watchPath.getFileName())) {
continue;
}
// process file
Platform.runLater(() -> {
loadFile();
});
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
} catch (IOException x) {
System.err.println(x);
}
}
}
public static void main(String[] args) {
launch(args);
}
}