Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created November 4, 2021 08:33
Show Gist options
  • Save Da9el00/5533ba7b6f3cd20d3d675c6f0b7da05b to your computer and use it in GitHub Desktop.
Save Da9el00/5533ba7b6f3cd20d3d675c6f0b7da05b to your computer and use it in GitHub Desktop.
JavaFX and Scene Builder - Option Picker
package sample;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private VBox options;
Random random = new Random();
@FXML
private Text fruit;
ArrayList<String> optionList = new ArrayList<>(Arrays.asList(
"Orange", "Apple", "Grapes", "Mango", "Pineapple", "Strawberry",
"Cherry", "Fig", "Kiwi", "Lemon"
));
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
initializeButtons();
}
@FXML
void getOption(ActionEvent event) {
String value = ((Button)event.getSource()).getText();
fruit.setText(value);
}
@FXML
void rerollOptions(ActionEvent event) {
initializeButtons();
}
private void initializeButtons(){
ObservableList<Node> buttons = options.getChildren();
ArrayList<String> fruits = getFruitOptions(buttons.size());
for (int i = 0; i < buttons.size(); i++) {
Button button = (Button) buttons.get(i);
button.setText(fruits.get(i));
}
}
private ArrayList<String> getFruitOptions(int optionAmount){
ArrayList<String> optionsCopy = (ArrayList<String>) optionList.clone();
ArrayList<String> pickedFruits = new ArrayList<>();
for (int i = 0; i < optionAmount; i++) {
int randomIndex = random.nextInt(optionsCopy.size());
pickedFruits.add(optionsCopy.remove(randomIndex));
}
return pickedFruits;
}
}
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Text layoutX="185.0" layoutY="53.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Option Picker">
<font>
<Font size="38.0" />
</font>
</Text>
<VBox fx:id="options" layoutX="250.0" layoutY="78.0" prefHeight="200.0" prefWidth="100.0" spacing="20.0">
<children>
<Button mnemonicParsing="false" onAction="#getOption" prefHeight="25.0" prefWidth="100.0" text="Button" />
<Button mnemonicParsing="false" onAction="#getOption" prefWidth="100.0" text="Button" />
<Button mnemonicParsing="false" onAction="#getOption" prefWidth="100.0" text="Button" />
<Button layoutX="10.0" layoutY="90.0" mnemonicParsing="false" onAction="#getOption" prefWidth="100.0" text="Button" />
</children>
</VBox>
<Button layoutX="14.0" layoutY="13.0" mnemonicParsing="false" onAction="#rerollOptions" text="Reroll" />
<Text layoutX="266.0" layoutY="324.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Picked Fruit">
<font>
<Font size="13.0" />
</font>
</Text>
<Text fx:id="fruit" layoutX="250.0" layoutY="363.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Fruit">
<font>
<Font size="25.0" />
</font>
</Text>
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment