TreeTableColumn.visible : A bound value cannot be set - java

I make a simple JavaFX application. In this application there is a treetable with 2 columns and a check box. If check box is selected column 2 will be visible, otherwise not visible. To do this I bound tree table column visible property to checkbox selected property. When I click the check box column state change but at the same time gives.
Caused by: java.lang.RuntimeException: TreeTableColumn.visible : A
bound value cannot be set.
If I use bidirectional binding I don't get this error. But I don't need bidirectional binding. Is it a bug or I don't use bind correctly? Please use below code to reproduce this error. I use jdk1.8.0_111.
JavaFXApplication.java
package test;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JavaFXApplication extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
FXMLDocumentController.java
package test;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
public class FXMLDocumentController implements Initializable {
#FXML
private TreeTableView<?> table;
#FXML
private CheckBox box;
#FXML
private TreeTableColumn<?, ?> c2;
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
c2.visibleProperty().bind(box.selectedProperty());
}
}
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.TreeTableColumn?>
<?import javafx.scene.control.TreeTableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="390.0" prefWidth="452.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.65" fx:controller="javafxapplication2.FXMLDocumentController">
<children>
<TreeTableView fx:id="table" layoutX="2.0" prefHeight="390.0" prefWidth="149.0">
<columns>
<TreeTableColumn prefWidth="75.0" text="C1" />
<TreeTableColumn fx:id="c2" prefWidth="75.0" text="C2" visible="false" />
</columns>
</TreeTableView>
<CheckBox fx:id="box" layoutX="234.0" layoutY="77.0" mnemonicParsing="false" text="CheckBox" />
</children>
</AnchorPane>

I think this actually is not a bug. It's definitely mysterious and unexpected behavior though. Part of the problem is that the binding conflict is coming from TableHeaderRow, which is created by the skin at display time.
The TableHeaderRow is responsible for rendering all the column headers, and it includes this built-in button for a menu that, by default, is a radio selection list of columns to show/hide:
As a result, the TableHeaderRow creates a bidirectional binding between these menu entries' selection state and each column's visible property.
It actually is possible to undo this binding but I found it annoying since the TableHeaderRow is null until the TableView is displayed. But, adapting this solution to access the TableHeaderRow, we can do something like:
TableHeaderRow headerRow = ((TableViewSkinBase) tableView.getSkin()).getTableHeaderRow();
try {
// get columnPopupMenu field
Field privateContextMenuField = TableHeaderRow.class.getDeclaredField("columnPopupMenu");
// make field public
privateContextMenuField.setAccessible(true);
// get context menu
ContextMenu contextMenu = (ContextMenu) privateContextMenuField.get(headerRow);
for (MenuItem menuItem : contextMenu.getItems()) {
// Assuming these will be CheckMenuItems in the default implementation
BooleanProperty selectedProperty = ((CheckMenuItem) menuItem).selectedProperty();
// In theory these menu items are in parallel with the columns, but I just brute forced it to test
for (TableColumn<?, ?> tableColumn : tableView.getColumns()) {
// Unlink the column's visibility with the menu item
tableColumn.visibleProperty().unbindBidirectional(selectedProperty);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
// Not strictly necessary but we don't want to be misleading :-p
tableView.setTableMenuButtonVisible(false);
But here's the kicker: you have to do this every time a column's visibility changes, because the TableHeaderRow has a listener that rebuilds the menu, and re-links the properties, every time a column is shown.
You could, of course, find a way to disable that listener.. but clearly this is already ridiculous and probably unnecessary.
Anyway, as you noted:
If I use bidirectional binding I don't get this error. But I don't need bidirectional binding.
I think the second statement isn't technically true: your use case doesn't require bidirectional binding, but it is actually appropriate here since there are other properties already linked with a column's visibility.
So, I would use bidirectional binding.

Related

Is it possible to attach a Text Formatter to a TextField directly as an attribute?

Controller Class
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.util.StringConverter;
import javafx.util.converter.IntegerStringConverter;
import java.util.function.UnaryOperator;
public class Controller {
#FXML
TextField principal;
UnaryOperator<TextFormatter.Change> integerFilter = change -> {
String newText = change.getControlNewText();
if (newText.matches("-?([1-9][0-9]*)?")) {
return change;
}
return null;
};
StringConverter<Integer> converter = new IntegerStringConverter() {
#Override
public Integer fromString(String s) {
if (s.isEmpty()) return 0 ;
return super.fromString(s);
}
};
TextFormatter<Integer> textFormatter = new TextFormatter<Integer>(converter, 0, integerFilter);
}
FXML file
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.TextField?>
<GridPane fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<TextField fx:id="principal" textFormatter="textFormatter"/>
</GridPane>
So I've got this text field that I want only take numeric input. After digging a bit I found out that a Text Formatter is best suited for the task. I was wondering if there was a way to attach a text formatter(textFormatter) as an attribute directly in the FXML file(like textFormatter="textFormatter") cause whenever I try to set it in the controller file, I either get a null pointer exception or it just doesn't work(i.e. the field still takes non-numeric input)

Internal NPE when launching JavaFX Application

So basically I began a dummy JavaFX project just to achieve a minimalistic example for my actual problem. But now I am not even able to run that minimalistic project anymore and do not receive enough error information to actually google it myself out. So right now, when I run the code, I receive the given error stack, which does not lead me anywhere.
I am using IntelliJ. JavaFX libraries are set correctly and VM Options set to:
--module-path "C:\Program Files\Java\javafx-sdk-11.0.2\lib" --add-modules javafx.controls,javafx.fxml
On top, when I run the code, those errors pop up in console, but the application seems to still be running, because I need to press the Red Stop Button of IntelliJ to actually stop it.
Has anyone some guess, what goes wrong here? I am not experienced enough to follow those errors, since they do not point into my code, but rather into some Deep Java code.
The Exception:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.NullPointerException
at java.base/java.lang.reflect.Method.invoke(Method.java:559)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
... 5 more
Main.java:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
private Stage rootStage;
public BorderPane mainWindow;
public AnchorPane left;
public AnchorPane bottom;
#Override
public void start(Stage primaryStage) throws Exception {
this.rootStage = primaryStage;
loadMainWindow();
}
public void loadMainWindow() throws IOException {
FXMLLoader loaderMainWindow = new FXMLLoader(Main.class.getResource("MainWindow.fxml"));
mainWindow = loaderMainWindow.load();
FXMLLoader loaderLeft = new FXMLLoader(Main.class.getResource("Left.fxml"));
left = loaderLeft.load();
mainWindow.setLeft(left);
//mainWindow.setBottom(bottom);
Scene scene = new Scene(mainWindow);
rootStage.setScene(scene);
rootStage.show();
}
public void main(String[] args) {
launch(args);
}
}
MainWindow.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.MainWindowController" />
MainWindowController:
package sample;
import javafx.fxml.Initializable;
import java.net.URL;
import java.util.ResourceBundle;
public class MainWindowController implements Initializable {
private Main main;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
public void setMain(Main main) {
this.main = main;
}
}
Left.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="100.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.LeftController">
<children>
<Button fx:id="button" layoutX="237.0" layoutY="169.0" mnemonicParsing="false" onAction="#buttonClick" text="Button" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" />
</children>
</AnchorPane>
LeftController.java:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import java.awt.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;
public class LeftController implements Initializable {
#FXML
private Button button;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
public void buttonClick(javafx.event.ActionEvent actionEvent) {
System.out.println("Some Stuff");
}
}
Solution
The error you're getting is caused by your main(String[]) method not being static. If you make it static then the error will go away.
Some Explanation
JavaFX offers the ability to launch applications without providing a main method, so long as the main class is a subclass of Application. However, developers can still include a main method which means this special launch functionality has to handle that situation gracefully. In other words, an explicit main method present in the Application subclass must act like the entry point of the application from the developer's point of view. Nonetheless, behind the scenes some deep internal class has become the "real" main class.
To do this, the main method is located—if present at all—via Class#getMethod(String,Class...) which, while only returning public methods, doesn't distinguish between static and non-static methods. If found, Method#invoke(Object,Object...) is used to invoke the main method reflectively. The first argument of invoke is the instance that the method should be invoked on; in the case of static methods the value is null. Unfortunately, the code assumes the method it found is static which causes a NullPointerException to be thrown—you can't call an instance method on a null "instance".
Update: This issue has been submitted on GitHub (#570) and subsequently JBS (JDK-8230119). The current idea is to emit a warning rather than throw the NullPointerException. However, the functionality that allows launching without a main method may be deprecated in a future release, which will affect how this issue is addressed.

Connecting SQL Server with Java (javafx)

I have the setup below for the connection SQL Server with Java. Am also using javafx. I am very new to developing with Java. Note: I added the sqljdbc driver. I don't want to add main to DBConnection because am using the Connection method in the controller. Is there a way to fix this or how can I add main method without changing the connection method? Am getting error message:
Error Message
Error: Main method not found in class application.ConnectionDB, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:controller="application.MainController" prefHeight="407.0" prefWidth="578.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label layoutX="123.0" layoutY="65.0" prefHeight="31.0" prefWidth="79.0" text="Date:" />
<Label layoutX="123.0" layoutY="138.0" prefHeight="25.0" prefWidth="127.0" text="Rim Current Value:" />
<Label layoutX="125.0" layoutY="210.0" text="Rim Sales Value for the Month:" />
<Label layoutX="123.0" layoutY="283.0" text="Rim Sold Cost Bought:" />
<TextField fx:id="txtdate" layoutX="123.0" layoutY="97.0" />
<TextField fx:id="txtcurvalue" layoutX="123.0" layoutY="163.0" />
<TextField fx:id="txtsalesvalue" layoutX="123.0" layoutY="234.0" />
<TextField fx:id="txtsoldcost" layoutX="123.0" layoutY="300.0" />
<Button layoutX="246.0" layoutY="340.0" mnemonicParsing="false" onAction="#Rmsubmit" prefHeight="25.0" prefWidth="103.0" text="Submit" />
</children>
</AnchorPane>
Connection Class
package application;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ConnectionDB
{
public static Connection dbConn() {
Connection conn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver:[server name];database=SalesManager;user=[username];password=[password];encrypt=true;trustServerCert ificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30";
conn = DriverManager.getConnection(url);
}
catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(ConnectionDB.class.getName()).log(Level.SEVERE,null,ex);
}
return conn;
}
}
Controller
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javax.print.DocFlavor.URL;
public class MainController {
#FXML
public TextField txtdate;
#FXML
public TextField txtcurvalue;
#FXML
public TextField txtsalesvalue;
#FXML
public TextField txtsoldcost;
public Connection conn =null;
public PreparedStatement pat = null;
#FXML
public void Rmsubmit(ActionEvent actionEvent) {
String sqla = "Insert into RimCalc(Date, Rim_Vale,Rim_Sales,Rim_Cost) Values (?,?,?,?)";
String date = txtdate.getText();
String rim_value = txtcurvalue.getText();
String Rim_Sales = txtsalesvalue.getText();
String rim_cost = txtsoldcost.getText();
try {
pat = conn.prepareStatement(sqla);
pat.setString(1, date);
pat.setString(2, rim_value);
pat.setString(3, Rim_Sales);
pat.setString(4, rim_cost);
int i = pat.executeUpdate();
if(i==1) {
System.out.println("Insert Successfully");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void initializer(URL url, ResourceBundle rb) {
conn = application.ConnectionDB.dbConn();
}
}
Java programs require a main method as an entry point in order to run. The standard definition of a main method is like this:
public static void main(String[] args) {
// First code of your program goes here
}
This main() method should include the code that starts your application and loads any interface elements for display.
A JavaFX application also needs to have a class that extends Application and override its start() method.
Here is a very quick and dirty example of one such class:
import javafx.application.Application;
import javafx.stage.Stage;
class Main extends Application {
public static void main(String[] args) {
launch(args); // Starts the JavaFX application and calls the start() method
}
#Override
public void start(Stage primaryStage) throws Exception {
// Here is where you'll initialize your views and such
}
}
I would recommend taking a few Java and JavaFX tutorials to get a feel for some of the basics before attempting more complicated tasks like connecting to databases.

resolving onAction='#initialize', either the event handler is not in the Namespace or there is an error in the script

This is my first program in javafx. I have just created a AnchorPane with the help of scene Builder. what I want from this is when we click any where in the AncherPane it should show the coordinate point of that pixel.
This is the controller class
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
public class AxisController implements Initializable{
#FXML
private AnchorPane anchr;
#Override
public void initialize(URL location, ResourceBundle resources) {
assert anchr != null : "fx:id=\"anchr\" was not injected: check your FXML file 'AxisFxml.fxml'.";
anchr.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println(event.getX());
System.out.println(event.getY());
}
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="anchr" onMouseClicked="#initialize" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.AxisController">
<!-- TODO Add Nodes -->
</AnchorPane>
Error Message
javafx.fxml.LoadException: Error resolving onAction='#initialize', either the event handler is not in the Namespace or there is an error in the script.
/home/xyz/workspace/AxisExample/bin/application/AxisFxml.fxml:9
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
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 application.AxisMain.start(AxisMain.java:16)
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.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:745)
You have a couple of misunderstandings here.
The methods whose names you set as values to the "event handler attributes", such as onMouseClicked, are the methods that will be invoked when those events happen. They should just contain the code that you want to be executed when, in this case, the mouse is clicked. You should not register event handlers in these methods. (This would mean: "when the anchor pane is clicked, register an event handler for the anchor pane being clicked", which is clearly not what you intend.)
As a consequence of this, methods that are registered in the FXML as event handler methods should take a single parameter of the correct event type. (It is also allowable to take no parameters, if you don't need the event object - a common scenario.)
The error you are getting is because the method you have declared to be the event handler (initialize(), which has a completely different role), has the wrong signature: it take the wrong number (and type) of parameters.
Secondly, the initialize() method, which may either be declared with no parameters, or be declared with parameters of type URL and ResourceBundle, is invoked during the process of loading the FXML. This happens after all the #FXML-annotated fields have been injected, but before the root of the UI is returned from the call to FXMLLoader.load(). Thus it's safe to access #FXML-annotated fields here: they will have been properly initialized.
So there are two distinct ways of registering event handlers.
Either:
Define a method in the controller, and reference it in the FXML file:
<AnchorPane fx:id="anchr" onMouseClicked="#handleClick" ... fx:controller="application.AxisController">
<!-- TODO Add Nodes -->
</AnchorPane>
and then
public class AxisController implements Initializable{
#FXML
private AnchorPane anchr;
#Override
public void initialize(URL location, ResourceBundle resources) {
assert anchr != null : "fx:id=\"anchr\" was not injected: check your FXML file 'AxisFxml.fxml'.";
}
#FXML
private void handleClick(MouseEvent event) {
System.out.println(event.getX());
System.out.println(event.getY());
}
}
Or:
Remove the onMouseClicked attribute from the FXML file, and register the event handler in the initialize() method. (So the controller is exactly as you have it.)
I would assume that the problem lies in your anchorPane on your FXML, have you assigned a function to the anchorPane for a click event on your FXML? if you have you can set the function for that event by doing the same as if you where initializing the variable for example:
#FXML
private void restartExited() {
exitedTransition(0, "circle", null, buttonCirc);
}
you can tell your anchorPane in the FXML to do this function when it is clicked or mouseOver for example, you do not need to set it in the controller as well as the FXML.
Or in your case:
#FXML
private void initialize() {
anchr.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println(event.getX());
System.out.println(event.getY());
}
});
}
Or you could remove the reference to onClick on the AnchorPane in your FXML and use your current code.
EDIT
Can I ask where you are defining the stage and the FXML to load to the stage, is this your stage initializer for your application or the controller for the FXML?

JavaFx 2.x TableView binding columns

Hello i`m trying to populate my JavaFx TableView with ObservableList like this:
emf = Persistence.createEntityManagerFactory("shopPu");
em = emf.createEntityManager();
List<Products> proList = em.createQuery("select p from Products p").getResultList();
ObservableList<Products> proObs = FXCollections.observableList(proList);
tableView.setEditable(true);
tableView.setItems(proObs);
Its works without an error my List is filling with data but TableView does not showing anything.
Here is my FXML
<TableView fx:id="tProducts" prefHeight="246.0" prefWidth="726.0" AnchorPane.bottomAnchor="160.0" AnchorPane.leftAnchor="7.0" AnchorPane.rightAnchor="7.0" AnchorPane.topAnchor="70.0">
<columns>
<TableColumn text="ID" fx:id="ID"/>
</columns>
</TableView>
i tried this:
<TableView fx:id="tProducts" prefHeight="246.0" prefWidth="726.0" AnchorPane.bottomAnchor="160.0" AnchorPane.leftAnchor="7.0" AnchorPane.rightAnchor="7.0" AnchorPane.topAnchor="70.0">
<columns>
<TableColumn text="ID" fx:id="ID">
<cellValueFactory>
<PropertyValueFactory property="id" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
But no luck its gives such error:
javafx.fxml.LoadException: PropertyValueFactory is not a valid type.
at javafx.fxml.FXMLLoader.createElement(Unknown Source)
at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
Please help how to populate TableView
UPDATE
Controller:
public class MainWindow implements Initializable {
#FXML private Label lblStatus;
#FXML private TableView<Products> tableView;
private EntityManager em;
private EntityManagerFactory emf;
#FXML
private void Refresh_Clicked(javafx.event.ActionEvent event) {
try {
emf = Persistence.createEntityManagerFactory("shopPu");
em = emf.createEntityManager();
List<Products> proList = em.createQuery("select p from Products p").getResultList();
ObservableList<Products> proObs = FXCollections.observableList(proList);
tableView.setEditable(true);
tableView.setItems(proObs);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
Thanks.
According to yours FXML your TableView should by named tProducts. But this should create error during injection, could You please paste controller code?
Without analyzing your code example any further, the posted error is probably caused by a missing import in the fxml for PropertyValueFactory.
I solved a semelhant problem in my project adding this line in the FXML file.
<?import javafx.scene.control.cell.PropertyValueFactory ?>
you have to make same name of your TableView and fxid: in code.
<TableView fx:id="table" layoutX="27.0" layoutY="65.0" prefHeight="193.0" prefWidth="552.0">
TableView table;
you have to initalize table columns...and define their property.
try this..
#FXML
private TableColumn<CheckDo, String> username;
username.setCellValueFactory(new PropertyValueFactory<CheckDo,String>("username"));
you have to set setvaluefactory of columns for show values of it

Categories

Resources