Getting an FXML window to open when running a java file - java

I am currently trying to build a picture viewing program using java and an FXML window. I have had a fiddle about - getting to know FXMLs and accessing them from a program and was able to get buttons to disappear and reappear - but upon adapting said code for this picture viewer, I found that the FXML panel wouldn't open upon running the file. There are no errors/warnings besides warnings about (as yet) unused libraries being declared. Upon start up, there's no error messages, no text boxes and no outputs to the terminal so I can't supply anything from there. The code is as follows:
package practice1;
import javafx.application.Application;
import javafx.stage.Stage;
import java.io.IOException;
import javax.imageio.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.image.ImageView;
import javafx.scene.image.*;
import java.awt.image.BufferedImage;
public class MainProgram extends Application{
public void start(Stage stage) {
try {
FXMLLoader fxmlLoader = new FXMLLoader();
String viewerFxml = "WindowPanel.fxml";
AnchorPane page = (AnchorPane)fxmlLoader.load(
this.getClass().getResource(viewerFxml).openStream());
Scene scene = new Scene(page);
stage.setScene(scene);
stage.show();
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
System.exit(1);
}
}
public static void main(String args[]) {
launch(args);
System.exit(0);
}
}
And the FXML is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="practice1.MyController">
<children>
<Button fx:id="TurnLeft" layoutX="113.0" layoutY="353.0" mnemonicParsing="false" onAction="#hide1" text="Turn Left" />
<Button fx:id="TurnRight" layoutX="237.0" layoutY="353.0" mnemonicParsing="false" onAction="#hide2" text="Turn Right" />
<ToolBar prefHeight="40.0" prefWidth="600.0">
<items>
<MenuButton mnemonicParsing="false" text="Pick Up">
<items>
<MenuItem mnemonicParsing="false" text="Action 1" />
<MenuItem mnemonicParsing="false" text="Action 2" />
</items>
</MenuButton>
<MenuButton mnemonicParsing="false" text="Drop">
<items>
<MenuItem mnemonicParsing="false" text="Action 1" />
<MenuItem mnemonicParsing="false" text="Action 2" />
</items>
</MenuButton>
</items>
</ToolBar>
<Button fx:id="proceed" layoutX="178.0" layoutY="315.0" mnemonicParsing="false" onAction="#changeImage" text="Proceed" />
<ImageView fx:id="mainImage" fitHeight="259.0" fitWidth="426.0" layoutY="40.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#Picture1.png" />
</image></ImageView>
<Text layoutX="436.0" layoutY="60.0" strokeType="OUTSIDE" strokeWidth="0.0" text="You have" />
<Text layoutX="436.0" layoutY="86.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Object1" />
<Text layoutX="436.0" layoutY="111.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Object2" />
<Text layoutX="436.0" layoutY="139.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Object 3" />
<ImageView fx:id="SmallImage2" fitHeight="89.0" fitWidth="117.0" layoutX="266.0" layoutY="45.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="SmallImage3" fitHeight="89.0" fitWidth="117.0" layoutX="266.0" layoutY="142.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="SmallImage1" fitHeight="89.0" fitWidth="117.0" layoutX="152.0" layoutY="45.0" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>
The file "Picture1.png" is located in the workspace with the position being:
WorkspaceA/Practice1/scr/practice1/Picture1.png

Background
The # notation is used in JavaFX to specify a relative location which is "assumed to be located at a path relative to the current FXML file".
What is Wrong
You load the FXML as a stream using the following code:
AnchorPane page = (AnchorPane)fxmlLoader.load(
this.getClass().getResource(viewerFxml).openStream());
A steam is not a location, so there is no concept of locations relative to the stream.
If I run your application locally, I will get a stack trace where it cannot find the picture file (here is just the last portion of it):
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1081)
... 18 more
How to fix it
Set the location in the loader prior to loading the FXML:
fxmlLoader.setLocation(getClass().getResource(viewerFxml));
AnchorPane page = fxmlLoader.load();
The loader will then be able to resolve the relative reference to your picture file.
Check your directory structure and build output
This may or may not be an issue for you.
You specify your image using the location specifier:
<Image url="#Picture1.png" />
Which tells the FXMLLoader to look for Picture1.png at the same location it got the FXML from; e.g. if you loaded the FXML from the filesystem, the image would be in the same folder on the filesystem as the FXML - similarly if you load the FXML from a jar, the image should be on the same path within the jar as your FXML was retrieved from.
You state that you place your picture at: WorkspaceA/Practice1/scr/practice1/Picture1.png. I'm not sure what that location is, but if it is the same location as your MainProgram.java source, your MyController.java source, WindowPanel.fxml and if your build system is set to copy the image and fxml over to the compile and packaging target directory, then it will work fine - if it's not, you will need to move the image to the appropriate source location.

Related

JavaFX and Scene Builder clip scene edges despite specifying USE_COMPUTED_SIZE

I'm using Scene Builder (v11.0.0) to create FXML files for scenes in JavaFX (v12) but, despite instructing all containers to USE_COMPUTED_SIZE for the preferred widths and heights, the rendered scenes (as seen in Scene Builder and also when run as a JavaFX application which loads those FXML files) are being clipped at the right and bottom edges so that bits of nodes are chopped off.
And in Scene Builder it seems that the renderer must know that the scene won't fit the allowed bounds because the editor shows blue boundary markers which are clearly some way beyond the rendered rectangle.
View in Scene Builder
The view in Scene Builder shows that more space is needed at the bottom in order to give the buttons sufficient space (their bottom edge, and the lower edge of the TitledPane is missing). And more space is needed at the right in order to fit the right edges of the DatePicker and TitledPane. The blue boundary markers show clearly where the actual content ends, so it's not clear why the display area is being calculated to be several pixels shorter than this.
View of running Java application
Once the FXML files are used to populate a window in a JavaFX application, the same thing is seen: the calculated size for the window is a number of pixels too few to fit the whole scene correctly.
If the blue boundary markers have correctly been calculated to show that extra display area width and height are needed, how do I tell the FXML to require this additional space when rendering?
Is this a known bug/limitation in Scene Builder, FXML, or JavaFX. Or is there something more I need to do beyond just selecting USE_COMPUTED_SIZE for the preferred dimensions?
In order to make this explicit, see the example FXML below which displays the problem illustrated.
scene.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TitledPane animated="false" collapsible="false" text="untitled">
<content>
<HBox>
<children>
<fx:include source="subscene.fxml" />
</children>
</HBox>
</content>
</TitledPane>
<TitledPane animated="false" collapsible="false" text="untitled">
<content>
<HBox>
<children>
<fx:include source="subscene.fxml" />
</children>
</HBox>
</content>
</TitledPane>
<TitledPane animated="false" collapsible="false" text="untitled">
<content>
<HBox alignment="BASELINE_RIGHT">
<children>
<Button mnemonicParsing="false" text="Button" />
<Button mnemonicParsing="false" text="Button" />
</children>
</HBox>
</content>
</TitledPane>
</children>
</VBox>
subscene.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label text="Label" />
<DatePicker />
</children>
</VBox>
This does appear to be a bug in JavaFX, specifically DatePicker, as this simple example can reproduce the problem:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
VBox root = new VBox(new DatePicker());
// Problem shows up when using USE_COMPUTED_SIZE (the default) as well
root.setMinSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
root.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
Resulting in:
Note: It does not seem to matter what parent the DatePicker is put in. Nor does the problem appear with other controls.
A workaround to this issue appears to be calling Window.sizeToScene() after calling show(). I don't understand why that would make a difference, but it does. Unfortunately, this will only help in the real application, not in Scene Builder.

FXML: ImageViews showing up in SceneBuilder but not in the actual app

I'm trying to write a simple Java app for modifying and visualizing logic circuits by dragging gates and connections about. I'm using SceneBuilder to put the interface together. Right now, I'm stuck at getting the available basic logic gates to display in their proper bar and respond to being interacted with. More accurately, I'm trying to get one gate to just display some console output, to confirm that the GUI-logic connection is working.
The biggest problem I'm having is that the ImageViews of the gates, possibly along with some other FXML elements, refuse to display in the actual compiled app for some reason, even though they work and react correctly in SceneBuilder and in its "Preview" feature.
I had to do some experimenting with wrapping them in various other FXML elements which I didn't really understand because apparently ImageWiew doesn't have a onDragDetected() method, even though the text input field for it is available in SceneBuilder. The intended work-in-progress app layout can be seen plainly enough directly from SceneBuilder on the first picture. Compare with the second one, which is of the actual running application.
Possibly relevant code:
Main.java
package main;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("RootLayout.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 640, 450));
primaryStage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}
TheCircuitController.java
package Gates;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
/**
* The class for holding all the information about gates, connections, and in and out pins in the current circuit
*/
public class TheCircuitController implements Initializable{
#FXML
private AnchorPane anchorPaneNAND;
//TODO temporarily public, make private later
public ArrayList<CircuitElement> allCircuitElements= new ArrayList<CircuitElement>();
public ArrayList<Pin> theCircuitInputPins = new ArrayList<Pin>();
public ArrayList<Pin> theCircuitOutputPins = new ArrayList<Pin>();
ArrayList<Connection> allCircuitConnections = new ArrayList<Connection>();
public ArrayList<Pin> allCircuitGateInputPins = new ArrayList<Pin>();
public ArrayList<Pin> allCircuitGateOutputPins = new ArrayList<Pin>();
public ArrayList<Gate> allCircuitGates = new ArrayList<Gate>();
private InbuiltGateType currentDragGateType;
#Override
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
// initialize your logic here: all #FXML variables will have been injected
anchorPaneNAND.setOnDragDetected(this::handleDragDetectedNAND);
}
#FXML
private void handleDragDetectedNAND(MouseEvent mouseEvent) {
System.out.println("drag detected nand!");
}
//other stuff of the class, unrelated to FXML
}
RootLayout.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="450.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Gates.TheCircuitController">
<children>
<MenuBar prefHeight="27.0" prefWidth="562.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<SplitPane dividerPositions="0.2413793103448276" prefHeight="402.0" prefWidth="640.0">
<items>
<ScrollPane fitToHeight="true" fitToWidth="true" prefHeight="400.0" prefWidth="122.0">
<content>
<VBox prefHeight="400.0" prefWidth="208.0" spacing="10.0">
<children>
<AnchorPane fx:id="anchorPaneNAND" onDragDetected="#handleDragDetectedNAND">
<children>
<ImageView>
<image>
<Image url="#../../resources/100px-NAND_ANSI.svg.png" />
</image>
</ImageView>
</children>
</AnchorPane>
<ImageView>
<image>
<Image url="#../../resources/100px-NOT_ANSI.svg.png" />
</image>
</ImageView>
<ImageView>
<image>
<Image url="#../../resources/100px-AND_ANSI.svg.png" />
</image>
</ImageView>
<ImageView>
<image>
<Image url="#../../resources/OR_ANSI.svg.png" />
</image>
</ImageView>
<ImageView>
<image>
<Image url="#../../resources/100px-NOR_ANSI.svg.png" />
</image>
</ImageView>
<ImageView>
<image>
<Image url="#../../resources/100px-XOR_ANSI.svg.png" />
</image>
</ImageView>
<ImageView>
<image>
<Image url="#../../resources/100px-XNOR_ANSI.svg.png" />
</image>
</ImageView>
</children>
<padding>
<Insets left="20.0" right="20.0" />
</padding></VBox>
</content></ScrollPane>
<ScrollPane prefHeight="400.0" prefWidth="406.0" />
</items>
</SplitPane>
</children>
</VBox>
I thus need to know:
Why are those gates(or at least one) not displaying as intended? And what's with the ScrollPane, why is it not displaying its sliders as it is in SceneBuilder? What things do I need to set up differently or wiggle with to get those gates to show up and interact correctly?
After a bit of random crapshooting, I found a solution.
First, I looked into View->Show Sample Controller Skeleton. There, I noticed that the handleDragDetectedNAND() method does not have any modifier, whereas mine had private, copied early from some tutorial or the other. I removed the modifier and the application now works. If anyone who passes by cared to explain why this is the case(I have no idea and no time to research, deadline's fast approaching), the value of this answer would rise significantly.
Be sure all images inside of src folder. (tested)
The image which outsite of src folder dont appear.
+ MyProject
+ not_working_dir
+ src
+ com.stackoverflow
+ working_dir

The controller 'AdminSceneController' has no event slot 'logout' - JavaFX Error

I am trying to create a simple button, but I am getting this stupid error and it doesn't make any sense.
Here is my Admin Scene FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="437.0" prefWidth="582.0" stylesheets="#application.css" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="appController.AdminSceneController">
<children>
<Separator layoutX="-14.0" layoutY="101.0" prefHeight="10.0" prefWidth="601.0" />
<Label layoutX="14.0" layoutY="29.0" text="admin panel">
<font>
<Font name="Book Antiqua" size="28.0" />
</font>
</Label>
<Button layoutX="174.0" layoutY="32.0" mnemonicParsing="false" style="-fx-background-radius: 100px;" text="+" textFill="#369033" />
<Button fx:id="logoutButton" layoutX="14.0" layoutY="65.0" mnemonicParsing="false" onAction="#logout" prefHeight="3.0" prefWidth="81.0" styleClass="logout" stylesheets="#application.css" text="(logout)" textFill="#070707" />
<Button layoutX="387.0" layoutY="392.0" mnemonicParsing="false" prefHeight="31.0" prefWidth="158.0" text="Delete" />
<ListView layoutX="223.0" layoutY="106.0" prefHeight="327.0" prefWidth="128.0" />
</children>
</Pane>
And this is my AdminSceneController.java
package appController;
import appDesign.PhotoAlbum;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
public class AdminSceneController {
public class MainSceneController {
#FXML
Button logoutButton;
#FXML
public void logout(ActionEvent event) throws Exception {
PhotoAlbum.primaryStage.show();
((Node)(event.getSource())).getScene().getWindow().hide();
}
}
}
I get a warning from Eclipse saying:
The controller 'AdminSceneController' has no event slot 'logout'
And when I run the program, I get the error:
javafx.fxml.LoadException: Error resolving onAction='#logout', either the event handler is not in the Namespace or there is an error in the script.
/C:/Users/Peter/Documents/GitHub/PhotoAlbum40/bin/appDesign/AdminPanelScene.fxml:19
Can anybody help?
Your AdminSceneController does not have a logout method, your class MainSceneController does though.
Remove the line
public class MainSceneController {
and the closing }and it should work.
You have to understand, that an inner class (MainSceneController) is not the same class as the enclosing class (AdminSceneController). By using fx:controller="appController.AdminSceneController" in the fxml a instance of AdminSceneController is created. This class however does not contain a single method or field. This causes the error.
Furthermore note that the FXMLLoader does not allow you to create non-static inner classes. If you want the FXMLLoader to create your controller instance, you have to make MainSceneController static and use fx:controller="appController.AdminSceneController$MainSceneController".
Ways around this would be specifying a controllerFactory or creating the controller instance yourself:
FXMLLoader loader = new FXMLLoader(getClass().getResource(...));
AdminSceneController enclosingInstance = new AdminSceneController(); // or any other way to get your hands on a instance of the enclosing class
// specify controller instance used yourself
loader.setController(enclosingInstance.new MainSceneController());
...
loader.load()
Which requires you to remove the fx:controller attribute from the fxml.
Of course you could also simply move the field / method to a top level class...

Container for image

I'm programming a app with JavaFX and Scene Builder. I want to create a container and insert an image inside.
But, the container have a size so if the image goes out that limits you can't see this image.
For Example, make a container and make the image too much bigger outside that, but just see what is inside the container.
Is that possible?
Solution
Use an ImageView (which is a node container for an Image). You can set a viewport on the ImageView to have the view render a specific part of an image.
Alternative Implementation
Specify your image in CSS and use combinations of -fx-background-image, -fx-background-repeat, -fx-background-position and -fx-background-size as defined in the JavaFX CSS reference guide.
The rest of this answer deals with just the FXML based solution and not the CSS based solution.
Code Based Sample
Here is a code snippet (adapted from the ImageView javadoc) which demonstrates setting a viewport in code:
Image image = new Image("flower.png");
ImageView view = new ImageView();
view.setImage(image);
Rectangle2D viewportRect = new Rectangle2D(40, 35, 110, 110);
view.setViewport(viewportRect);
FXML Based Sample
Here is some FXML to demonstrate the viewport approach.
<ImageView pickOnBounds="true">
<image>
<Image url="http://icons.iconarchive.com/icons/vincentburton/diaguita-ceramic-bowl/128/Diaguita-Ceramic-Bowl-4-icon.png" />
</image>
<viewport>
<Rectangle2D minX="35.0" minY="55.0" width="55.0" height="40.0" />
</viewport>
</ImageView>
Here is a complete example which you can load up into SceneBuilder. The first ImageView displays an unclipped image, the second ImageView displays a clipped image.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.geometry.Rectangle2D?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<StackPane id="StackPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="-1.0" prefWidth="-1.0" style="-fx-background-color: burlywood;" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<VBox alignment="CENTER" prefHeight="-1.0" prefWidth="-1.0" spacing="10.0" style="-fx-background-color: cornsilk;">
<children>
<ImageView pickOnBounds="true">
<image>
<Image url="http://icons.iconarchive.com/icons/vincentburton/diaguita-ceramic-bowl/128/Diaguita-Ceramic-Bowl-4-icon.png" />
</image>
</ImageView>
<ImageView pickOnBounds="true">
<image>
<Image url="http://icons.iconarchive.com/icons/vincentburton/diaguita-ceramic-bowl/128/Diaguita-Ceramic-Bowl-4-icon.png" />
</image>
<viewport>
<Rectangle2D height="40.0" minX="35.0" minY="55.0" width="55.0" />
</viewport>
</ImageView>
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</VBox>
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</StackPane>
On using the FXML Based Sample in SceneBuilder
To generate the FXML, the majority of the work was done in SceneBuilder, but setting the viewport was done by hand editing the FXML saved from SceneBuilder (because SceneBuilder 1.1 does not possess the UI to set the viewport on ImageViews from within the SceneBuilder tool). After hand-editing the FXML to add the viewport, you can reload the FXML in SceneBuilder and SceneBuilder will render the viewport in the hand-edited FXML fine.
Also, SceneBuilder 2 build 14 preview did not display images that are located using the http protocol (SceneBuilder 1.1 did not have an issue with this).
Attribution
Icon used in the answer is licensed CC Attribution-Noncommercial-Share Alike 3.0 with a linkback to the icon author.

Netbeans says class does not exist in FXML file for observablearrayList but it does

I have this FXML file generated by Netbeans (but I have modified the TableColumn bit):
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.collections.*?>
<AnchorPane id="AnchorPane" fx:id="MainPane" prefHeight="529.0" prefWidth="513.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="d1example2.UserInterfaceController">
<children>
<SplitPane dividerPositions="0.17835671342685372" focusTraversable="true" layoutX="14.0" layoutY="14.0" orientation="VERTICAL" prefHeight="501.0" prefWidth="485.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<AnchorPane id="AnchorPane" layoutX="95.0" layoutY="32.0">
<children>
<TextField fx:id="FirstField" layoutX="0.0" layoutY="0.0" prefWidth="51.0" />
<Text fx:id="TimesText" layoutX="65.0" layoutY="16.0" strokeType="OUTSIDE" strokeWidth="0.0" text="*" />
<TextField id="FirstField" layoutX="90.0" layoutY="0.0" prefWidth="51.0" />
<Text fx:id="EqualSign" layoutX="168.0" layoutY="16.0" strokeType="OUTSIDE" strokeWidth="0.0" text="=" />
<Text fx:id="EquationResult" layoutX="245.0" layoutY="16.0" scaleX="1.632154219166214E8" scaleY="1.436917974231407" strokeType="OUTSIDE" strokeWidth="0.0" text="" />
</children>
</AnchorPane>
<Button fx:id="SubmitButton" layoutX="356.0" layoutY="32.0" mnemonicParsing="false" onMouseClicked="#handleButtonAction" text="Submit" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TableView layoutX="13.0" layoutY="14.0" prefHeight="379.0" prefWidth="456.0">
<columns>
<TableColumn text="Half Value">
<cellValueFactory><PropertyValueFactory property="HalfContent" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Double Value">
<cellValueFactory><PropertyValueFactory property="DoubleContent" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<Data HalfContent="First Column Text" DoubleContent="Second Column Text" />
</FXCollections>
</items>
</TableView>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
And this other class called "Data" which looks like this:
package d1example2;
import javafx.beans.property.SimpleStringProperty;
/**
*
* #author zmeshign
*/
public final class Data {
private final SimpleStringProperty HalfContent = new SimpleStringProperty("");
private final SimpleStringProperty DoubleContent = new SimpleStringProperty("");
public Data() {
this("", "");
}
public Data(String HalfContent, String DoubleContent) {
setHalfContent(HalfContent);
setDoubleContent(DoubleContent);
}
public String getHalfContent() {
return HalfContent.get();
}
public void setHalfContent(String hContent) {
HalfContent.set(hContent);
}
public String getDoubleContent() {
return DoubleContent.get();
}
public void setDoubleContent(String dContent) {
DoubleContent.set(dContent);
}
}
So in this line:
<Data HalfContent="First Column Text" DoubleContent="Second Column Text" />
it says that the class "Data" does not exist while I have it under the same package. All I'm trying to do is to learn how to display a string under a column in a TableView.
I would immensely appreciate any help!
Thanks
How to fix it
In your case the appropriate directive you need to add to the FXML file in order for it to find your Data class is:
<?import d1example2.Data?>
Why it was failing for you
FXML files are just files, they are not compiled to Java classes (at least not in the JavaFX 2.2 version). This means that the files themselves aren't really aware of what package they are in (as packaging is a Java concept). The FXML files can be told where to locate the Java classes they need though. To do this you use the <?import java.lang.*?> directive at the top of the file (substituting your package name to perform the appropriate import).
Update based on additional question
This fixed the problem with the "data" part, but now it's saying: Class d1example2.Data does not support property "HalfContent". It also says this with "DoubleContent".
You need to follow Java property naming conventions. Under these conventions property names start with a lower case letter rather than an uppercase letter, so use halfContent and doubleContent as your property names in FXML.
Instead of:
<Data HalfContent="First Column Text" DoubleContent="Second Column Text" />
Write:
<Data halfContent="First Column Text" doubleContent="Second Column Text" />
Additionally, you might want to define property accessors for your properties:
public StringProperty halfContentProperty() {
return halfContent;
}
. . .

Categories