Q: Help me identify what's causing the error, and hopefully getting the toolbar to work.
I'm trying to take the FXML-version from this page:
Difference in my java-class from link1:
It's not a main-class.
It does not extend Application but HBox or anything if it would work.
Error message: "javafx.fxml.LoadException: javafx.fxml.JavaFXBuilder$ObjectBuilder does not have a default property."
Well, here the class is:
public class MainWindow implements Initializable {
private ResourceBundle rb;
final static boolean RIGHT_TO_LEFT = false;
//FXML ATTRIBUTES
#FXML
private OverviewTab owContent;
#FXML
private StatisticsTab statisticsContent;
#FXML
private SettingsTab settingsContent;
#FXML
private Polygon dock_bottom;
#FXML
private TabPane tabpane;
#FXML
private Tab owTab;
#FXML
private Tab statisticsTab;
#FXML
private Tab settingsTab;
#FXML
private Button helpButton;
#FXML
private Button updateButton;
#FXML private Button task;
/**
* Create the application.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
this.rb = rb;
// Adding Swing style of custom Listener
tabpane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {
#Override public void changed(ObservableValue<? extends Tab> observableValue, Tab oldTab, Tab newTab) {
if(newTab.equals(owTab)){
}
}
});
//statisticsContent.addListener(this);
owTab.setText(rb.getString("overviewTab_headline"));
statisticsTab.setText(rb.getString("statisticsTab_headline"));
settingsTab.setText(rb.getString("settingsTab_headline"));
}
}
The author of the tutorial you are referencing, mentions the error and provides solution to it in his next post/tutorial. See ToolBar in FXML No Longer Requires tag. I think you are facing the same problem.
Related
I have 3 fxml files:
Main.fxml
Menu.fxml
Manager.fxml
and controller for each.
I need to controll BorderPane center of Main.fxmlfrom both remaining, so I fx:include'd them, gave them fx:id and also set included element Manager.fxml visible = false (only menu button set Manager to center of BorderPane ) but now I'am lost and dont understand how to set included fxml visible when button in Menu.fxmlis pressed. Easy when its about simple element but dont work same with included fxml file.
At the beginning I had only menu inlcuded in Main.fxml and method setCenter in its controller that i used to switch center element using MenuController.java but then i couldn't find way to set main center using manager (so manager could be swithched with other one) becouse FXMLloader only loaded Main and Menu so couldnt set proper MainController instance in ManagerController.
My code before change:
public class MainAdminController {
#FXML
private AdminMenuController adminMenuController;
#FXML
private BorderPane adminBorderPane;
#FXML
public void initialize(){
adminMenuController.setMainController(this);
}
public void setView(String fxmlPatch){
adminBorderPane.setCenter(Fxml.loadFXML(fxmlPatch));
}
}
AdminMenuController
public class AdminMenuController {
public static final String MANAGE_ACCOUNTS = "/fxml/admin/ManageAccounts.fxml";
public static final String MANAGE_ADMINS = "/fxml/admin/ManageAdmins.fxml";
public static final String CHANGE_PASSWORD = "/fxml/ChangePassword.fxml";
#FXML
private MainAdminController mainAdminController;
public void setMainController(MainAdminController mainAdminController) {
this.mainAdminController = mainAdminController;
}
#FXML
private ToggleGroup adminButtons;
#FXML
public void viewManageAccounts() {
mainAdminController.setView(MANAGE_ACCOUNTS);
}
#FXML
public void viewManageAdmins() {
mainAdminController.setView(MANAGE_ADMINS);
}
#FXML
public void viewChangePassword() {
mainAdminController.setView(CHANGE_PASSWORD);
}
}
i'm trying to set on top of a borderpane an fxml hbox, the borderpane is inside another borderpane that contains all.
The containers are in this form:
Stage-> PincipalBorderPane -> Vbox(Center of MainMenuBorderPane)-> ContentBorderPane(in bottom of Vbox).
Also in the left of my PrincipalBorderPane, I have a sidebar menu that contains buttons, I want to set on the top of ContentBorderPane an fxml document that will be like an small menu to show buttons that would set on the bottom of ContentMenu different panes. The problem is that I don't know how to set an fxml to a borderpane that is inside another borderpane.
public class MenuContentController implements Initializable {
private boolean flag = true;
private boolean flagsc = true;
#FXML
private JFXButton Minbtn;
#FXML
private JFXButton Maxbtn;
#FXML
private JFXButton Closebtn;
#FXML
private JFXButton Sidebarbtn;
#FXML
private JFXTextField Searchtxt;
#FXML
private JFXButton Ingressbtn;
#FXML
private JFXButton Egressbtn;
#FXML
private JFXButton Statusbtn;
#FXML
private BorderPane ContentTools;
#FXML
private void open_sidebar(ActionEvent event) throws IOException {
BorderPane border_pane = (BorderPane) ((Node) event.getSource()).getScene().getRoot();
if (flag) {
//Revisar animación más adelante
Parent sidebar = FXMLLoader.load(getClass().getResource("/app/fxml/Sidebar.fxml"));
sidebar.translateXProperty();
//
border_pane.setLeft(sidebar);
//
Timeline timeline = new Timeline();
KeyValue kv = new KeyValue(sidebar.translateXProperty(),0,Interpolator.EASE_IN);
KeyFrame kf = new KeyFrame(Duration.seconds(10), kv);
timeline.getKeyFrames().add(kf);
timeline.play();
flag = false;
} else {
border_pane.setLeft(null);
flag = true;
}
}
**#FXML
public void open_admintools_priv(ActionEvent event) throws IOException{
ContentTools = new BorderPane();
BorderPane bp = (BorderPane) ContentTools;
if (flagsc) {
Parent admintools = FXMLLoader.load(getClass().getResource("/app/fxml/AdminTools.fxml"));
bp.setTop(admintools);
flagsc=false;
} else {
ContentTools.setTop(null);
flagsc = true;
}
System.out.println(flagsc);**
}
The method open_admintools_priv should to set visible on top of ContentTools(ContentBorderpane) the Admintools.fxml by pressing a button in another fxml(sidebar.fxml) that I set on the left of my PrincipalBorderPane with the method open_sidebar, that I call creating a new controller of MenuContentController in the SidebarController:
public class SidebarController implements Initializable {
private boolean flagsc = true;
#FXML
private JFXButton condominiobtn;
#FXML
private JFXButton adminbtn;
#FXML
private JFXButton finanzasbtn;
#FXML
private JFXButton reportesbtn;
#FXML
private JFXButton confbtn;
#FXML
private void open_admintools(ActionEvent event) throws IOException{
MenuContentController aux = new MenuContentController();
aux.open_admintools_priv(event);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Another problem is that by this way, at the moment I create a MenuContentController, the variable flag is always on true, this variable I use it to set the fxml in borderpane when flag is true, or quit it when flag is false. I don't know if creating an instance of a controller is the way to acces to the objects of that fxml, thanks for your time.
This question already has answers here:
Passing Parameters JavaFX FXML
(10 answers)
Closed 5 years ago.
I have a TextField and a button in FXMLController and a label in SecondFXMLController
Now I want to get Value of TextField in SecondFXMLController
Note: Two fxml files will be loaded in Same stage
FXMLController:
public class FXMLDocumentController implements Initializable {
#FXML
private Label label;
#FXML
private Button button;
#FXML
private TextField firstField;
#FXML
private AnchorPane first;
private int a;
#FXML
private void handleButtonAction(ActionEvent event) throws IOException {
AnchorPane pane = FXMLLoader.load(getClass().getResource("SecondFXML.fxml"));
first.getChildren().setAll(pane);
}
public String setTo() {
System.out.println("In setTo function");
System.out.println(firstField.getText());
return firstField.getText();
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
SecondFXMLController:
public class SecondFXMLController implements Initializable
{
#FXML
private Label secondField;
private String f;
private AnchorPane pane;
private FXMLDocumentController con;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
FXMLLoader fxml = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
try {
pane = fxml.load();
con = fxml.getController();
f = con.setTo();
} catch (IOException ex) {
Logger.getLogger(SecondFXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(f);
secondField.setText(f);
}
}
Now problem is that i'am not able to fetch the value of textField in setTo() function so its returning null.
NOTE: Both FXML files should be loaded in the Same Stage
Can Any one please give the Solution
Instead of returning a String from the method, I would suggest to return a StringProperty. Later you can bind this String Property to the textProperty of the Label.
public class FXMLDocumentController implements Initializable {
...
#FXML
private TextField firstField;
...
public StringProperty firstFieldTextProperty() {
return firstField.textProperty();
}
...
}
In SecondFXMLController, just bind the secondLabel's textProperty to the method.
public class SecondFXMLController implements Initializable {
#FXML
private Label secondField;
...
#Override
public void initialize(URL url, ResourceBundle rb) {
...
FXMLLoader fxml = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
pane = fxml.load();
con = fxml.getController();
secondField.textProperty.bind(con.firstFieldTextProperty());
...
}
}
i'm trying to add an ActionListener to a label whitch pop out whenever user types wrong password or login.
Here is my Login Controller
public class LoginController implements Initializable {
#FXML
private Label label;
#FXML
private TextField LoginField;
#FXML
private PasswordField PasswdField;
#FXML
private Button LogInButton;
#FXML
private Label IncorrectDataLabel;
//private String uri = "http://google.com";
#FXML
private void LogIn(ActionEvent event) throws IOException {
if(LoginField.getText().equals("MKARK")&&PasswdField.getText().equals("KACZOR1"))
{
Parent parent = FXMLLoader.load(getClass().getResource("/fxmlFiles/MainScreen.fxml"));
Scene MainScene = new Scene(parent);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setScene(MainScene);
stage.show();
}
else
{
IncorrectDataLabel.setVisible(true);
// <------------------- Here I want to bind hyperlink to a label with would open the google site, whenever user clicks it.
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
How am i able to fix that issue? I've tried many times (setOnAction, addMouseListener) but nothing worked :(.
If You dont mind i would also ask about the public void initialize function. What is it for? It pop out automatically when i created the class.
Thanks in advance
Labels do not fire action events. You could use a listener for mouse clicked events, e.g:
#FXML
private void gotoGoogle() {
// open url etc
}
and in the FXML file
<Label fx:id="IncorrectDataLabel" onMouseClicked="#gotoGoogle" ... />
However, it probably makes more sense to use a Hyperlink for this, which would give the user better visual feedback that it was something on which they were expected to click. Just replace the label with
<Hyperlink fx:id="IncorrectDataLabel" onAction="#gotoGoogle" text="..." ... />
and update the type in the controller accordingly:
#FXML
private Hyperlink IncorrectDataLabel ;
You need the appropriate import for javafx.control.Hyperlink in both the FXML file and in the controller.
Off-topic note: use proper Java naming conventions for your variable and method names.
This question already has an answer here:
javafx 8 compatibility issues - FXML static fields
(1 answer)
Closed 8 years ago.
I'm introducing in javafx and i found it very interesting. But I have got a problem that i can't solve.
In the simplest case of a Hello World I can't put a #FXML public static var like this:
public class FXMLDocumentController implements Initializable
{
#FXML
public static Label label;
#FXML
private void handleButtonAction(ActionEvent event)
{
System.out.println("You clicked me!");
label.setText("Hello World!");
}
}
If I change it to private it works.
The cause of that I want to made this vars publics is because I'm using diferents controllers for diferents views (in my real app) and i want to communicate between theirs.
PS: Sorry for my bad english
You should not use a static field here. Your controller belongs to one view and every time the view is created by the FXML Loader new instances of all nodes in the view will be created. SO by calling the FXML Loader 2 times you will receive 2 instances of the view. In addition a new instance of your controller class will be created whenever you load the view by using the FXML viewer. By using a static field you will override values of old controller instances and this will end in horrible bugs. Here is a short introduction to the static keyword: What does the 'static' keyword do in a class?
If you just remove "static" it will work.
Instead of using public fields you should add getter and setter methods to your controller class and mark the field as private: Why use getters and setters?
I want to do something similar like this:
I'm working with tabs and i have this two controllers:
FXML_Tab1Controller.java:
public class FXML_Tab1Controller implements Initializable {
FXML_Tab2Controller tab2controller;
#FXML public Label Label1;
#FXML public TextField TextField1;
#FXML public Button Button1;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
#FXML private void actionButton1(ActionEvent event)
{
Label1.setText(tab2controller.TextField2.getText());
}
}
FXML_Tab2Controller.java:
public class FXML_Tab2Controller implements Initializable {
FXML_Tab1Controller tab1controller;
#FXML public Label Label2;
#FXML public TextField TextField2;
#FXML public Button Button2;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
#FXML private void actionButton2(ActionEvent event){
Label2.setText(tab1controller.TextField1.getText());
}
}
something similar like that video:
https://www.youtube.com/watch?v=XLVx46ycxco