JavaFX: Add MapView to VBox - java

I want to add a MapView from JXMaps to the VBox in JavaFX, i tried to add the MapView to the border panel and then added to the VBox, but i canĀ“t find any solution to this, i hope you can help me, this is my code:
public class ControlerMap
{
#FXML private BorderPane borderPanel;
#FXML private Button butom;
#FXML VBox cosa;
#FXML public void initialize()
{
MapView sample = new MapView();
sample.setMaxHeight(394.0);
sample.setPrefWidth(704.0);
sample.setVisible(true);
borderPanel = new BorderPane(sample);
borderPanel.setVisible(true);
borderPanel.setLayoutX(76.0);
borderPanel.setLayoutY(134.0);
borderPanel.setPrefHeight(200.0);
borderPanel.setPrefWidth(467.0);
cosa.getChildren().add(borderPanel);
}
}
And here is my code from the .FXML file:
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="496.0" prefWidth="757.0"
xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="prueba.web.googleMap.ControlerMap">
<children>
<Button fx:id="butom" layoutX="25.0" layoutY="14.0"
mnemonicParsing="false" text="Button" />
<VBox fx:id="cosa" layoutX="32.0" layoutY="68.0" prefHeight="394.0"
prefWidth="704.0">
<children>
<BorderPane fx:id="borderPanel" prefHeight="400.0"
prefWidth="704.0" />
</children>
</VBox>
</children>
</AnchorPane>

As I can see you created empty MapView.
MapView sample = new MapView();
Maps will be actually drawn only after initialization of its properties (at least zoom and center). You have to set it in following way:
sample.setOnMapReadyHandler(new MapReadyHandler() {
#Override
public void onMapReady(MapStatus status) {
if (status == MapStatus.MAP_STATUS_OK) {
final Map map = mapView.getMap();
map.setCenter(new LatLng(35.91466, 10.312499));
map.setZoom(2.0);
}
}
});

Related

JavaFX Scene width and height not working properly

I have this code, which launches a stage in FX:
public class ApplicationStart extends Application {
#Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/frames/login.fxml"));
LoginController loginController = new LoginController();
loader.setController(loginController);
Parent root = loader.load();
stage.setTitle("Login");
stage.setScene(new Scene(root, 600, 400));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
In the login.fxml file, the Pane has the width of 600, and height of 400. And in the code above, I set the width and height of the Scene accordingly.
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1">
But when I launch the application, the width and height are totally messed up, they are way bigger, like this. I tried to reduce the width and height in the code above, trying to debug what is happening, to something like Scene(root, 400, 200). Now it looked ok, but when I clicked on the "Register" button, which is the lower most button, the button doesn't respond, like if I would click outside the window (if I have intellij in the background, for example, and i click the button, the intellij window comes first, and the app window goes back). It's like the app window has some visible bounds, and some actual bounds. The visible bounds are something like width + 200 height + 200, and the actual logical bounds are the exact width and height I set. What can I do to solve this? Here's the entire fxml file.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TextField layoutX="357.0" layoutY="131.0" />
<PasswordField layoutX="357.0" layoutY="187.0" />
<Label layoutX="310.0" layoutY="136.0" text="Email:" />
<Label layoutX="285.0" layoutY="191.0" text="Password:" />
<Button layoutX="357.0" layoutY="260.0" mnemonicParsing="false" prefHeight="32.0" prefWidth="152.0" style="-fx-background-color: #25b2fd;" text="Log In" />
<ImageView fx:id="logoImageView" fitHeight="230.0" fitWidth="324.0" layoutX="14.0" layoutY="85.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../../../../images/logo.png" />
</image>
</ImageView>
<ImageView fx:id="lockImageView" fitHeight="51.0" fitWidth="53.0" layoutX="408.0" layoutY="60.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../../../../images/lock.png" />
</image>
</ImageView>
<Button layoutX="357.0" layoutY="307.0" mnemonicParsing="false" prefHeight="32.0" prefWidth="152.0" style="-fx-background-color: #25b2fd;" text="Register" />
</children>
</Pane>
And here's the LoginController class, but I doubt it will help, it doesn't have much in it other than initializing the images:
public class LoginController implements Initializable {
#FXML
private ImageView logoImageView = new ImageView();
#FXML
private ImageView lockImageView = new ImageView();
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
File logoFile = new File("images/logo.png");
Image logoImage = new Image(logoFile.toURI().toString());
logoImageView.setImage(logoImage);
File lockFile = new File("images/lock.png");
Image lockImage = new Image(lockFile.toURI().toString());
lockImageView.setImage(lockImage);
}
}

field that is supposed to be assigned by fxml is throwing NullPointerException when accesed from inner class - javaFX

I have a controller for an fxml file. The controller has a field public BorderPane mainBorderPane; which is supposed to be filled with the Border pane of the same fx:id found in the fxml file. When I try to access it from an inner class it gives a NullPointerExcetion
The clearBorderPaneCenter(); and the clearBorderPaneRight work just fine but when I run the cashSceneController.show() it crashes on the line mainBorderPane.setRight(rightLayout);
fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:id="mainBorderPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Program.gui.MainSceneController">
<top>
<Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</top>
<left>
<Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</left>
<center>
<Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
<right>
<Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</right>
<bottom>
<Pane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</bottom>
</BorderPane>
Simplified version of controller:
public class MainSceneController {
MainSceneController.CashSceneController cashSceneController = new MainSceneController.CashSceneController();
public BorderPane mainBorderPane;
public void switchLayout(byte ID){
//todo Make this work switchScene()
clearBorderPaneCenter();
clearBorderPaneRight();
cashSceneController.show();
}
public void clearBorderPaneRight(){
try {
mainBorderPane.setRight(null);
OutputHelper.log("cleared right of mainBorderPane");
} catch (Exception e){
OutputHelper.log("clearing right of mainBorderPane not required - already cleared");
}
}
public void clearBorderPaneCenter(){
try {
mainBorderPane.setCenter(null);
OutputHelper.log("cleared centre of mainBorderPane");
} catch (Exception e){
OutputHelper.log("clearing centre of mainBorderPane not required - already cleared");
}
}
public class CashSceneController{
VBox rightLayout;
public void show() {
setVBox();
mainBorderPane.setRight(rightLayout);
}
public void setVBox(){
rightLayout = new VBox();
//......
}
}
}
This is how the fxml file is loaded
SceneController = new MainSceneController(new Scene(FXMLLoader.load(getClass().getResource("gui/MainScreen.fxml"))));
I hope that I explained my problem well enough. I'm new to stack overflow and have no idea of what is a good question
Edit: it appears that the problem is caused by the mainBorderPane not being assigned at all. The clearBorderPaneCenter and clearBorderPaneRight seem to not crash cause they are caught by the try catch. Any ideas why the mainBorderPane might not be getting assigned correctly?
You are missing the fxml annotation. It is required for the FXMLLoader to inject the BorderPane into the code
#FXML
public BorderPane mainBorderPane;

JavaFX - Adding buttons dynamically to fxml created VBox

I am trying to add x number of buttons to a VBox located in a scrollpane.
The Vbox, and scrollpane are made with scenebuilder.
However i can't seem to connect the VBox to the controller as a variable.
The piece of code that spawns the scene is the following:
Stage stage = MyMain.stage;
stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("/main/fxml/catEdit.fxml"))));
stage.show();
My FXML is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<BorderPane maxHeight="-Infinity"
maxWidth="-Infinity"
minHeight="-Infinity"
minWidth="-Infinity"
prefHeight="400.0"
prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.111"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="main.controllers.CatEditCon">
<center>
<ScrollPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0">
<children>
<VBox fx:id="catBox" alignment="CENTER" prefHeight="200.0" prefWidth="100.0" />
</children></AnchorPane>
</content>
</ScrollPane>
</center>
</BorderPane>
And the controller is:
package main.controllers;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import main.MyMain;
import main.database.master.Category;
import main.fxml.custom.CatBut;
public class CatEditCon{
#FXML public VBox catBox;
public CatEditCon(){
catBox = new VBox();
updateScroll();
}
public void updateScroll(){
Category cat = MyMain.baseData.getCategories();
System.out.println(catBox);
int len = MyMain.baseData.getArticles().size();
for(int i = 0 ; i < len; i++ ){
System.out.println(""+i);
Button b = new Button();
catBox.getChildren().add(b);
}
}
}
Any tips to what i do wrong?
The scene opens, and the sysout counts upwards as it should. But no buttons appear?
If the catBox isnt initialized in the constructor, i get a nullPionter to that object. So it is as if i don't make the connection to the fxml object.

Adding columns to a Table view when initialized in FXML

I am trying to add columns to a table view like this:
#FXML
private TableView tblView;
//private Label tblViewTitle;
#FXML
public void showTable(ActionEvent e) throws Exception{
Stage TableView = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("tableView.fxml"));
TableView.setTitle("Table");
TableView.setScene(new Scene(root));
//tblViewTitle.setText("test");
//For dynamic calculate later on
TableColumn clm1 = new TableColumn("Test");
TableColumn clm2 = new TableColumn("Test");
TableColumn clm3 = new TableColumn("Test");
TableColumn clm4 = new TableColumn("Test");
tblView.getColumns().addAll(clm1, clm2, clm3, clm4);
TableView.show();
}
The fxml file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.101" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<TableView fx:id="tblView" editable="true" layoutX="84.0" layoutY="94.0" prefHeight="306.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="94.0" />
<Label fx:id="tblViewTitle" layoutX="14.0" layoutY="26.0" prefHeight="49.0" prefWidth="181.0">
<font>
<Font size="39.0" />
</font>
</Label>
</children>
</AnchorPane>
I'm getting a nullPointerException when I'm trying to add the columns to the table view and I don't understand why.
Is it that I can't add columns because the table view is not an object?

SubScene in JavaFX does not show elements loaded from an FXML file

I have a program created with an FXML file (made in SceneBuilder) that has four SubScenes in it:
#FXML
public SubScene p1sub;
#FXML
public SubScene p2sub;
#FXML
public SubScene p3sub;
#FXML
public SubScene p4sub;
Each of these subscenes is nearly identical to the rest.
I can get the root node (which contains these) to show up just fine, but when I try to add the SubScenes, they don't show up.
//This is the code I use to initialize one of the four.
Parent root2a = null;
try {
FXMLLoader loader2 = new FXMLLoader(getClass().getResource(
"PlayerConfigurationSubScreen.fxml"));
root2a = (Parent) loader2.load();
} catch (Exception e) {
/*code*/
}
/*code*/
if (root2a != null) {
System.out.println("root2 isn't null");
p1sub = new SubScene(root2a, 149, 260);
}
/*code*/
stage.show();
Any idea how to make them show up? I'm new at JavaFX.
Set the subscene dimensions in fxml and instead of "new" just add the parent to the subscene.
....
p1sub.setRoot(root2a);
....
I think you are approaching the problem the wrong way. From what I understand from your code, you want to load a subscene and then assign it to one of your predefined "slots".
This however will not work, as the pre-defined slots has nothing to do with what is actually rendered. Everything that is drawn in a window has to be assigned as child of a container of some kind.
I've made an example here that reproduces the problem you are facing:
public class ReplaceTest extends Application {
#FXML Button button;
#FXML Label oldLabel;
#Override
public void start(Stage primaryStage) throws IOException{
Parent root = FXMLLoader.load( getClass().getResource("ReplaceTest.fxml") );
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private static int i = 0;
#FXML protected void simpleClick(ActionEvent e){
Label newLabel = new Label("Hello! " + i++);
oldLabel = newLabel;
System.out.println("Nothing happens...");
}
}
And the FXML file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<VBox alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="200.0" spacing="10.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="replaceTest.ReplaceTest">
<children>
<Label fx:id="oldLabel" text="TEXT TO BE REPLACED" />
<Button fx:id="button" onAction="#simpleClick" text="Replace label" />
</children>
<padding>
<Insets top="10.0" />
</padding>
</VBox>
If you try this example, you will see that overwriting oldLabel does not affect the rendering window. Instead, you can try to create areas to which you add your new subscenes as children. This will update the rendering window and will (hopefully) produce the behavior you desire. Again, I've made an example you can consult:
public class ReplaceTest extends Application {
#FXML Button button;
#FXML VBox wrappingBox;
#Override
public void start(Stage primaryStage) throws IOException{
Parent root = FXMLLoader.load( getClass().getResource("ReplaceTest.fxml") );
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private static int i = 0;
#FXML protected void simpleClick(ActionEvent e){
Label newLabel = new Label("Hello! " + i++);
wrappingBox.getChildren().clear();
wrappingBox.getChildren().add(newLabel);
System.out.println("Someting happens!");
}
}
And the FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<VBox alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="200.0" spacing="10.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="replaceTest.ReplaceTest">
<children>
<VBox fx:id="wrappingBox" alignment="TOP_CENTER">
<children>
<Label text="TEXT TO BE REPLACED" />
</children>
</VBox>
<Button fx:id="button" onAction="#simpleClick" text="Replace label" />
</children>
<padding>
<Insets top="10.0" />
</padding>
</VBox>

Categories

Resources