Communication between Controller and Main Class in JavaFX - java

I have just started with JavaFX. I'm in the process of developing a GUI for my program, and I've used SceneBuilder to design my GUI. I currently have a main class that uses an FXMLLoader to load an .fxml file and displays it. This FXML file has another class called "Controller" as controller. As I said, I haven't had that much to do with JavaFX, so it's possible that I'm just doing it wrong.
Now I have 2 problems: First, I want to communicate between the two classes (code follows). Second, I would like to know how to integrate the code from the main class of the GUI into my already existing main class, I already tried it, but I always got only error messages (stacktrace and code follows).
isi_ko
My GUI Main Class
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 {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent normalParent = FXMLLoader.load(getClass().getResource("normalLayout.fxml"));
primaryStage.setTitle("Test");
primaryStage.setScene(new Scene(normalParent, primaryStage.getWidth(), primaryStage.getHeight()));
primaryStage.show();
}
}
My GUI Controller Class
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class Controller implements EventHandler<ActionEvent> {
#FXML Label activePercent;
#FXML Label activeLabel;
#FXML Label activeNumber;
#FXML Label lastLable;
#FXML Label lastNumber;
#FXML Label nextLabel;
#FXML Label nextNumber;
#FXML Label faderValue1;
#FXML Label faderValue2;
#FXML Label faderValue3;
#FXML Label faderValue4;
#FXML Label faderValue5;
#FXML
public void test() {
}
#Override
public void handle(ActionEvent event) {
getFaderValueLable(1).setText("1");
getFaderValueLable(2).setText("2");
getFaderValueLable(3).setText("3");
getFaderValueLable(4).setText("4");
getFaderValueLable(5).setText("5");
}
public Label getFaderValueLable(int i){
switch (i){
case 1:
return faderValue1;
case 2:
return faderValue2;
case 3:
return faderValue3;
case 4:
return faderValue4;
case 5:
return faderValue5;
default:
return null;
}
}
}
My normal Main Class (without GUI Code)
import com.fazecast.jSerialComm.SerialPort;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class Main{
Fader[] faders;
final int baudRate = 1000000; //The Speed that is Used to Communicate with the Arduino
private int physicalFaderAmount; //Amount of Physical Faders, counted from 1
private int faderPages; //Amount of Faderpages, counted from 1
private int activeFaderpage; //The Currently active "physical" Faderpage
private ArduinoController arduinoController; //The Class that communicates with the Arduino
private EosOscController oscController; //The Class that Communicates with Eos
Main(String[] args) {
//Todo Redo this part when UI done
//at the Moment here are just some default values
activeFaderpage = 0;
physicalFaderAmount = 5;
faderPages = 4;
faders = new Fader[physicalFaderAmount];
for (int i = 0; i < physicalFaderAmount; i++){
faders[i] = new Fader(faderPages, i);
}
oscController = new EosOscController(8001, 8000, "192.168.178.133", this);
arduinoController = new ArduinoController(SerialPort.getCommPort("COM4"), this);
}
public static void main(String[] args) {
Main main = new Main(args);
}
// This Class is for Storing Information about a particular physical Fader
public class Fader {
public int[][] values;
// The last 3 Falues the Fader has on the Different Faderpages.
// The First Koordinate is for the Page, the second coordinate is for which of the last 3 Values you want to use.
// The First Value of Every page is the Current value of the Fader
// The Others are for detecting inconsistent Potivalues
public int faderID; //The number of the Physical Fader, starting from 0.
// Uses local values, not OSC
// Sets the Value of the Fader
// Can send the new Value directly to the Arduino or Eos
public void setValue(int physicalPage, int value, boolean sendToEos, boolean sendToArduino) {
if (value != this.values[physicalPage][2] || (Math.max(values[physicalPage][1], value) - Math.min(values[physicalPage][1], value)) > 1) {
values[physicalPage][0] = value;
//Todo: Test this
if (sendToEos) {
oscController.setFaderIntensity(physicalPage, faderID, value / 100.0D, true);
}
//Todo Test this
if (sendToArduino) {
arduinoController.sendMessage(new ArduinoController.MessageToArduino(faderID, value), true);
}
}
values[physicalPage][2] = values[physicalPage][1];
values[physicalPage][1] = value;
}
Fader(int pages, int faderID) {
values = new int[pages][3];
this.faderID = faderID;
}
}
}
My normal Main Class (with GUI Code)
import com.fazecast.jSerialComm.SerialPort;
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{
Fader[] faders;
final int baudRate = 1000000; //The Speed that is Used to Communicate with the Arduino
private int physicalFaderAmount; //Amount of Physical Faders, counted from 1
private int faderPages; //Amount of Faderpages, counted from 1
private int activeFaderpage; //The Currently active "physical" Faderpage
private ArduinoController arduinoController; //The Class that communicates with the Arduino
private EosOscController oscController; //The Class that Communicates with Eos
Main() {
//Todo Redo this part when UI done
//at the Moment here are just some default values
activeFaderpage = 0;
physicalFaderAmount = 5;
faderPages = 4;
faders = new Fader[physicalFaderAmount];
for (int i = 0; i < physicalFaderAmount; i++){
faders[i] = new Fader(faderPages, i);
}
oscController = new EosOscController(8001, 8000, "192.168.178.133", this);
arduinoController = new ArduinoController(SerialPort.getCommPort("COM4"), this); // For PC
// arduinoController = new ArduinoController(SerialPort.getCommPort("ttyACM0"), this); //For Raspberry Pi
}
public static void main(String[] args) {
launch();
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent normalParent = FXMLLoader.load(getClass().getResource("normalLayout.fxml"));
primaryStage.setTitle("Test");
primaryStage.setScene(new Scene(normalParent, primaryStage.getWidth(), primaryStage.getHeight()));
primaryStage.show();
}
// This Class is for Storing Information about a particular physical Fader
public class Fader {
public int[][] values;
// The last 3 Falues the Fader has on the Different Faderpages.
// The First Koordinate is for the Page, the second coordinate is for which of the last 3 Values you want to use.
// The First Value of Every page is the Current value of the Fader
// The Others are for detecting inconsistant Potivalues
public int faderID; //The number of the Physical Fader, starting from 0.
// Uses local values, not OSC
// Sets the Value of the Fader
// Can send the new Value directly to the Arduino or Eos
public void setValue(int physicalPage, int value, boolean sendToEos, boolean sendToArduino) {
if (value != this.values[physicalPage][2] || (Math.max(values[physicalPage][1], value) - Math.min(values[physicalPage][1], value)) > 1) {
values[physicalPage][0] = value;
//Todo: Test this
if (sendToEos) {
oscController.setFaderIntensity(physicalPage, faderID, value / 100.0D, true);
}
//Todo Test this
if (sendToArduino) {
arduinoController.sendMessage(new ArduinoController.MessageToArduino(faderID, value), true);
}
}
values[physicalPage][2] = values[physicalPage][1];
values[physicalPage][1] = value;
}
Fader(int pages, int faderID) {
values = new int[pages][3];
this.faderID = faderID;
}
}
}
Stacktrace for Main Class with GUI Code
Exception in Application constructor
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class Main
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodException: Main.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getConstructor(Class.java:1825)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$7(LauncherImpl.java:818)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
... 1 more
Exception running application Main
Process finished with exit code 1

Related

JavaFX: Make Chips Editable in JFXChipView

I want to ask if it is possible to make a chip in JFXChipView editable once it has been set.
You can create your own JFXChip and implement a behavior to enable editing. First, you need to have an editable label. I looked up online and I found this post: JavaFX custom control - editable label. Then, you can extend JFXChip to use that EditableLabel:
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXChip;
import com.jfoenix.controls.JFXChipView;
import com.jfoenix.svg.SVGGlyph;
import javafx.beans.binding.Bindings;
import javafx.beans.property.Property;
import javafx.scene.layout.HBox;
public class EditableChip<T> extends JFXChip<Property<T>> {
protected final HBox root;
public EditableChip(JFXChipView<Property<T>> view, Property<T> item) {
super(view, item);
JFXButton closeButton = new JFXButton(null, new SVGGlyph());
closeButton.getStyleClass().add("close-button");
closeButton.setOnAction(event -> {
view.getChips().remove(item);
event.consume();
});
// Create the label with an initial value from the item
String initialValue = view.getConverter().toString(item);
EditableLabel label = new EditableLabel(initialValue);
label.setMaxWidth(100);
// Bind the item to the text in the label
item.bind(Bindings.createObjectBinding(() -> view.getConverter().fromString(label.getText()).getValue(), label.textProperty()));
root = new HBox(label, closeButton);
getChildren().setAll(root);
}
}
Note: I am using Property<T> instead of using the desired class T because JFXChipView stores the item the first time you add it. And in that case, you're going to get the values as you entered them the first time when calling JFXChipView#getChips().
Sample application:
import com.jfoenix.controls.JFXChipView;
import javafx.application.Application;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;
public class EditableChipViewApp extends Application {
#Override
public void start(Stage primaryStage) {
JFXChipView<Property<String>> chipView = new JFXChipView<>();
chipView.setChipFactory(EditableChip::new);
chipView.setConverter(new StringConverter<Property<String>>() {
#Override
public String toString(Property<String> object) {
return object == null ? null : object.getValue();
}
#Override
public Property<String> fromString(String string) {
return new SimpleStringProperty(string);
}
});
VBox container = new VBox(chipView);
Scene scene = new Scene(container, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Result:
This is how you get the actual values of the chips:
List<String> chipsValues = chipView.getChips().stream().map(Property::getValue).collect(Collectors.toList());

How to open popup window from an other class without button click in javaFX?

So I've got a method called 'popup' in a javaFX controller class which opens a small popup window on top of the actual application window. This method runs without problem if it's assigned to a button in fxml and the button is clicked, but this is not the way I want to use it.
I've got an other class called 'Timer' with a new task (new thread) which is counting down from a certain number, and at a point it will open a popup window with a message. My purpose is to call and run the 'popup' method from this 'Timer' class. When I call the 'popup' method from here, it starts executing, but the popup window doesn't appear at all. (The method call happens as I get the message "in popup" on console from 'popup' method. )
So why does it work when a button click calls 'popup' method from the fxml file and why not when I call it from an other class? Thanks.
Please see the controller class with 'popup' method and the Timer class below (using Gradle in project):
"SceneController" controller class:
package GradleFX;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
//import java.awt.event.ActionEvent;
public class SceneController implements Initializable {
public static String password = "";
protected static int timercount = 20;
#FXML
private Label PWLabel;
#FXML
private Label bottomLabel;
#FXML
private PasswordField PWField;
#FXML
private Label showPWLabel;
protected static Label myBottomLabel;
private static PasswordField myPWField;
private static Label myShowPWLabel;
private static int tries;
#Override
public void initialize(URL location, ResourceBundle resources) {
Timer timerTask = new Timer();
myBottomLabel = bottomLabel;
myPWField = PWField;
myShowPWLabel = showPWLabel;
new Thread(timerTask).start();
}
**/***********************************************************************
/*This method runs if button is pressed in main application,
but can't make it work by calling it from Timer Class */
public void popup() {
System.out.println("in popup");
Stage dialogStage = new Stage();
dialogStage.initModality(Modality.WINDOW_MODAL);
VBox vbox = new VBox(new Text("Hi"), new Button("Ok."));
vbox.setAlignment(Pos.CENTER);
vbox.setPadding(new Insets(15));
dialogStage.setScene(new Scene(vbox));
dialogStage.show();
}
//****************************************************************************
public void showPW() {
myShowPWLabel.setText(myPWField.getText());
}
public void hidePW() {
myShowPWLabel.setText("");
}
public void exit() {
System.exit(0);
}
public void write() {
PWLabel.setText("Mukodik");
}
public void writeInput(String in) {
password = in;
System.out.println("final password text text: " + password);
writeFinally();
}
public void writeFinally() {
System.out.println("This is 'password' : " + password);
//bottomLabel.setText(password);
}
public void bottomLabelWrite() {
bottomLabel.setText(myPWField.getText());
}
public static void setLabel() throws InterruptedException {
myBottomLabel.setText("");
myBottomLabel.setText("Database has been permanently erased.");
//Thread.sleep(3000);
//System.exit(0);
}
public static void noKeyEnteredNote() {
myBottomLabel.setTextFill(Color.BLACK);
myBottomLabel.setText("No key entered. Type Main Key.");
}
public static void rightKey() {
myBottomLabel.setText("Yes, this is the right key.");
}
public static void wrongKey() throws InterruptedException {
tries = MasterKey.numOfTryLeft;
if (tries > 0) {
myBottomLabel.setTextFill(Color.RED);
myBottomLabel.setText("!!!Wrong key!!! You've got " + tries + " tries left!");
}
}
public void simpleTest(String in) {
System.out.println("in simpleTest and in is: " + in);
}
public void getMainKey() throws IOException, InterruptedException {
MasterKey masterKey = new MasterKey();
System.out.println("Inside SceneController");
masterKey.requestKey(myPWField.getText());
}
public void changeScreen(ActionEvent event) throws IOException, InterruptedException {
getMainKey();
if (MasterKey.isRightKey) {
Parent tableViewParent = FXMLLoader.load(getClass().getResource("Menu.fxml"));
Scene tableViewScene = new Scene(tableViewParent);
Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
window.setScene(tableViewScene);
window.show();
}
}
}
This is Timer class:
package GradleFX;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
public class Timer extends Task {
private ActionEvent actionEvent;
#Override
protected Integer call() throws Exception {
boolean notCalled = true;
while (SceneController.timercount > 0) {
SceneController sceneController = new SceneController();
System.out.println(SceneController.timercount);
Thread.sleep(1000);
SceneController.timercount--;
if (SceneController.timercount < 19) {
System.out.println("Less than 5");
if(notCalled) {
sceneController.popup();
notCalled = false;
}
}
}
System.exit(0);
return null;
}
}
Add this to your code:
#Override
public void initialize(URL location, ResourceBundle resources) {
Timer timerTask = new Timer();
myBottomLabel = bottomLabel;
myPWField = PWField;
myShowPWLabel = showPWLabel;
new Thread(timerTask).start();
timerTask.setOnFinished(e->{
popup();
});
}

How to increase the number of possible selections within a ToggleGroup (e.g. RadioButtons)?

I am somewhat new to programming and new to OOP (2nd Java project over all right now) and would love any hints or help.
I am currently working on a character creation program for my very own pen&paper game. I am using JavaFX (without FXML and thus without SceneBuilder) for the GUI part. I am working with Eclipse Neon on JDK1.8.0_131.
Here is my issue:
tl;dr: How to increase the number of possible selections within a ToggleGroup?
I am about to create a list of options for the user to choose from. The list consist of about 30 different advantages he or she can choose to improve their character. The allowed maximum of chosen options depends on the character and varies around 5. I already implemented an array of pairs (I know about HashMaps), where each entry is a pair consisting of the advantage's name and an integer representing its costs (they vary in their values, so in their costs).
The list itself should now be implemented via
ScrollPane scrollAdv = new ScrollPane();
VBox vBoxAdv = new VBox();
scrollAdv.setContent(vBoxAdv);
Pair<String, Integer>[] listAdv = info.getAdvantages();
for (int i = 0; i < listAdv.length; i++) {
String name = listAdv[i].getKey(); // delivers the 1st entry of a pair
int costs = listAdv[i].getValue(); // delivers the 2nd entry of a pair
ToggleButton toggleButton = new ToggleButton();
toggleButton.setUserData(name);
toggleButton.setText(name + " (" + costs + ")");
vBoxAdv.getChildren().add(toggleButton);
}
Note that I don't care too much about ToggleButtons and they could easily be replaced with RadioButtons. Both work the same way, if I understood the documentation correctly. They both use ToggleGroup to make sure, only one option is selected.
So while you probably guessed so by now, what I want to do is give the user the possibility to chose more than one option at once. I do not want to make it so the user has to chose one option after the other, while resetting the list in between.
Thanks for reading and thanks in advance for any help or hints.
edit: I could always just add a counter which refreshes whenever one option is selected or deselected and blocks any selection if it's < 1, but I thought that there should be a better solution, e.g. increase the built-in limit of 1 which ToggleGroup seems to be using.
One way would be to disable the remaining toggles when the limit is reached. Here's a ToggleSet class that does that:
import java.util.ArrayList;
import java.util.List;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.IntegerBinding;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Node ;
import javafx.scene.control.Toggle;
public class ToggleSet<T extends Node & Toggle> {
private final ObservableList<T> toggles = FXCollections.observableArrayList(t -> new Observable[] {t.selectedProperty()});
private final FilteredList<T> selectedToggles = toggles.filtered(t -> ((Toggle)t).isSelected());
private final IntegerProperty maximumSelectable = new SimpleIntegerProperty(0);
private final IntegerBinding numSelected = Bindings.size(selectedToggles);
public ToggleSet(int maximumSelectable) {
this.maximumSelectable.addListener((obs, oldMax, newMax) -> {
if (newMax.intValue() < numSelected.get()) {
List<Toggle> togglesToClear = new ArrayList<>(selectedToggles.subList(0, numSelected.get() - newMax.intValue()));
togglesToClear.forEach(t -> t.setSelected(false));
}
});
setMaximumSelectable(maximumSelectable);
}
public ToggleSet() {
this(0);
}
public ObservableList<T> getSelectedToggles() {
return FXCollections.unmodifiableObservableList(selectedToggles) ;
}
public IntegerProperty maximumSelectableProperty() {
return maximumSelectable ;
}
public final int getMaximumSelectable() {
return maximumSelectableProperty().get();
}
public final void setMaximumSelectable(int maximumSelectable) {
maximumSelectableProperty().set(maximumSelectable);
}
public void addToggle(T toggle) {
if (numSelected.get() >= getMaximumSelectable()) {
toggle.setSelected(false);
}
toggles.add(toggle);
toggle.disableProperty().bind(toggle.selectedProperty().not().and(numSelected.greaterThanOrEqualTo(maximumSelectable)));
}
public void removeToggle(T toggle) {
toggles.remove(toggle);
toggle.disableProperty().unbind();
}
}
Here's an example testing it:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Spinner;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class ToggleSetTest extends Application {
#Override
public void start(Stage primaryStage) {
ToggleSet<ToggleButton> toggleSet = new ToggleSet<>(5);
GridPane grid = new GridPane() ;
Spinner<Integer> maxSelectedSpinner = new Spinner<>(0, 20, 5);
maxSelectedSpinner.getValueFactory().valueProperty().bindBidirectional(toggleSet.maximumSelectableProperty().asObject());
grid.add(new HBox(2, new Label("Maximum selected"), maxSelectedSpinner), 0, 0, 2, 1);
grid.addRow(1, new Label("Selection"), new Label("Include in set"));
for (int i = 1; i <= 20 ; i++) {
RadioButton button = new RadioButton("Button "+i);
CheckBox checkBox = new CheckBox();
checkBox.selectedProperty().addListener((obs, wasChecked, isNowChecked) -> {
if (isNowChecked) {
toggleSet.addToggle(button);
} else {
toggleSet.removeToggle(button);
}
});
checkBox.setSelected(true);
grid.addRow(i + 1, button, checkBox);
}
grid.setPadding(new Insets(10));
grid.setHgap(5);
grid.setVgap(2);
Scene scene = new Scene(grid);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If you want the same behavior as the ToggleGroup, where the previous selection becomes unselected, it's a little trickier, but the following should work:
import java.util.ArrayList;
import java.util.List;
import javafx.beans.Observable;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node ;
import javafx.scene.control.Toggle;
public class ToggleSet<T extends Node & Toggle> {
private final ObservableList<T> toggles = FXCollections.observableArrayList(t -> new Observable[] {t.selectedProperty()});
private final ObservableList<T> selectedToggles = FXCollections.observableArrayList();
private final IntegerProperty maximumSelectable = new SimpleIntegerProperty(0);
private final ChangeListener<Boolean> toggleListener = (obs, wasSelected, isNowSelected) -> {
#SuppressWarnings("unchecked")
T toggle = (T) ((Property<?>)obs).getBean();
if (isNowSelected) {
selectedToggles.add(toggle);
ensureWithinMax();
} else {
selectedToggles.remove(toggle);
}
};
public ToggleSet(int maximumSelectable) {
this.maximumSelectable.addListener((obs, oldMax, newMax) -> ensureWithinMax());
setMaximumSelectable(maximumSelectable);
}
private void ensureWithinMax() {
if (this.maximumSelectable.get() < selectedToggles.size()) {
List<Toggle> togglesToClear = new ArrayList<>(selectedToggles.subList(0, selectedToggles.size() - this.maximumSelectable.get()));
togglesToClear.forEach(t -> t.setSelected(false));
}
}
public ToggleSet() {
this(0);
}
public ObservableList<T> getSelectedToggles() {
return FXCollections.unmodifiableObservableList(selectedToggles) ;
}
public IntegerProperty maximumSelectableProperty() {
return maximumSelectable ;
}
public final int getMaximumSelectable() {
return maximumSelectableProperty().get();
}
public final void setMaximumSelectable(int maximumSelectable) {
maximumSelectableProperty().set(maximumSelectable);
}
public void addToggle(T toggle) {
if (toggle.isSelected()) {
selectedToggles.add(toggle);
ensureWithinMax();
}
toggle.selectedProperty().addListener(toggleListener);
toggles.add(toggle);
}
public void removeToggle(T toggle) {
toggle.selectedProperty().removeListener(toggleListener);
toggles.remove(toggle);
}
}
(Use the same test code.)

FXMLLoader.getController() returns null no matter what

I have been doing a small app for my studies in JavaFX. In the app, I need to get the controller separately from the Main file, so I've devised this:
public class DiceObserver implements Observer {
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
public Controller controller = loader.<Controller>getController();
Dice ov;
public DiceObserver(Dice dice){
this.ov = dice;
}
public void update(Observable o, Object arg) {
int roll = ov.getLastValue().getRolledValue();
System.out.println(roll);
controller.updateBarChart(roll);
}
}
The FXML file:
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller{
#FXML public TextField freqText;
#FXML public Slider freqSlider;
#FXML public Button startButton;
#FXML public Button endButton;
#FXML public BarChart rollChart;
#FXML public ImageView dicePic;
XYChart.Series s1 = new XYChart.Series<>();
XYChart.Series s2 = new XYChart.Series<>();
XYChart.Series s3 = new XYChart.Series<>();
XYChart.Series s4 = new XYChart.Series<>();
XYChart.Series s5 = new XYChart.Series<>();
XYChart.Series s6 = new XYChart.Series<>();
Image side1 = new Image("Dice_Side_1.png");
SimulationThread t = SimulationThread.getInstance();
public void startSimulation(ActionEvent event) { // start the simulation
t.start();
}
public void getNewFreqValue(ActionEvent event) {
freqText.setText(String.valueOf(freqSlider.getValue()));
}
public void stopSimulation(ActionEvent event) {
t.end();
}
public void updateBarChart(int diceRoll){
System.out.println(diceRoll);
int rollval = diceRoll;
switch (rollval) {
case 1:
s1.getData().add(rollval);
break;
case 2:
s2.getData().add(rollval);
break;
case 3:
s3.getData().add(rollval);
break;
case 4:
s4.getData().add(rollval);
break;
case 5:
s5.getData().add(rollval);
break;
case 6:
s6.getData().add(rollval);
break;
}
}
public void updateDicePic(int diceRoll){
dicePic.setImage(side1);
}
public void initialize() {
}
}
The updateBarChart method is in the controller. When I run the code, the loader has a value, but the getcontroller method returns null. How can I fix this?
It returns null because you have not specified a controller as-of yet. From the code you posted above, your controller and FXML are not aware of each other.
Call:
loader.setController(yourControllerInstance);
Example:
https://github.com/SnakeDoc/superD/blob/master/src/com/vanomaly/superd/Main.java#L39
Or you must specify the controller in your FXML document (in your root element):
<AnchorPane maxHeight="...".... fx:controller="sample.Controller">
<HBox .... >
....
Example:
https://github.com/SnakeDoc/Narvaro/blob/master/src/resources/Narvaro.fxml#L13
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
public Controller controller = loader.<Controller>getController();
You neither set the controller, nor can a controller class specified in the fxml file (fx:controller attribute) be initialized, since you do not call load() before using getController().
I guess you're trying to do the latter. However without actually loading the fxml file, no controller will be created. Just creating the FXMLLoader instance won't load anything. The input is processed when you call the load method. (Processing the input is of course the prerequisite for knowing and thus being able to create the controller instance.)

PaginationSample Javafx cannot assign a value to final variable pagination

I'm trying to reproduce a Pagination Sample from oracle samples, but when I imported the project something strange happened that I can not build and run the project:
The complete code is:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.util.Callback;
public class PaginationSample extends Application {
private final Pagination pagination;
private Image[] images = new Image[7];
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setScene(new Scene(root));
VBox outerBox = new VBox();
outerBox.setAlignment(Pos.CENTER);
//Images for our pages
for (int i = 0; i < 7; i++) {
images[i] = new Image(PaginationSample.class.getResource("animal" + (i + 1) + ".jpg").toExternalForm(), false);
}
pagination = PaginationBuilder.create().pageCount(7).pageFactory(new Callback<Integer, Node>() {
#Override public Node call(Integer pageIndex) {
return createAnimalPage(pageIndex);
}
}).build();
//Style can be numeric page indicators or bullet indicators
Button styleButton = ButtonBuilder.create().text("Toggle pagination style").onAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent me) {
if (!pagination.getStyleClass().contains(Pagination.STYLE_CLASS_BULLET)) {
pagination.getStyleClass().add(Pagination.STYLE_CLASS_BULLET);
} else {
pagination.getStyleClass().remove(Pagination.STYLE_CLASS_BULLET);
}
}
}).build();
outerBox.getChildren().addAll(pagination, styleButton);
root.getChildren().add(outerBox);
}
//Creates the page content
private VBox createAnimalPage(int pageIndex) {
VBox box = new VBox();
ImageView iv = new ImageView(images[pageIndex]);
box.setAlignment(Pos.CENTER);
Label desc = new Label("PAGE " + (pageIndex + 1));
box.getChildren().addAll(iv, desc);
return box;
}
#Override public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) { launch(args); }
}
But netbeans show me the error "cannot assign a value to final variable pagination" for the following line:
pagination = PaginationBuilder.create().pageCount(7).pageFactory(new Callback<Integer, Node>() {
Someone can explain me what is going wrong??
A final field can be initialized only once. So the best place to initialize it is when its declared
private final Pagination pagination = new Pagination(...);
or it can be done in the constructor, since the constructor is assured to be called once per instance
private final Pagination pagination;
public PaginationSample() {
pagination = new Pagination(...);
}
final field cannot be initialized in a method because a method can be called multiple times once an instance of that class gets created

Categories