49465

JavaFX change Pane color from a different class

Question:

I have a question. I want to change the color of a pane from another controller class. I am using this code:

FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Menu.fxml")); try { Parent loaded = (Parent) loader.load(); } catch (IOException e) { e.printStackTrace(); } MenuController controller = (MenuController) loader.getController(); Platform.runLater(new Runnable() { @Override public void run() { Pane pane = controller.getRedPane(); pane.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY))); } });

The loaded, controller and pane aren't null. But the pane's color doesn't change, can someone help me with this problem? Thank you very much.

[EDIT]

public class MenuController implements Initializable { @FXML private GridPane MenuRoot; @FXML private Pane redPane; @Override public void initialize(URL location, ResourceBundle resources) { } @FXML private void changeGridSize(ActionEvent event){ new ChangeSizes(); } public GridPane getMenuRoot(){ return this.MenuRoot; } public Pane getRedPane(){ return this.redPane; } }

[EDIT]

<?import java.lang.*?> <?import javafx.scene.layout.*?> <GridPane fx:id="MenuRoot" gridLinesVisible="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nl.voxworks.homeserver.client.MenuController"> <columnConstraints> <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="0.0" percentWidth="50.0" /> <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="0.0" percentWidth="50.0" /> </columnConstraints> <rowConstraints> <RowConstraints maxHeight="1.7976931348623157E308" minHeight="0.0" percentHeight="50.0" vgrow="ALWAYS" /> <RowConstraints maxHeight="1.7976931348623157E308" minHeight="0.0" percentHeight="50.0" vgrow="ALWAYS" /> </rowConstraints> <children> <Pane fx:id="redPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: red;" GridPane.rowIndex="1" /> <Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: black;" GridPane.columnIndex="1" GridPane.rowIndex="1" /> </children> </GridPane>

[EDIT] (different project all files included) package javafxapplication16;

import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.geometry.Insets; import javafx.scene.layout.Background; import javafx.scene.layout.BackgroundFill; import javafx.scene.layout.CornerRadii; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; public class ChangeSize { public ChangeSize(){ FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml")); try { loader.load(); } catch (IOException ex) { Logger.getLogger(ChangeSize.class.getName()).log(Level.SEVERE, null, ex); } FXMLDocumentController controller = (FXMLDocumentController) loader.getController(); Platform.runLater(new Runnable() { @Override public void run() { Pane pane = controller.getPane(); pane.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY))); } }); } }

FXMLDocumentController.

package javafxapplication16; 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; import javafx.scene.layout.Pane; public class FXMLDocumentController implements Initializable { @FXML private Pane redPane; @FXML private void changeGridSize(ActionEvent event){ new ChangeSize(); } @Override public void initialize(URL url, ResourceBundle rb) { } public Pane getPane(){ return this.redPane; } }

JavaFXApplication16

package javafxapplication16; import java.io.IOException; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class JavaFXApplication16 extends Application { @Override public void start(Stage stage) throws Exception { // Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); // FXMLLoader loader Parent loaded=null; FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml")); try { loaded = (Parent) loader.load(); } catch (IOException e) { e.printStackTrace(); } Scene scene = new Scene(loaded); stage.setScene(scene); stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }

FXML

<?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <GridPane fx:id="MenuRoot" gridLinesVisible="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication16.FXMLDocumentController"> <columnConstraints> <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="0.0" percentWidth="50.0" /> <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="0.0" percentWidth="50.0" /> </columnConstraints> <rowConstraints> <RowConstraints maxHeight="1.7976931348623157E308" minHeight="0.0" percentHeight="50.0" vgrow="ALWAYS" /> <RowConstraints maxHeight="1.7976931348623157E308" minHeight="0.0" percentHeight="50.0" vgrow="ALWAYS" /> </rowConstraints> <children> <Pane fx:id="redPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: red;" GridPane.rowIndex="1" /> <Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: black;" GridPane.columnIndex="1" GridPane.rowIndex="1" /> <Button mnemonicParsing="false" onAction="#changeGridSize" text="Button" /> </children> </GridPane>

Answer1:

This is my code: I hope helped you

FXMLDocumentController.class

public class FXMLDocumentController implements Initializable { @FXML private Pane redPane; @FXML private void changeGridSize(ActionEvent event){ // new ChangeSizes(); } @Override public void initialize(URL url, ResourceBundle rb) { } public Pane getPane(){ return this.redPane; } }

JavaFXMLApplication3.class

public class JavaFXMLApplication3 extends Application { @Override public void start(Stage stage) throws Exception { // Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); // FXMLLoader loader Parent loaded=null; FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml")); try { loaded = (Parent) loader.load(); } catch (IOException e) { e.printStackTrace(); } Scene scene = new Scene(loaded); stage.setScene(scene); stage.show(); FXMLDocumentController controller = (FXMLDocumentController) loader.getController(); Platform.runLater(new Runnable() { @Override public void run() { Pane pane = controller.getPane(); pane.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY))); } }); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); }

FXMLDocument.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.*?> <GridPane fx:id="MenuRoot" gridLinesVisible="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxmlapplication3.FXMLDocumentController"> <columnConstraints> <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="0.0" percentWidth="50.0" /> <ColumnConstraints hgrow="ALWAYS" maxWidth="1.7976931348623157E308" minWidth="0.0" percentWidth="50.0" /> </columnConstraints> <rowConstraints> <RowConstraints maxHeight="1.7976931348623157E308" minHeight="0.0" percentHeight="50.0" vgrow="ALWAYS" /> <RowConstraints maxHeight="1.7976931348623157E308" minHeight="0.0" percentHeight="50.0" vgrow="ALWAYS" /> </rowConstraints> <children> <Pane fx:id="redPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: red;" GridPane.rowIndex="1" /> <Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: black;" GridPane.columnIndex="1" GridPane.rowIndex="1" /> </children> </GridPane>

I test your code and the problem is that you create another Pane redPane that isn't show: when you call FXMLLoader you create a new loader that isn't show in stage.

I suggested you a possible solution : create a static Stage ... example code:

public class JavaFXMLApplication3 extends Application { static Stage staticstage; // Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); // FXMLLoader loader @Override public void start(Stage stage) throws Exception { // Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); // FXMLLoader loader staticstage=stage; Parent loaded=null; FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml")); try { loaded = (Parent) loader.load(); } catch (IOException e) { e.printStackTrace(); } Scene scene = new Scene(loaded); stage.setScene(scene); stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }

and change class ChangeSize

public class ChangeSize { public ChangeSize() { Parent loaded=null; FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml")); try { loaded = (Parent) loader.load(); } catch (IOException e) { e.printStackTrace(); } Scene scene = new Scene(loaded); JavaFXMLApplication3.staticstage.setScene(scene); JavaFXMLApplication3.staticstage.show(); FXMLDocumentController controller = (FXMLDocumentController) loader.getController(); Platform.runLater(new Runnable() { @Override public void run() { Pane pane = controller.getPane(); pane.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY))); } }); } }

another solution is to pass FXMLDocumentController to class ChangeSize ... example code :

public class ChangeSize { public ChangeSize(FXMLDocumentController controller) { Platform.runLater(new Runnable() { @Override public void run() { Pane pane = controller.getPane(); pane.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, Insets.EMPTY))); } }); }

and in FXMLDocumentController class change the changeGridSize

@FXML private void changeGridSize(ActionEvent event){ new ChangeSize(this); }

Recommend

  • Missing OpenFilesEvent for JavaFX on Mac
  • Undefined element when tab is not active
  • Difficulties with updating my Mac to the latest Java JDK
  • Is there anything like an Objective-C block in Java?
  • Finding javafx jar file for windows
  • How do I recognize a line break with a switch case that evaluates a char in Java?
  • Exception in the iconization of JInternalFrame with DefaultDesktopManager
  • Android - VerifyError
  • jhipster run embedded jar with prod profile - issue with liquibase
  • Cassandra NoClassDefFoundError: com/google/common/util/concurrent/AsyncFunction
  • Invert string in Rust
  • What does certain JVM do after loading ByteCode into memory?
  • Glassfish - java.lang.NoClassDefFoundError
  • Action Pack components in Rails
  • Embedded Glassfish JPA Datasource connection fail
  • URLConnection doesn't work since API 10 and higher?
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Problem deserializing objects from cache on MyBatis 3/Java
  • Display issues when we change from one jquery mobile page to another in firefox
  • Why winpcap requires both .lib and .dll to run?
  • align graphs with different xlab
  • Python - Map / Reduce - How do I read JSON specific field in using DISCO count words example
  • Return words with double consecutive letters
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • How to format a variable of double type
  • Python: how to group similar lists together in a list of lists?
  • coudnt use logback because of log4j
  • JaxB to read class hierarchy
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize