JavaFX-SceneBuilder - Can't access fx:id when opening new window - java

I have this code (part of a quite big application):
The following function is located in the controller class of the project.
public void callSearch(){
Stage supplyStage = new Stage();
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("supplyResult.fxml"));
supplyStage.setTitle("Supply Result Set");
supplyStage.setScene(new Scene(root, 1400, 900));
supplyStage.show();
} catch (IOException e) {
e.printStackTrace();
}
try {
typeColumn.setCellValueFactory(new PropertyValueFactory<Supply, String>("type"));
nameColumn.setCellValueFactory(new PropertyValueFactory<Supply, String>("name"));
dateColumn.setCellValueFactory(new PropertyValueFactory<Supply, String>("order_date"));
pricePerUnitColumn.setCellValueFactory(new PropertyValueFactory<>("pricePerUnit"));
quantityColumn.setCellValueFactory(new PropertyValueFactory<>("quantity"));
}
catch (NullPointerException npe){
System.out.println("Null...");
}
searchSupplyStartDate = java.sql.Date.valueOf(startDateDatePicker.getValue());
searchSupplyEndDate = java.sql.Date.valueOf(endDateDatePicker.getValue());
tableOutput.setItems(myMain.getSupplies(searchSupplyStartDate,searchSupplyEndDate,null,null));
}
My problem is that I get a NullPointerException in the line typeColumn.setCellValueFactory(new PropertyValueFactory<Supply, String>("type"));
In previous version of the application I had the columns in the same window as the rest of the scene. Now I try to add a second window when displaying the same stuff, to make the first scene a bit more beautiful. (the code was working when I had one scene).
In fact, to make things clear, i want with the press of a button, the function callSearch() to be called. Then a window appears, with a table of 5 columns attached: typeColumn, nameColumn, dateColumn, pricePerUnitColumn, quantityColumn. Those 5 columns are declared in the (only) controller of the project, so i thought that they are visible in any fxml file in the project, but that's not the case.
Does anybody know what am I missing? the typeColumn is visible from the SupplyResult.fxml in scene builder, but does not seem to execute the code it has to...

Related

Not switching to next FXML file?

Im making a button that switches to the next scene in Javafx, ie switching to the next FXML file.
Here is the code:
public void submitorder(ActionEvent actionEvent) throws IOException {
Customer cust = new Customer();
cust.setOrder(order);
tbl.getCustomers().add(cust);
if (tbl.getCustomers().size() == noOfDiners) {
System.out.println("if statement");
allcustomers = tbl.getCustomers();
try{
System.out.println("This prints");
Window mainWindow = btnSubmitOrder.getScene().getWindow();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("paymentScreen.fxml"));
Parent root = loader.load();
System.out.println("test");
PaymentScreenController psc = loader.getController();
psc.dataReceiver(allcustomers);
mainWindow.getScene().setRoot(root);
} catch(Exception e){
System.out.println(e.getMessage());
}
}
lvOrder.getItems().clear();
}
So that first print statement in the try method prints out so it goes into the try method but it still doesnt switch the scene. Ive used this exact code elsewhere in my program and it works so it dont know what im doing wrong.
Ive also tried not using a try method but i just get an error. In this case nothing happens
That if statement is also correct
Any help would be appreciated thanks

Error displaying some PDFs with OpenViewerFX

I am using OpenViewerFX (JPedal) to display PDF files in a JavaFX application. Some files are displayed well, others only show not printable characters (in documents created by myself and also documents from other people).
See for example this one:
This is my code:
private final PdfDecoderFX pdf = new PdfDecoderFX();
private Group group;
#Override
public void start(Stage stage) {
BorderPane bp = new BorderPane();
Scene scene = new Scene(bp, 400, 400);
stage.setScene(scene);
stage.show();
group = new Group();
group.getChildren().add(pdf);
bp.setCenter(group);
Platform.runLater(() -> {
try {
pdf.openPdfFile("D:\\Dokument1.pdf");
decodePage();
} catch (PdfException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
});
}
private void decodePage() {
try {
pdf.setPageParameters(2.0f, 0);
pdf.decodePage(1);
pdf.waitForDecodingToFinish();
} catch (final Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
The PDF file was created from a Word document with Adobe PDF printer. I used the standard font and only standard characters. I tested several settings when creating the file, including
Compatibility "PDF 1.3" to "PDF 1.7"
Enable/Disable web optimization
Include all fonts
Include Open-Type fonts
but always the same result.
What could I be doing wrong?
Seems like a font issue to me. The debugging output helped me in the past - at least I saw some error message in there. I'm not quite sure what the correct option was but try the following:
Did you try to enable jpedal logging?
//debug code
LogWriter.log_name="/yourpath/log.txt";
LogWriter.setupLogFile(true,0,"1.0","v",false);
What does the org.jpedal.PdfDecoder.getPageDecodeStatus(int type)state where type is any value Defined in org.jpedal.parser.DecodeStatus
We use a patched version of the ViewerFX - can you try that one?
Are you sure you specified the correct path to the fonts?
To add a whole directory of fonts via JVM flag set the JVM flag -Dorg.jpedal.fontdirs=dirList where dirList is a comma-separated list of possible directories.
What is your OS? Windows or Linx?
Did you also try the -Dorg.jpedal.inclusiveLogFilter= "memory,error"JVM option?
Try to set -Dverbose=true
Call GUI.debugFX=true; see here.

Java FX - Associate a second fxml file to a second controller file

I try to developp an application and I work on Eclipse.
First I worked on four files :
Main.java, that launch the interface by using the sheet1.fxml file
MyController.java, that declares the button and the anchor of the fxml file sheet1. That file implements an action event : to go on a second interface (sheet2.fxml) after clicking on the button of the first fxml file (that is sheet1).
Now I want to work on the second interface, sheet2.fxml. I'd like to :
- add text based on a count of files in a folder
- create buttons in order to go on a third interface
But my question is how could I do ?
I tried to create a second controller to make the declaration of the Text nbExcel and buttons then build the relation with sheet2.fxml file but I don't see the trick.
How to associate events that run the components located on that sheet2 and that new controller ?
I began to do that on "myController2" :
public class myController2 extends Application {
// How and where to associate that controller with the fxml file "sheet2" ?
public void start2() throws IOException{
Stage primaryStage2 = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("sheet1.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage2.setScene(scene);
primaryStage2.show();
}
#FXML
// declaration text in order to count files in folder in sheet2 interface
private Text nbExcel;
// declaration action buttons in sheet2 interface
// To do
// 1 - INITIALISATION
public void initialize(URL location, ResourceBundle resources) {
File a = new File("C:/Controles/Excel");
int b = 0;
for (File file : a.listFiles()) {
if (file.isFile() && (file.getName().endsWith(".xlsx") )) {
b++;
}
}
nbExcel.setText(Integer.toString(b));
}
Any help would be really appreciated.
Thank you !
In order to connect the fxml file and create the new scene you could do something like this:
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/application/sheet2.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
EDIT: To confirm this bit of code should be in the controller class that you have shown in your question.

Decorated and not-movable stage in JavaFx

I want to create a decorated stage in JavaFx and it will be not movable too. I am creating this stage from another controller class. I am able to create and show the stage but it is moving freely. How can I create this? Help and suggestions are highly appreciated. I am pasting the method for opening new stage.
private void addReceiver() {
Parent receivercvDetailsparent;
try {
receivercvDetailsparent = FXMLLoader.load(getClass().getClassLoader().getResource("com/project/fxml/ReceiverDetails.fxml"));
Stage stageReceiver = new Stage();
stageReceiver.initModality(Modality.APPLICATION_MODAL);
stageReceiver.setTitle("AppName v2.0.0");
stageReceiver.setScene(new Scene(receivercvDetailsparent));
stageReceiver.initStyle(StageStyle.DECORATED);
stageReceiver.setResizable(false);
stageReceiver.getIcons().add(new Image("/com/project/images/logo.png"));
stageReceiver.show();
} catch (IOException e) {
e.printStackTrace();
}
}
A stage is a window usually so the only way to sort of fake it's immovability is to make it undecorated
stage.initStyle(StageStyle.UNDECORATED);
It only prevents window dragging automatically, but I'm afraid it is the only thing you can do

Load FXML in initialize()

I'm creating my first JavaFX application, and I'm doing OK so far. I'm just encountering one problem.
For displaying and loading FXML files I'm using a VistaNavigator class found on StackOverflow:
public static void loadVista(String fxml) {
try {
mainController.setVista(
FXMLLoader.load(
VistaNavigator.class.getResource(
fxml
)
)
);
} catch (IOException e) {
e.printStackTrace();
}
}
I have a ScanController, which receives input from the keyboard and checks a ticket ID based on this input. When the ticket is OK, it loads "scan-success.fxml", otherwise it loads "scan-failure.xml", each of these FXML-files have an own controller. I'm loading the success FXML like this:
VistaNavigator.loadVista(VistaNavigator.VISTA_SCAN_SUCCESS);
This is working great. The Success-FXML page is showing and the ScanSuccessController is loading. In the initialize() method of ScanSuccessController.java I have the following code snippet:
try {
Thread.sleep(2000); //1000 milliseconds is one second.
VistaNavigator.loadVista(VistaNavigator.VISTA_SCAN_HOME);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
So I would like to show the success-page for about 2 seconds, and then head back to the home screen (scan-home.fxml with controller ScanHomeController.java) to scan some more tickets. However, when executing this code, just 'nothing' happens, no exception and no change of FXML-file.
When I try to load a new vista after clicking a button (in an EventHandler) it works great.
I can imagine that JavaFX is not able to load a new FXML-file before the controller has been fully initialised, but cannot figure out where to put this line of code..
I hope someone can help me out with this.
What about this:
#Override
public void initialize(URL url, ResourceBundle rb) {
Timeline timeline=new Timeline();
timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(2),
e->VistaNavigator.loadVista(VistaNavigator.VISTA_SCAN_HOME)));
timeline.play();
}
Note that by using a Timeline everything runs in the JavaFX thread.

Categories

Resources