JavaFx - Dynamic JXML, add Pane element on Initialize - java

i am new in JavaFX, and as any newcomer, i am flood with doubts.
So i have to populate an <AnchorPane> with children <Pane> like this FXML code:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<AnchorPane fx:id="anchor" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<!-- Add multiple Panels here-->
</children>
</AnchorPane>
My Controller looks like this:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
#FXML javafx.scene.layout.AnchorPane anchor;
#FXML javafx.scene.layout.Pane pane;
#Override
public void initialize(URL location, ResourceBundle resources) {
pane.prefHeight(100);
pane.prefWidth(300);
pane.setStyle("-fx-background-color: aqua");
for(int i = 0; i < 3; i++) {
anchor.getChildren().add(pane);
pane.setLayoutY(i*100);
}
}
}
Hope someone can help me... Thank You!!

The solution for it is...
The FXML code:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<AnchorPane fx:id="anchor" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<ScrollPane prefHeight="400.0" prefWidth="300.0">
<content>
<GridPane fx:id="gridPane" prefHeight="410.0" prefWidth="294.0"></GridPane>
</content>
</ScrollPane>
</children>
</AnchorPane>
The Controller Class:
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
#FXML private GridPane gridPane;
private Pane paneContainer;
private Label paneLabel;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
for(int i = 0; i<4; i++) {
paneLabel = new Label();
paneLabel.setText("it is..." + i);
paneContainer = new Pane();
paneContainer.setStyle("-fx-background-color: aqua; -fx-border-style: solid; -fx-border-width: 1px; -fx-border-color:#000; ");
paneContainer.setPrefWidth(200);
paneContainer.setPrefHeight(100);
paneContainer.getChildren().add(paneLabel);
gridPane.add(paneContainer, 0, i);
}
}
}
Use GridPane is the easiest way to do it!

Related

Why does my controller seem to not have the event slot 'onShowClick'? [duplicate]

I want to add a method to a button which is defined in my Controller class
in the console is only an error which tells me that it couldn't find the method
here is the code
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Controller">
<children>
<Button layoutX="126" layoutY="90" text="lololol" onAction="#test" fx:id="button" />
</children>
</AnchorPane>
and the Controller.java
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import java.awt.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable
{
#Override
public void initialize(URL url, ResourceBundle resourceBundle)
{
}
#FXML
private void test(ActionEvent event)
{
System.out.println("lollolol");
}
}
Replace:
import java.awt.event.ActionEvent;
with:
import javafx.event.ActionEvent;

JavaFX Setting an Image to ImageView after selecting it

I want to select a picture on the HDD with the help of JFileChooser and display it in the GUI. The problem is that I can't set the selected image to the ImageView. I think I'm doing something completely wrong but I also tried various other ways to display it for example with BufferedImage but nothing worked for me. I don't get behind this, please help me.
Controller:
package application;
import java.io.File;
import java.net.MalformedURLException;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
public class WindowController {
#FXML private Button btn_load;
#FXML private TextField text_path;
#FXML private ImageView img_frame;
public Main main;
public void setMain(Main main) {
this.main = main;
}
#FXML
public void handle_load() throws MalformedURLException{
JFileChooser chooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = chooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
text_path.setText(chooser.getSelectedFile().getAbsolutePath());
File file = new File(chooser.getSelectedFile().getAbsolutePath());
String localURL = file.toURI().toURL().toString();
img_frame.setImage(localURL);
}
}
}
And the fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="700.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.WindowController">
<children>
<ImageView fx:id="img_frame" fitHeight="516.0" fitWidth="627.0" layoutX="142.0" layoutY="114.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="97.0" AnchorPane.leftAnchor="142.0" AnchorPane.rightAnchor="142.0" AnchorPane.topAnchor="114.0" />
<HBox layoutX="14.0" layoutY="14.0" prefHeight="25.0" prefWidth="885.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="12.0">
<children>
<Button fx:id="btn_load" mnemonicParsing="false" onAction="#handle_load" text="Load Image" HBox.hgrow="NEVER" />
<TextField fx:id="text_path" prefHeight="25.0" prefWidth="0.0" HBox.hgrow="SOMETIMES" />
</children>
</HBox>
</children>
</AnchorPane>
instead of adding it to a url, just add
img_view.setImage(new Image(url));
It will fix your problem

Javafx: Error resolving onAction // eventHandler not in the namespace

I am a complete beginner, messing around with javafx. My first try:
[FXML]
<?import javafx.scene.web.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<AnchorPane fx:controller="sample.Controller" 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" >
<children>
<Button fx:id="but0" layoutX="208.0" layoutY="146.0" mnemonicParsing="false" onAction="#Handle" text="Button" />
</children>
</AnchorPane>
And the controller class:
package sample;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class Controller {
#FXML
private Button but0;
#FXML
private void Handle(EventHandler e){
but0.setText("Bla");
}
}
which results in the following error:
Error resolving onAction='#Handle', either the event handler is not in the Namespace or there is an error in the script.
Although my Controller class is clearly set as controller for the parent AnchorPane.
The parameter of the handler method either needs to be a Event or it needs to be absent. The following 2 versions should both work:
#FXML
private void Handle(){
but0.setText("Bla");
}
#FXML
private void Handle(ActionEvent e){
but0.setText("Bla");
}
Your controller needs to implement Initializable, like so: (this is just the NetBeans default FXMLController, I'm assuming you load it with FXMLLoader.load() )
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class FXMLDocumentController implements Initializable { // notice this
#FXML
private Button but0;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked the button!");
}
#Override // and this
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Also, methods should start with a lowercase letter, "handle" instead of "Handle" ;) while the second version works, too, it is not considered good style.

Custom Control in javafx

I create a custom AnchorPane (ItemCard) in JavaFX to use it in another FXML file.
ItemCard.fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<fx:root type="AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<AnchorPane fx:id="ItemCardPane" prefHeight="360.0" prefWidth="266.0" stylesheets="#ItemCard.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<ImageView fx:id="ItemCardView" fitHeight="399.0" fitWidth="245.0" layoutX="11.0" layoutY="6.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" BorderPane.alignment="CENTER" />
</children>
</AnchorPane>
</children>
</fx:root>
the controller of ItemCard is:
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
/**
*
* #author ola
*/
public class ItemCard extends AnchorPane {
#FXML
ImageView ItemCardView;
#FXML
AnchorPane ItemCardPane;
public ItemCard() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ItemCard.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
public void setImage(Image image) {
ItemCardView.setImage(image);
// getChildren().add(ItemCardView);
}
}
i want to use this itemCard Anchore Pane in another fxml.
home.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import com.syrianep.reader.ui.KidsTheme.resources.*?>
<?import javafx.scene.layout.*?>
<AnchorPane fx:id="home" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1000.0" prefWidth="1250.0" style="-fx-border-radius: 5; -fx-background-color: tranparant;" stylesheets="#mainView.css" AnchorPane.bottomAnchor="0" AnchorPane.leftAnchor="0" AnchorPane.rightAnchor="0" AnchorPane.topAnchor="0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml" fx:controller="com.syrianep.reader.ui.KidsTheme.resources.HomeController" >
<children>
<ItemCard layoutX="50.0" layoutY="50.0"AnchorPane.bottomAnchor="0" AnchorPane.leftAnchor="0"/>
</children>
</AnchorPane>
but the problem that when i open home.fxml using scene builder the itemCard is not viewed.
error is;
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617)

Java FX fxml on Action

I want to add a method to a button which is defined in my Controller class
in the console is only an error which tells me that it couldn't find the method
here is the code
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Controller">
<children>
<Button layoutX="126" layoutY="90" text="lololol" onAction="#test" fx:id="button" />
</children>
</AnchorPane>
and the Controller.java
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import java.awt.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable
{
#Override
public void initialize(URL url, ResourceBundle resourceBundle)
{
}
#FXML
private void test(ActionEvent event)
{
System.out.println("lollolol");
}
}
Replace:
import java.awt.event.ActionEvent;
with:
import javafx.event.ActionEvent;

Categories

Resources