Last active
July 24, 2024 03:13
-
-
Save Da9el00/fcc6742993d797fe5715c89abeb81b75 to your computer and use it in GitHub Desktop.
JavaFX Custom Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.layout.VBox?> | |
<?import javafx.scene.shape.Rectangle?> | |
<fx:root type="javafx.scene.layout.VBox" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/20.0.1"> | |
<Rectangle fx:id="flipper" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="200.0" strokeType="INSIDE" width="200.0" /> | |
</fx:root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.animation.FillTransition; | |
import javafx.fxml.FXML; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.paint.Color; | |
import javafx.scene.shape.Rectangle; | |
import javafx.util.Duration; | |
import java.io.IOException; | |
public class Flipper extends VBox { | |
@FXML | |
private Rectangle flipper; | |
private boolean state = false; | |
public Flipper() { | |
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource( | |
"flipper.fxml")); | |
fxmlLoader.setRoot(this); | |
fxmlLoader.setController(this); | |
try { | |
fxmlLoader.load(); | |
} catch (IOException exception) { | |
throw new RuntimeException(exception); | |
} | |
setupElements(); | |
} | |
private void setupElements() { | |
flipper.onMouseClickedProperty().set(event -> switchState()); | |
} | |
private void switchState(){ | |
FillTransition ft; | |
if (state){ | |
ft = new FillTransition(Duration.millis(1000), flipper, Color.RED, Color.BLUE); | |
state = false; | |
} else { | |
ft = new FillTransition(Duration.millis(1000), flipper, Color.BLUE, Color.RED); | |
state = true; | |
} | |
ft.setCycleCount(1); | |
ft.play(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
import java.io.IOException; | |
public class HelloApplication extends Application { | |
@Override | |
public void start(Stage stage) throws IOException { | |
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); | |
Scene scene = new Scene(fxmlLoader.load()); | |
stage.setTitle("Hello!"); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
public static void main(String[] args) { | |
launch(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.fxml.FXML; | |
import javafx.scene.control.Label; | |
public class HelloController { | |
@FXML | |
private Label welcomeText; | |
@FXML | |
private Flipper flipper; | |
@FXML | |
protected void onHelloButtonClick() { | |
welcomeText.setText("Welcome to JavaFX Application!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment