JavaFX - LineChart horizontal marker issue - java

I'm trying to add a horizontal line to a linechart in JavaFX. Found this post in SO and now trying to modify it to work in my case, however I'm getting an error, and hopefully someone can see what is the problem here.
Here is the controller code:
package gems.view;
public class GUImainController implements Initializable, Observer
{
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
private ArrayList<Float> tempArr = new ArrayList<>();
private ArrayList<String> tempTimeArray = new ArrayList<>();
private Series<String, Number> tempChart = new Series<>();
#FXML
private LineChartWithMarker<String, Number> lcTemperature = new LineChartWithMarker<>(xAxis,yAxis);
#Override
public void initialize(URL arg0, ResourceBundle arg1)
{
yAxis.setPrefWidth(35);
xAxis.setLabel("Temperature \\u00b0");
yAxis.setLabel("Time");
tempChart = new XYChart.Series<String, Number>();
lcTemperature.getData().add(tempChart);
tempArr.add(observable.getLatestMeasurementsFromServer().getTemperature());
tempTimeArray.add(observable.getLatestMeasurementsFromServer().getDateTime().getTime());
for (int i = 0; i < tempArr.size(); i++)
{
tempChart.getData().add(new XYChart.Data<>(tempTimeArray.get(i), tempArr.get(i)));
}
tempChart.getNode().lookup(".chart-series-line").setStyle("-fx-stroke: orange;");
String s = "test";
Data<String, Number> verticalMarker = new Data<>(s, 20);
lcTemperature.addHorizontalValueMarker(verticalMarker);
}
Here is the LineChartWithMarker class:
package gems.view;
import java.util.Objects;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.shape.Line;
public class LineChartWithMarker<X,Y> extends LineChart
{
private ObservableList<Data<X, Y>> horizontalMarkers;
public LineChartWithMarker(CategoryAxis xAxis, NumberAxis yAxis)
{
super(xAxis, yAxis);
horizontalMarkers = FXCollections.observableArrayList(data -> new Observable[] {data.YValueProperty()});
horizontalMarkers.addListener((InvalidationListener)observable -> layoutPlotChildren());
}
public void addHorizontalValueMarker(Data<X, Y> marker)
{
Objects.requireNonNull(marker, "the marker must not be null");
if (horizontalMarkers.contains(marker)) return;
Line line = new Line();
marker.setNode(line );
getPlotChildren().add(line);
horizontalMarkers.add(marker);
}
public void removeHorizontalValueMarker(Data<X, Y> marker) {
Objects.requireNonNull(marker, "the marker must not be null");
if (marker.getNode() != null) {
getPlotChildren().remove(marker.getNode());
marker.setNode(null);
}
horizontalMarkers.remove(marker);
}
#Override
protected void layoutPlotChildren() {
super.layoutPlotChildren();
for (Data<X, Y> horizontalMarker : horizontalMarkers) {
Line line = (Line) horizontalMarker.getNode();
line.setStartX(0);
line.setEndX(getBoundsInLocal().getWidth());
line.setStartY(getYAxis().getDisplayPosition(horizontalMarker.getYValue()) + 0.5); // 0.5 for crispness
line.setEndY(line.getStartY());
line.toFront();
}
}
}
And here is the FXML (just the part for line chart):
<LineChart fx:id="lcTemperature" minWidth="100.0" prefHeight="155.0" prefWidth="708.0" BorderPane.alignment="CENTER">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis autoRanging="false" lowerBound="20.0" minorTickCount="10" minorTickLength="10.0" side="LEFT" tickLabelGap="10.0" tickLength="20.0" tickUnit="1.0" upperBound="30.0" />
</yAxis>
</LineChart>
Herew is the error stack:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/My%20Projects/GEMS_SEP2/GEMS_SEP2/bin/FXML/GUIgems.fxml:42
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at gems.view.GUIuserClient.start(GUIuserClient.java:33)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Caused by: java.lang.IllegalArgumentException: Can not set gems.view.LineChartWithMarker field gems.view.GUImainController.lcTemperature to javafx.scene.chart.LineChart
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
at java.lang.reflect.Field.set(Unknown Source)
at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163)
at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:765)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
... 12 more
Exception running application gems.view.GUIuserClient
By the way, GUIgems.fxml:42 is the last line of the FXML code I have posted above.
Also, its maybe worth mentioning that the line chart works fine if I am using a simple LineChart, like this:
#FXML
private LineChart<String, Number> lcTemperature;
It does not work when I write like this:
#FXML
private LineChartWithMarker<String, Number> lcTemperature = new LineChartWithMarker<>(xAxis,yAxis);

Alright so I managed to find a solution, if anyone will find themselves in the same situation.
Constructor of LineChartWithMarker needs to look like this:
public LineChartWithMarker(#NamedArg("xAxis") Axis<X> xAxis,
#NamedArg("yAxis") Axis<Y> yAxis)
{
super(xAxis, yAxis);
horizontalMarkers = FXCollections.observableArrayList(data -> new Observable[] {data.YValueProperty()});
horizontalMarkers.addListener((InvalidationListener)observable -> layoutPlotChildren());
}
And the FXML needs to look like this:
<?import gems.view.LineChartWithMarker?>
<LineChartWithMarker fx:id="lcTemperature" minWidth="100.0" prefHeight="155.0" prefWidth="708.0" BorderPane.alignment="CENTER">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis autoRanging="false" lowerBound="20.0" minorTickCount="10" minorTickLength="10.0" side="LEFT" tickLabelGap="10.0" tickLength="20.0" tickUnit="1.0" upperBound="30.0" />
</yAxis>
</LineChartWithMarker>

Related

Tableview getItems.AddAll causes java.lang.reflect.InvocationTargetException

Update: The cause is tblTimeDataClock is null.Any idea why?
I am getting a Tableview getItems.AddAll causes java.lang.reflect.InvocationTargetException when calling the getItems().AddAll() method. I have tried following the various examples on the internet but nothing seems so work. The exception occurs at the tblTimeDataClock.getItems().addAll(data) call.
I do see a warning on the new PropertyValue call saying Redundant Type arguments in new expression. Not sure if that has anything to do with it,
What am I doing wrong?
I put together this abbreviated example.
#FXML
private TableView<Test> tblTimeDataClock;
#FXML
private TableColumn<Test, String> colTimeSeconds;
#FXML
private TableColumn<Test, String> colDataVolts;
#FXML
private TableColumn<Test, String> colClockVolts;
#Override
public void initialize(URL url, ResourceBundle rb) {
listTimeDataClockMeasurement = new <Test>ArrayList();
colTimeSeconds.setCellValueFactory(new PropertyValueFactory<Test, String>("n1"));
colDataVolts.setCellValueFactory(new PropertyValueFactory<Test, String>("n2"));
colDataVolts.setCellValueFactory(new PropertyValueFactory<Test, String>("n3"));
ObservableList<Test> data = FXCollections.observableArrayList();
tblTimeDataClock.getItems().addAll(data);
}
The Test class
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Test {
private final SimpleStringProperty n1;
private final SimpleStringProperty n2;
private final SimpleStringProperty n3;
public String getN1() {
return n1.get();
}
public String getN2() {
return n2.get();
}
public String getN3() {
return n3.get();
}
public Test(String n1, String n2, String n3) {
this.n1 = new SimpleStringProperty(n1);
this.n2 = new SimpleStringProperty(n2);
this.n3 = new SimpleStringProperty(n3);
}
}
FXML added per request
<content>
<TableView fx:id="tblDataClock" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn fx:id="colTimeSeconds" prefWidth="116.0" text="Time" />
<TableColumn id="colData" fx:id="colDataVolts" prefWidth="127.0" text="Data" />
<TableColumn id="colClock" fx:id="colClockVolts" prefWidth="137.0" text="Clock" />
</columns>
</TableView>
</content>
Looks like the cause might be java.lang.NullPointerException
at serialdecoder.SerialDecoderController.initialize(SerialDecoderController.java:60)
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
file:/E:/Data/Java/SerialDecoder/dist/run67790734/SerialDecoder.jar!/serialdecoder/SerialDecoder.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at serialdecoder.SerialDecoder.start(SerialDecoder.java:22)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.NullPointerException
at serialdecoder.SerialDecoderController.initialize(SerialDecoderController.java:60)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
Exception running application serialdecoder.SerialDecoder
Found it! My TableView id was incorrect in my FXML file. :(

spring with javafx application exception when load fxml after mapping

Without mapping fxml this code run perfectly and load fxml
if i map fxml getting exception
i try alot but unable to understand what is the problem.....
SpringFxmlLoader class method load() return statement not returning anything because of which i think exception occur
javafx.fxml.LoadException:/E:/aa/JavaFXwithSpringBoot/bin/test/spring/boot/LoginPage.fxml:15
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579) at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at
javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) at
test.spring.boot.config.SpringFXMLLoader.load(SpringFXMLLoader.java:34)
at
test.spring.boot.config.StageManager.loadViewNodeHierarchy(StageManager.java:80)
at
test.spring.boot.config.StageManager.switchScene(StageManager.java:31)
at test.spring.boot.Main.displayInitialScene(Main.java:35) at
test.spring.boot.Main.start(Main.java:24) at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at
com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at
com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method) at
com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at
com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at
com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source) Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type 'test.spring.boot.LoginPageController'
available at
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
at
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090)
at
javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:929)
at
javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at
javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at
javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707) at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) ... 16 more
Exception in Application start method 2017-10-24 17:18:03.779 ERROR
6416 --- [lication Thread] test.spring.boot.config.StageManager :
Unable to load FXML view >> /test/spring/boot/LoginPage.fxml
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type 'test.spring.boot.LoginPageController'
available at
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090)
~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:929)
~[jfxrt.jar:na] at
javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
~[jfxrt.jar:na] at
javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
~[jfxrt.jar:na] at
javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
~[jfxrt.jar:na] at
javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
~[jfxrt.jar:na] at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) ~[jfxrt.jar:na]
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
~[jfxrt.jar:na] at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
~[jfxrt.jar:na] at
test.spring.boot.config.SpringFXMLLoader.load(SpringFXMLLoader.java:34)
~[bin/:na] at
test.spring.boot.config.StageManager.loadViewNodeHierarchy(StageManager.java:80)
[bin/:na] at
test.spring.boot.config.StageManager.switchScene(StageManager.java:31)
[bin/:na] at test.spring.boot.Main.displayInitialScene(Main.java:35)
[bin/:na] at test.spring.boot.Main.start(Main.java:24) [bin/:na] at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
[jfxrt.jar:na] at
com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
~[jfxrt.jar:na] at
com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
~[jfxrt.jar:na] at java.security.AccessController.doPrivileged(Native
Method) ~[na:1.8.0_131] at
com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
~[jfxrt.jar:na] at
com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
~[jfxrt.jar:na] at com.sun.glass.ui.win.WinApplication._runLoop(Native
Method) ~[jfxrt.jar:na] at
com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
~[jfxrt.jar:na] at java.lang.Thread.run(Unknown Source)
~[na:1.8.0_131]
2017-10-24 17:18:03.846 INFO 6416 --- [lication Thread]
s.c.a.AnnotationConfigApplicationContext : Closing
org.springframework.context.annotation.AnnotationConfigApplicationContext#f096164:
startup date [Tue Oct 24 17:18:02 IST 2017]; root of context hierarchy
2017-10-24 17:18:03.848 INFO 6416 --- [lication Thread]
o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed
beans on shutdown java.lang.reflect.InvocationTargetException at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at
com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at
com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at
sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) Caused by:
java.lang.RuntimeException: Exception in Application start method at
com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source) Caused by:
java.lang.NullPointerException: Root cannot be null at
javafx.scene.Scene.(Scene.java:336) at
javafx.scene.Scene.(Scene.java:194) at
test.spring.boot.config.StageManager.prepareScene(StageManager.java:62)
at test.spring.boot.config.StageManager.show(StageManager.java:36) at
test.spring.boot.config.StageManager.switchScene(StageManager.java:32)
at test.spring.boot.Main.displayInitialScene(Main.java:35) at
test.spring.boot.Main.start(Main.java:24) at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at
com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at
com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method) at
com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at
com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at
com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more Exception running application test.spring.boot.Main
Main.java
#ComponentScan("test.spring.boot")
#SpringBootApplication
public class Main extends Application {
protected ConfigurableApplicationContext springContext;
protected StageManager stageManager;
#Override
public void start(Stage primaryStage) throws IOException {
stageManager = springContext.getBean(StageManager.class, primaryStage);
displayInitialScene();
}
#Override
public void init() throws Exception
{
springContext=springBootApplicationContext();
}
protected void displayInitialScene() {
stageManager.switchScene(FxmlView.LOGIN);
}
#Override
public void stop() throws Exception {
springContext.close();
}
private ConfigurableApplicationContext springBootApplicationContext() {
SpringApplicationBuilder builder = new SpringApplicationBuilder(Main.class);
String[] args = getParameters().getRaw().stream().toArray(String[]::new);
return builder.run(args);
}
public static void main(String[] args) {
Application.launch(args);
}
}
Controller.java
public class LoginPageController implements Initializable{
#FXML
private TextField txtUserName;
#FXML
private PasswordField txtPassward;
#FXML
private Button btnLogin;
#FXML
private Label lblLogin;
#Autowired
private UserService userService;
#Lazy
#Autowired
private StageManager stageManager;
#FXML
public void login()
{
if(userService.authenticate(txtUserName.getText(), txtPassward.getText()))
{
stageManager.switchScene(FxmlView.USER);
}
else
{
lblLogin.setText("Login Failed.");
}
}
#Override
public void initialize(URL location, ResourceBundle resources) {
}
}
FxmlView.java
public enum FxmlView {
USER {
#Override
public String getTitle() {
return getStringFromResourceBundle("user.title");
}
#Override
public String getFxmlFile() {
return "/test/spring/boot/TestSpring.fxml";
}
},
LOGIN {
#Override
public String getTitle() {
return getStringFromResourceBundle("login.title");
}
#Override
public String getFxmlFile() {
return "/test/spring/boot/LoginPage.fxml";
}
};
public abstract String getTitle();
public abstract String getFxmlFile();
String getStringFromResourceBundle(String key){
return ResourceBundle.getBundle("Bundle").getString(key);
}
}
SpringFxmlLoader.java
#Component
public class SpringFXMLLoader {
private final ResourceBundle resourceBundle;
private final ApplicationContext context;
#Autowired
public SpringFXMLLoader(ApplicationContext context, ResourceBundle resourceBundle) {
this.resourceBundle = resourceBundle;
this.context = context;
}
public Parent load(String fxmlPath) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setControllerFactory(context::getBean); //Spring now FXML Controller Factory
loader.setResources(resourceBundle);
loader.setLocation(getClass().getResource(fxmlPath));
System.out.println("springfxmlloader class : "+fxmlPath);
System.out.println("LOADER : ");
return loader.load();
}
}
StageManager.java
public class StageManager {
private static final Logger LOG = getLogger(StageManager.class);
private final Stage primaryStage;
private final SpringFXMLLoader springFXMLLoader;
public StageManager(SpringFXMLLoader springFXMLLoader, Stage stage) {
System.out.println(">>>>>>>>>>>>>>>>>> 1");
this.springFXMLLoader = springFXMLLoader;
this.primaryStage = stage;
}
public void switchScene(final FxmlView view) {
System.out.println(">>>>>>>>>>>>>>>>>> 2");
Parent viewRootNodeHierarchy = loadViewNodeHierarchy(view.getFxmlFile());
show(viewRootNodeHierarchy, view.getTitle());
}
private void show(final Parent rootnode, String title) {
Scene scene = prepareScene(rootnode);
//scene.getStylesheets().add("/styles/Styles.css");
//primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setTitle(title);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.centerOnScreen();
System.out.println(">>>>>>>>>>>>>>>>>> 3");
try {
primaryStage.show();
} catch (Exception exception) {
logAndExit ("Unable to show scene for title" + title, exception);
}
}
private Scene prepareScene(Parent rootnode){
System.out.println(">>>>>>>>>>>>>>>>>> 4");
Scene scene = primaryStage.getScene();
System.out.println("SCN >>>>>>>>>>>>>>>>>>>> : "+ scene);
if (scene == null) {
scene = new Scene(rootnode);
}
scene.setRoot(rootnode);
return scene;
}
private Parent loadViewNodeHierarchy(String fxmlFilePath) {
System.out.println(">>>>>>>>>>>>>>>>>> 5 path: "+fxmlFilePath);
Parent rootNode = null;
try {
System.out.println(fxmlFilePath);
rootNode = springFXMLLoader.load(fxmlFilePath);
System.out.println(">>>>5 : try");
Objects.requireNonNull(rootNode, "A Root FXML node must not be null");
} catch (Exception exception) {
System.out.println("File path????? : "+fxmlFilePath);
logAndExit("Unable to load FXML view >> " + fxmlFilePath, exception);
exception.printStackTrace();
}
return rootNode;
}
private void logAndExit(String errorMsg, Exception exception) {
System.out.println(">>>>>>>>>>>>>>>>>> 6");
LOG.error(errorMsg, exception, exception.getCause());
Platform.exit();
}
}
You forgot to add a Stereotype on your LoginPageController class that's why Spring is telling you:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'test.spring.boot.LoginPageController'
As the class is not marked the component-scan will ignore it and there will be no bean register assignable for that type.
Try:
#Controller
public class LoginPageController implements Initializable{
#Autowired
public LoginPageController(UserService userService, private StageManager stageManager){
this.userService = userService;
this.stageManager = stageManager;
}
}

How to load video in java FX

I want to insert video into javaFX. Insert video from computer - Not from youtube or something.
Play/pause/minimize buttons, borders are not needed.
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
public class MediaMP4 extends Application {
Stage window;
Scene scene1;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws FileNotFoundException {
window = primaryStage;
primaryStage.setTitle("Moves");
Media media = new Media ("pr.mp4");
MediaPlayer player = new MediaPlayer (media);
MediaView view = new MediaView (player);
Group full = new Group ();
full.getChildren().addAll(view);
scene1 = new Scene (full,600,600);
primaryStage.setScene(scene1);
window.show();
player.play();
}
}
My pr.mp4 file is inside project, not in package.
Stack trace:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'pr.mp4'
at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:211)
at javafx.scene.media.Media.<init>(Media.java:393)
at vv.MediaMP4.start(MediaMP4.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Exception running application vv.MediaMP4
The relevant line in your stack trace is:
Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'pr.mp4'.
You need to specify an URI string with a scheme as a construtor argument, as specified here.
So change your line from this:
Media media = new Media ("pr.mp4");
to something like this:
Media media = new Media("file://c:/myproject/pr.mp4"));
Have a look at this question for more details: How to target a file (a path to it) in Java/JavaFX

JavaFX Clock widget throws Exceptions randomly

I have written a little Clock Widget extending the Text class of JavaFX. To update the Time I am using a Task that basically sets the text to the current System Time. When I run this application in Eclipse it sometimes throws a NullpointerException in the line I call stage.show().
This is what my widgets sourcecode looks like:
package clock;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javafx.concurrent.Task;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class CurrentClockText extends Text {
private final DateTimeFormatter formatter;
public static final int HOUR = 1;
public static final int HOUR_MINUTE = 2;
public static final int HOUR_MINUTE_SECOND = 3;
public static final int HOUR_MINUTE_SECOND_MILLISECOND = 4;
private final long updateInterval;
private final Task<Void> updater = new Task<Void>() {
#Override
protected Void call() throws Exception {
while (true) {
LocalDateTime now = LocalDateTime.now();
String nowTextual = formatter.format(now);
setText(nowTextual);
try {
Thread.sleep(updateInterval);
} catch (InterruptedException ignore) {
}
}
}
};
public CurrentClockText() {
this(HOUR_MINUTE);
}
public CurrentClockText(final int detailLevel) {
String timeFormat = "";
switch (detailLevel) {
case HOUR:
timeFormat = "HH";
updateInterval = 60000;
break;
case HOUR_MINUTE:
timeFormat = "HH:mm";
updateInterval = 15000;
break;
case HOUR_MINUTE_SECOND:
timeFormat = "HH:mm:ss";
updateInterval = 500;
break;
case HOUR_MINUTE_SECOND_MILLISECOND:
updateInterval = 1;
timeFormat = "HH:mm:ss.S";
break;
default:
throw new IllegalArgumentException(
"Unknown detail level for Clock: " + detailLevel);
}
setFont(new Font("Verdana", 28));
formatter = DateTimeFormatter.ofPattern(timeFormat);
Thread updaterThread = new Thread(updater, "CurrentClockText.updaterThread");
updaterThread.setDaemon(true);
updaterThread.start();
}
}
This is the main class:
package clock;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Boot extends Application {
#Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
CurrentClockText clock = new CurrentClockText(4);
pane.setCenter(clock);
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Boot.launch(args);
}
}
And this is the stacktrace:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/14845382.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at com.sun.javafx.text.PrismTextLayout.addTextRun(Unknown Source)
at com.sun.javafx.text.GlyphLayout.addTextRun(Unknown Source)
at com.sun.javafx.text.GlyphLayout.breakRuns(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.buildRuns(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.layout(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.ensureLayout(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.getBounds(Unknown Source)
at javafx.scene.text.Text.getLogicalBounds(Unknown Source)
at javafx.scene.text.Text.impl_computeLayoutBounds(Unknown Source)
at javafx.scene.Node$12.computeBounds(Unknown Source)
at javafx.scene.Node$LazyBoundsProperty.get(Unknown Source)
at javafx.scene.Node$LazyBoundsProperty.get(Unknown Source)
at javafx.scene.Node.getLayoutBounds(Unknown Source)
at javafx.scene.Node.prefWidth(Unknown Source)
at javafx.scene.Node.minWidth(Unknown Source)
at javafx.scene.layout.Region.computeChildPrefAreaWidth(Unknown Source)
at javafx.scene.layout.BorderPane.getAreaWidth(Unknown Source)
at javafx.scene.layout.BorderPane.computePrefWidth(Unknown Source)
at javafx.scene.Parent.prefWidth(Unknown Source)
at javafx.scene.layout.Region.prefWidth(Unknown Source)
at javafx.scene.Scene.getPreferredWidth(Unknown Source)
at javafx.scene.Scene.resizeRootToPreferredSize(Unknown Source)
at javafx.scene.Scene.preferredSize(Unknown Source)
at javafx.scene.Scene.impl_preferredSize(Unknown Source)
at javafx.stage.Window$9.invalidated(Unknown Source)
at javafx.beans.property.BooleanPropertyBase.markInvalid(Unknown Source)
at javafx.beans.property.BooleanPropertyBase.set(Unknown Source)
at javafx.stage.Window.setShowing(Unknown Source)
at javafx.stage.Window.show(Unknown Source)
at javafx.stage.Stage.show(Unknown Source)
at clock.Boot.start(Boot.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/19600960.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/18503843.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/27167109.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/2180324.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/3326003.run(Unknown Source)
... 1 more
Exception running application clock.Boot
As I said this Exception only occurs in some runs and not always. It happens mostly when I change the formatting pattern of detailLevel = 4 but remains for another run after changing it back again. I am guessing this might actually have something to do with Eclipse but I can't debug the code either because the Exception gets thrown in the stage.show call which I absolutely do not understand. What's the cause of this random Exception and how can I fix it?
In JavaFX, as in most other GUI toolkits, there is one specific thread which handles all UI related operations. An application must not update the UI outside of this thread. If the UI needs to be updated from another thread, there are usually APIs available which ensure that code is executed in the context of the UI thread. In case of JavaFX, see the Concurrency in JavaFX Tutorial for more information.
In your case, the simplest solution would be to make sure that your setText() call is executed on the JavaFX application thread, not on the thread associated with your Task:
...
Platform.runLater(() -> setText(nowTextual));
...
There are also other APIs available in JavaFX to do animations, which can be used to call a handler method at specific time intervals - that would remove the Thread.sleep() call from your loop. See Timeline for more information.

GWT using OSM causes Exception

i have problems with gwt using openlayers and openstreetmaps to get a simple
example projekt to work.
When i run the projekt it always throws an exception:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:406)
at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:5 26)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Unknown Source) Caused by: com.google.gwt.core.client.JavaScriptException: (TypeError) #org.gwtopenmaps.openlayers.client.layer.OSMImpl::Osmarender(Ljava/lang/String;)([string: 'Osmarender']): $wnd.OpenLayers.Layer.OSM.Osmarender is not a constructor
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:24 9)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at org.gwtopenmaps.openlayers.client.layer.OSMImpl.Osmarender(OSMImpl.java)
at org.gwtopenmaps.openlayers.client.layer.OSM.Osmarender(OSM.java:38)
at de.iisys.sara2.sara2lightWebclient.client.View.initMap(View.java:33)
at de.iisys.sara2.sara2lightWebclient.client.View.onModuleLoad(View.java:26) ... 9 more
My Example Code:
public class View implements EntryPoint {
public View() {
}
public void onModuleLoad() {
initMap();
}
private void initMap() {
MapOptions defaultMapOptions = new MapOptions();
MapWidget mapWidget = new MapWidget("684px", "330px", defaultMapOptions);
OSM osm_1 = OSM.Osmarender("Osmarender");
OSM osm_2 = OSM.Mapnik("Mapnik");
OSM osm_3 = OSM.CycleMap("CycleMap");
OSM osm_4 = OSM.Maplint("Maplint");
osm_1.setIsBaseLayer(true);
osm_2.setIsBaseLayer(true);
osm_3.setIsBaseLayer(true);
osm_4.setIsBaseLayer(true);
Map map = mapWidget.getMap();
map.addLayer(osm_1);
map.addLayer(osm_2);
map.addLayer(osm_3);
map.addLayer(osm_4);
map.addControl(new LayerSwitcher());
map.addControl(new MousePosition());
LonLat lonLat = new LonLat(6.95, 50.94);
lonLat.transform("EPSG:4326", "EPSG:900913");
map.setCenter(lonLat, 12);
RootPanel.get().add(mapWidget);
}
Does anybody know how to solve this problem?
After some research I can tell you that osmarender as well as maplint are not supported anymore: http://wiki.openstreetmap.org/wiki/Tiles#home
If you delete those to layers from your function (osm_1 and osm_4) everything works perfectly.

Categories

Resources