Search code examples
javafxjavafx-8fxmlmenuitemscenebuilder

JavaFX, SceneBuilder, MenuItem --> Image


I recently found the google-material-icons and now i want to make my application more good looking using icons. Right now i want to add an image to the application close MenuItem. With SceneBuilder you can just add items, but what i now wanna do is at least still use fxml. I have two questions:

  1. Can I edit the fxml without it being overwritten by scenebuilder again?
  2. how can i add the icon to MenuItems?

Thank you very much. In case you need it, here my FXML:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>

<BorderPane fx:id="borderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="@../styles/Styles.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.freakyonline.ucone.FXMLController">
   <center>
      <TabPane prefHeight="167.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
        <tabs>
          <Tab closable="false" text="Players">
               <content>
                  <TableView fx:id="playerTable" editable="true" onContextMenuRequested="#handlePTContextMenuRequest" onInputMethodTextChanged="#handleTextChanged" prefHeight="200.0" prefWidth="200.0">
                    <columns>
                      <TableColumn fx:id="nickColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="75.0" text="Nickname" />
                      <TableColumn fx:id="groupColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="75.0" text="Group" />
                        <TableColumn fx:id="yearOfBirthColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="94.0" text="Year of Birth" />
                        <TableColumn fx:id="ageColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="72.0" text="Age" />
                        <TableColumn fx:id="genderColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="59.0" text="Gender" />
                        <TableColumn fx:id="lastQuitColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="75.0" text="Last Quit" />
                    </columns>
                     <padding>
                        <Insets bottom="3.0" left="5.0" right="5.0" top="3.0" />
                     </padding>
                  </TableView>
               </content>
            </Tab>
          <Tab fx:id="consoleOneTab" closable="false" onSelectionChanged="#handleConsoleOneTabSelected" text="ConsoleOne">
               <content>
                  <VBox prefHeight="200.0" prefWidth="100.0">
                     <children>
                        <TextArea fx:id="consoleOneTextArea" editable="false" wrapText="true" VBox.vgrow="ALWAYS" />
                        <TextField fx:id="consoleOneTextField" alignment="TOP_LEFT" onAction="#handleConsoleOneAction" promptText="type here ..." />
                     </children>
                  </VBox>
               </content></Tab>
        </tabs>
      </TabPane>
   </center>
   <top>
      <MenuBar fx:id="mainMenuBar" BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" onAction="#handleFileClose" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" onAction="#handleHelpAbout" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
</BorderPane>

Solution

  • Yes, you can add an Image in your MenuItem without affect on fxml.

    First, add FX:ID to your menu-item.

    <MenuItem fx:id="close_item" mnemonicParsing="false" onAction="#handleFileClose" text="Close" />
    

    Now use setGraphic() method to add an ImageView in your menu-item.

    @FXML MenuItem close_item;
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
    
        ImageView menuIcon = new ImageView(new Image("/path/image.png"));
        menuIcon.setFitHeight(20);
        menuIcon.setFitWidth(20);
        close_item.setGraphic(menuIcon);
        //...
        //...
    }