logo
down
shadow

JavaFX change Pane color from a different class


JavaFX change Pane color from a different class

By : Prof. Marcellius
Date : November 17 2020, 11:58 AM
help you fix your problem I have a question. I want to change the color of a pane from another controller class. I am using this code: , This is my code: I hope helped you
FXMLDocumentController.class
code :
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;
    }

}
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);
    }
<?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>
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);
    }

}
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)));

                }
            });



        }


    }
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)));

            }
        });



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


Share : facebook icon twitter icon
How to change the color of pane in javafx?

How to change the color of pane in javafx?


By : Daniel Krajewski
Date : March 29 2020, 07:55 AM
I wish did fix the issue. If you really just want to know how to accomplish that particular thing, I'd suggest the following:
Set the Nodes' CSS like this, using the hexacolor that was entered by the user:
code :
String enteredByUser = "abcdef";
yournode.setStyle("-fx-background-color: #" + enteredByUser);
yournode.setBackground(new Background(new BackgroundFill(Color.web("#" + enteredByUser), CornerRadii.EMPTY, Insets.EMPTY)));
How to change a pane color from a different class in javaFX

How to change a pane color from a different class in javaFX


By : Ryan
Date : March 29 2020, 07:55 AM
wish helps you You do not need a separate class for your requirement. A simple change listener on the textProperty() of the textfield should be good enough.
code :
textField.textProperty().addListener((ob, oldValue, newValue) -> {
     if (isNumeric(newValue)) {
         scene.setFill(Color.AQUA);
     } else {
         scene.setFill(Color.FIREBRICK);
     }
});
private boolean isNumeric(String str) {
    for(Character ch : str.toCharArray()){
        if(Character.isAlphabetic(ch)){
            return false;
        }
    }
    return true;
}
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class TextFieldBindSceneColor extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {

        TextField textField = new TextField();
        VBox box = new VBox(textField);
        box.setAlignment(Pos.CENTER);
        box.setStyle("-fx-background-color: transparent");
        Scene scene = new Scene(box, 200, 200);

        textField.textProperty().addListener((ob, oldValue, newValue) -> {
            if (isNumeric(newValue)) {
                scene.setFill(Color.AQUA);
            } else {
                scene.setFill(Color.FIREBRICK);
            }
        });
        //scene.setFill(Color.RED);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private boolean isNumeric(String str) {
        for(Character ch : str.toCharArray()){
            if(Character.isAlphabetic(ch)){
                return false;
            }
        }
        return true;
    }
}
how to add same background color for all pane in javafx?

how to add same background color for all pane in javafx?


By : karpaga meenakshi
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You don't write a css for every view, you give every element the same style class.
code :
    Pane pane = new Pane();
    pane.getStyleClass().add("bg-black-style");
scene.getStylesheets().add("css-file.css");
.bg-black-style {
    -fx-background-color:  black;
}
How to get Background color of tile pane in Javafx?

How to get Background color of tile pane in Javafx?


By : Adrian
Date : March 29 2020, 07:55 AM
around this issue Problem description - In my application, I need to get back color of tile pane (or any other control), but i don't find any property/function. , Try this in the ActionEvent you can do this
code :
handle(ActionEvent event){//suppose we are in the handle method
   Object o = event.getSource();
   if(o instanceof Region){
       Background b = ((Region)o).getBackground();
       Paint p = b.getFills().get(0).getFill();//paint is actually your color :)
       if(p instanceof Color){
          ((Color)p) //now you have a color :)
Background color transition on a JavaFX 8 Node/Region/Pane

Background color transition on a JavaFX 8 Node/Region/Pane


By : Randerson Maurilio
Date : March 29 2020, 07:55 AM
will help you Best way would be to provide a custom animation, like this (elaborating on fabian's answer):
code :
@Override
public void start(Stage primaryStage) {

    Label label = new Label("Bla bla bla bla");

    Button btn = new Button("flash");
    VBox box = new VBox(10, label, btn);
    box.setPadding(new Insets(10));

    btn.setOnAction((ActionEvent event) -> {

        //**************************
        //this animation changes the background color
        //of the VBox from red with opacity=1 
        //to red with opacity=0
        //**************************
        final Animation animation = new Transition() {

            {
                setCycleDuration(Duration.millis(1000));
                setInterpolator(Interpolator.EASE_OUT);
            }

            @Override
            protected void interpolate(double frac) {
                Color vColor = new Color(1, 0, 0, 1 - frac);
                box.setBackground(new Background(new BackgroundFill(vColor, CornerRadii.EMPTY, Insets.EMPTY)));
            }
        };
        animation.play();

    });

    Scene scene = new Scene(box, 100, 100);

    primaryStage.setScene(scene);
    primaryStage.show();

}
Related Posts Related Posts :
  • Stream corrupted exception - invalid code type AC [java]
  • Routing Sysout values to JTextArea
  • GWT RPC serialization for Dynamic Host Page
  • Best Approch of Returning Value from 'getter' Method
  • Understanding xml schemalocation
  • Checking if a PDF is PDF/A 1-a format or not using PDFBOX in java
  • Difference Between Variable and Identifier
  • How to compute two aggregate functions with Java streams?
  • Parse the value of the json to baseadapter in android?
  • JavaFX animation on raspberry pi
  • Solve "unchecked warning" in Java avoiding @suppressWarnings
  • How to specify a new name for a file while moving it to another directory in Java?
  • Popup window not showing up with spinner item selection?
  • how to iterate through json objects in java
  • Caused by: java.lang.NoClassDefFoundError: org/springframework/aop/framework/AbstractAdvisingBeanPostProcessor
  • Getting NoClassDefFoundError while running PMD task from Gradle
  • Matlab drag and drop file from windows explorer to figure (gui)
  • How does scala's actor model make use of C threads and native system threads?
  • Store java.util.Date as key in a Map based on reference equality instead of "value" equality
  • Error when logging into FTP from AsyncTask class using org.apache.commons.net.ftp.FTPClient
  • creating unique index in mongoDB
  • hibernate Query to find record based on substring
  • How many strings are in jvm string pool intern
  • Unable to initialize native-platform. on Jenkins
  • How to create a list of dummy files in Java?
  • Docker: Which approach is better? WAR embedded in image or base image + war?
  • java.lang.InternalError: CallerSensitive annotation expected at frame 1
  • How to write a method to read JSON files of different structures and return different POJO objects in Java?
  • Reference to Classes in Java
  • Unable to call function via companion object
  • REST: convert XML content passed with POST to a java object, attribute inside the element
  • Device not receive message from GCM
  • How to increase Openfire concurrent connection limit?
  • Android send custom parameter on AsyncTask
  • GWT - Get dom element's value from another page
  • How can i initialize an array in Java?
  • Is it possible to increase a java process's(when is running) size?
  • Getting Node Address instead of Value in Binary Search Tree
  • PdfByteContent not adding in iText pdf
  • Spring JpaRepository: delete() with subsequent save() in the same transaction
  • producer-consumer using arraydeque working only in breakpoint
  • Adding a JPanel to another JPanel
  • Comparison with operator ==, how does it work?
  • Error buildling with maven for openshift
  • What is the java equivilant to MySQL's smallint
  • proxy trouble on eclipse with https
  • Spring-Jdbc Template and Prepared statement
  • Mule - exception in Outbound endpoint Message payload is of type: byte[]
  • Modify package names for java classes generated from wsdl
  • How to download multiple images and have one progressBar using AsynTask (Android)
  • Implementing Service Provider, IdP and user in java with SAML (POC)
  • Why does my scanner repeat?
  • Can I forget ResultSetFuture in Cassandra java driver without facing memory leak?
  • XML to JAXB binding having nested elements?
  • Java: ToString() prints the same hashcode each time
  • Why am I getting a thread exception in my graphics program?
  • Can we read 4th byte from hexadecimal value?
  • How to create watermark in image just like Google maps watermarks
  • how come Thread class called inside another class?
  • AssertionError: Unexpected schema version 0: Unexpected schema version 0
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org