0

I have created a class which is named NavButton. I want to be able to use this into an .fxml file.

The Problem Is---> I created an .fxml file with SceneBuilder which contains some buttons, and then replacing the tag Button with NavButton, but it doesn't work.

I can find tutorials on how to add a costume item in .fxml with SceneBuilder but no how i can do this mannually.

The NavButton class:

 import javafx.beans.property.BooleanProperty;
 import javafx.beans.property.BooleanPropertyBase;
 import javafx.css.PseudoClass;
 import javafx.scene.control.Button;

 public class NavButton extends Button {

        /** Define a pseudo class */
        private PseudoClass MARKED_PSEUDO_CLASS = PseudoClass.getPseudoClass("marked");
        BooleanProperty marked = new BooleanPropertyBase(false) {
            public void invalidated() {
                pseudoClassStateChanged(MARKED_PSEUDO_CLASS, marked.get());
            }

            @Override
            public Object getBean() {
                return NavButton.this;
            }

            @Override
            public String getName() {
                return "marked";
            }

        };

        public void setMarked(boolean m) {
            marked.set(m);
        }
    }

The problematic .fxml file:

    <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>

<fx:root prefHeight="44.0" prefWidth="626.0" style="-fx-border-color: white;" type="BorderPane" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
  <center>
  <GridPane prefHeight="32.0" prefWidth="514.0" BorderPane.alignment="CENTER">
     <columnConstraints>
        <ColumnConstraints />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
     </columnConstraints>
     <rowConstraints>
        <RowConstraints />
        <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
     </rowConstraints>
     <children>
        <NavButton id="navigationBarButton" fx:id="djMode" mnemonicParsing="false" prefHeight="40.0" prefWidth="150.0" text="DJMode" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
        <NavButton id="navigationBarButton" fx:id="simpleMode" mnemonicParsing="false" prefHeight="40.0" prefWidth="150.0" text="SimpleMode" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
        <NavButton id="navigationBarButton" fx:id="librariesMode" mnemonicParsing="false" prefHeight="40.0" prefWidth="170.0" text="LibrariesMode" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.rowIndex="1" />
     </children>
   </GridPane>
  </center>
 <padding>
   <Insets bottom="10.0" left="5.0" right="5.0" top="5.0" />
 </padding>
<left>
   <Button fx:id="settings" mnemonicParsing="false" BorderPane.alignment="CENTER" />
 </left>
 <right>
   <HBox prefHeight="32.0" prefWidth="94.0" BorderPane.alignment="CENTER">
      <children>
         <Button fx:id="minimize" mnemonicParsing="false" styleClass="specialButton" />
         <Button fx:id="maximize_normalize" mnemonicParsing="false" styleClass="specialButton" />
         <Button fx:id="close" mnemonicParsing="false" styleClass="specialButton" />
      </children>
   </HBox>
 </right>
</fx:root>

The error is:

javafx.fxml.LoadException: NavButton is not a valid type.
 /C:/eclipse/Building%20The%20XRPlayer/builder/bin/fxmls/NavigationBar.fxml:25

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2774)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2704)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at windows.MainWindow$NavigationBar.<init>(MainWindow.java:533)
 ..........
GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • "It doesn't work" is not really going to get you much help. What actually happens? – James_D Jun 20 '16 at 22:36
  • @James_D I will post the error and fxml file...ok done. – GOXR3PLUS Jun 20 '16 at 22:37
  • Are you posting the FXML file that works, or the one that actually generates the error? – James_D Jun 20 '16 at 22:41
  • @James_D The fxml works.But where the buttons are i want to add my costume item which is NavButton and extends Button. – GOXR3PLUS Jun 20 '16 at 22:43
  • 1
    @GoXr3Plus Remember to always mention classes and basically everything which is part of your code not with the strong text function, but by using formatting. – SpaceCore186 Jun 20 '16 at 22:44
  • 1
    @JavaNoob thanks i will. – GOXR3PLUS Jun 20 '16 at 22:45
  • @JavaNoob do you know a way to add the code?(Control+K) works but it doesn't make it well and i have to modify if with spaces... – GOXR3PLUS Jun 20 '16 at 22:47
  • So, do you think it might be a good idea to post the code that doesn't work, instead of code that does work? Presumably it is the code that doesn't work that you want help with, not the code that does work. – James_D Jun 20 '16 at 22:47
  • @James_D In the code above when i change the Button tag in .fxml with NavButton tag i get the error.(I can find only tutorials on how to add costume items with SceneBuilder but no how to do this mannually) – GOXR3PLUS Jun 20 '16 at 22:50
  • 1
    @GoXr3Plus What I do for avoiding that problem is copying the code to the question first, then selecting it and then pressing the formatting function button in the tool bar. – SpaceCore186 Jun 20 '16 at 22:50
  • So post the actual version of the FXML... ah forget it. I am not going to beg you to provide the information needed to fix *your* problem. – James_D Jun 20 '16 at 22:52
  • @James_D How would he post a code that works when he is asking for a solution to the problem? If he had a code that works, he wouldn't be asking in the first place. – SpaceCore186 Jun 20 '16 at 22:56
  • Third comment: I asked "Are you posting the FXML that works, or the one that generates the error". Response: "The FXML works". Why on earth would you post the version that *does* work when you have edited it *afterwards* and *then* it doesn't work? – James_D Jun 20 '16 at 22:58
  • 1
    @James_D I am trying man.. :) .I have posted the .fxml that doesn't work.I changed the Button tag with NavButton tag.I have said in the question that the .fxml that i provided before is working but when you change Button tag with NavButton tag it doesn't work. – GOXR3PLUS Jun 20 '16 at 22:59
  • I kind of assumed you changed the imports too when you said you changed `Button` to `NavButton`. How do you expect anyone to know what you forgot to do if you only post the code that works? – James_D Jun 20 '16 at 23:04
  • @James_D He said that he changed `Button` to `NavButton`. If that's not in the code, it's because he forgot to post the modified code in the question. It's as simple as saying "What you said in your question doesn't match with the code you posted". – SpaceCore186 Jun 20 '16 at 23:12
  • 1
    @JavaNoob is done bro. – GOXR3PLUS Jun 20 '16 at 23:14

1 Answers1

2

It looks like you didn't update the imports when you changed the class for the Button.

First - since FXML really doesn't play well with the default package (which is strongly discouraged anyway), you need to put your NavButton class in a package:

package com.mycompany.mydepartment.myproject ;

// imports ...

public class NavButton extends Button {
   // code ...
}

and then add an import to the FXML file:

<?xml version="1.0" encoding="UTF-8"?>

<?import com.mycompany.mydepartment.myproject.NavButton?>
<!-- existing code... -->
James_D
  • 201,275
  • 16
  • 291
  • 322