I have a Javafx application which is to be deployed in Android. My app needs to get the latitude and longitude but I dont know how to implement them. Can someone give me a code sample on how to do it in JavaFX?
UPDATED:
package com.gpstracker;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class GPSTracker extends Application {
#Override
public void start(Stage stage) {
TextField t1 = new TextField();
TextField t2 = new TextField();
Button button = new Button("Show Location");
button.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event) {
PositionService positionService = PlatformFactory.getPlatform().getPositionService();
Position position = positionService.getPosition();
}
});
VBox root = new VBox(20);
root.setPadding(new Insets(20));
root.getChildren().addAll(t1, t2, button);
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(root, visualBounds.getWidth(), visualBounds.getHeight());
stage.setScene(scene);
stage.show();
}
}
Gloun cant seem to find a class call PositionService and Position.
With Gluon Mobile, the current position of a device can be tracked with the PositionService. A sample code snippet:
PositionService positionService = PlatformFactory.getPlatform().getPositionService();
Position position = positionService.getPosition();
System.out.println("Current GPS position: " + position.getLatitude() + "," + position.getLongitude());
This can be achieved via the Gluon Mobile library.
http://gluonhq.com/products/mobile/
To use PositionService, mentioned by Joeri, you have to add a dependency to Gluon open source charm-down library. Here is a sample configuration for your build.gradle:
dependencies {
compile 'com.gluonhq:charm-down-common:2.0.0';
desktopRuntime 'com.gluonhq:charm-down-desktop:2.0.0';
androidRuntime 'com.gluonhq:charm-down-android:2.0.0';
iosRuntime 'com.gluonhq:charm-down-ios:2.0.0';
}
Related
I'm writing script to scroll mobile application up and down but Appium studio code with testng framework not working in Eclipse.
I'm new with this kind of script and testing please help to fix it.
I tried Appium studio code.
but it need to fix to run script.
//package <set your test package>;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.TouchAction;
import java.time.Duration;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.By;
import org.testng.annotations.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.logging.Level;
public class scrollandsave {
private String reportDirectory = "reports";
private String reportFormat = "xml";
private String testName = "scrollandsave";
protected AndroidDriver<AndroidElement> driver = null;
DesiredCapabilities dc = new DesiredCapabilities();
#BeforeMethod
public void setUp() throws MalformedURLException {
dc.setCapability("reportDirectory", reportDirectory);
dc.setCapability("reportFormat", reportFormat);
dc.setCapability("testName", testName);
dc.setCapability(MobileCapabilityType.UDID, "330033acecf394bd");
driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), dc);
driver.setLogLevel(Level.INFO);
}
#Test
public void testscrollandsave() {
new TouchAction(driver).press(480, 1348).waitAction(Duration.ofMillis(624)).moveTo(338, 312).release().perform();
new TouchAction(driver).press(591, 1376).waitAction(Duration.ofMillis(592)).moveTo(421, 138).release().perform();
driver.findElement(By.xpath("(//*[#id='listAllImgByCat']/*/*/*/*[#id='icPlayVideo'])[1]")).click();
new WebDriverWait(driver, 30).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#text='EDIT']")));
driver.findElement(By.xpath("//*[#text='EDIT']")).click();
new WebDriverWait(driver, 30).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#text='SAVE']")));
driver.findElement(By.xpath("//*[#text='SAVE']")).click();
driver.findElement(By.xpath("//*[#text='Export as GIF']")).click();
driver.findElement(By.xpath("//*[#id='btnHome']")).click();
}
#AfterMethod
public void tearDown() {
driver.quit();
}
}
I expect screen should scroll down whenever I execute above code.
you can try this code driver.executeScript("seetest:client.swipe(\"Down\", 1000, 500)");
. This code would swipe from Down, from the offset 1000 for 500ms.
Visit here for more information
I am using ControlsFX - HiddenSidesPane where i add some link(ToggleButtons) to be clicked for navigation.
The problem i have is whenever anything is clicked, the HiddenSiddesPane hides.
The desired behavior is when anything inside it is clicked it should not close/hide, unless cursor hovers out of it.
SSCCE to demonstrate undesired behavior
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.controlsfx.control.HiddenSidesPane;
public class MyHiddenSidesPaneDemo extends Application{
public static void main(String[] args) { Application.launch(args); }
#Override
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox();
TableView tv = new TableView();
HiddenSidesPane hiddenSidesPane = new HiddenSidesPane();
hiddenSidesPane.setContent(tv);
hiddenSidesPane.setLeft(new ListView());
root.getChildren().addAll(hiddenSidesPane);
primaryStage.setTitle("HiddenSidesPane Example Demo");
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Try the following:
ListView listView = new ListView();
hiddenSidesPane.setLeft(listView);
listView.setOnMouseEntered(e->hiddenSidesPane.setPinnedSide(Side.LEFT)); //Keep left side pinned
listView.setOnMouseExited(e->hiddenSidesPane.setPinnedSide(null)); //unpin when mouse exits
I've been working on this program using NetBeans for 6 months and I've never experienced such an issue. It has began when I added org.apache.commons library (I needed it to get time from a online server) , when I run the program ,sometimes everything goes fine and sometimes it hangs on running state creating a process in the background. Here's the Class with the main method.
package samplefx.ctrl;
import java.io.IOException;
import java.net.InetAddress;
import java.sql.Date;
import java.text.ParseException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
public class Optilight extends Application {
public static EntityManagerFactory emf;
EntityManager em;
#Override
public void start(Stage primaryStage) throws IOException, ParseException {
emf = Persistence.createEntityManagerFactory("SampleFXPU");
String TIME_SERVER = "time-a.nist.gov";
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
Date time = new Date(returnTime);
Access access = new Access();
String fix = "XC193283R";
String var = access.Y();
Date start = Date.valueOf("2016-08-23");
Date end = Date.valueOf("2016-08-31");
Boolean j = false;
if (start.before(time) && time.before(end)) {
j = true;
}
boolean i = false;
if (fix.equals(var)) {
i = true;
}
if (!j || !i) {
System.exit(0);
}
// load main form in to VBox (Root)
BorderPane mainPane = (BorderPane) FXMLLoader.load(getClass().getResource("/samplefx/view/Login.fxml"));
// add main form into the scene
Scene scene = new Scene(mainPane);
//primaryStage.setTitle("Optilight 2.2.1");
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.UNDECORATED);
//primaryStage.setMaximized(true); // make the main form fit to the screen
primaryStage.show();
primaryStage.setOnCloseRequest(e -> {
emf.close();
Platform.exit();
System.exit(0);
});
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
I finally found the answer to this, it's a server timeout issue . I added these two lines :
timeClient.open();
timeClient.setSoTimeout(5000);
after this line :
NTPUDPClient timeClient = new NTPUDPClient();
Is there any event handler present in Java FX, if i close a window directly bt pressing [X] button on Top right side.
Which events gets fire in this case ?
Nothing is working so far , neither setOnHiding not setOnCloseRequest()
Please help.
Try this one
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class Main extends Application {
#Override
public void start(Stage stage) {
Text text = new Text("!");
text.setFont(new Font(40));
VBox box = new VBox();
box.getChildren().add(text);
final Scene scene = new Scene(box,300, 250);
scene.setFill(null);
stage.setScene(scene);
stage.show();
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent we) {
System.out.println("Stage is closing");
}
});
stage.close();
}
public static void main(String[] args) {
launch(args);
}
}
Source Stage close event : Stage « JavaFX « Java
The circle should go back and forth but the start button is not starting it for some reason? When I ran the example file it worked and it looks exactly like mine so not sure why Mine isnt starting. Does anybody know why? Thanks
package projavafx.metronometransition.ui;
import javafx.animation.Animation;
import javafx.animation.Interpolator;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class MetronomeTransitionMain extends Application{
Button startButton;
Button pauseButton;
Button resumeButton;
Button stopButton;
Circle circle;
public static void main(String[] args) { Application.launch(args);}
#Override
public void start(Stage stage) {
circle = new Circle(100, 50, 4, Color.BLUE);
TranslateTransition anim = new TranslateTransition(new Duration(1000.0), circle);
anim.setFromX(0);
anim.setToX(200);
anim.setAutoReverse(true);
anim.setCycleCount(Animation.INDEFINITE);
anim.setInterpolator(Interpolator.LINEAR);
startButton = new Button("start");
startButton.setOnAction(e -> anim.playFromStart());
pauseButton = new Button("pause");
startButton.setOnAction(e -> anim.pause());
resumeButton = new Button("resume");
resumeButton.setOnAction(e -> anim.play());
stopButton = new Button("stop");
stopButton.setOnAction(e -> anim.stop());
HBox commands = new HBox(10, startButton,
pauseButton,
resumeButton,
stopButton);
commands.setLayoutX(60);
commands.setLayoutY(420);
Group group = new Group(circle, commands);
Scene scene = new Scene(group, 400, 500);
startButton.disableProperty().bind(anim.statusProperty()
.isNotEqualTo(Animation.Status.STOPPED));
pauseButton.disableProperty().bind(anim.statusProperty()
.isNotEqualTo(Animation.Status.RUNNING));
resumeButton.disableProperty().bind(anim.statusProperty()
.isNotEqualTo(Animation.Status.PAUSED));
stopButton.disableProperty().bind(anim.statusProperty()
.isEqualTo(Animation.Status.STOPPED));
stage.setScene(scene);
stage.setTitle("Metronome using TranslateTransition");
stage.show();
}
}