Change the property of a rectangle by using another class JavaFX - java

I'm working on a project and I want a rectangle to appear when I press a button. However, I want to do this by directing the button click to a different class. Here is what I've tried:
Here is my first class, "Main"
static boolean btnClicked = false;
#Override
public void start(Stage primaryStage) {
Button btn = new Button("Make Popup Visible");
Rectangle menu = new Rectangle(40,40,200,200);
menu.setFill(Color.BLACK);
menu.setOpacity(0);
btn.addEventHandler(MouseEvent.MOUSE_CLICKED,(MouseEvent e) ->{
AddRect.showMenu();
});
if(btnClicked == true) {
menu.setOpacity(1);
}
Group root = new Group();
root.getChildren().addAll(btn, menu);
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
And my second class, "AddRect"
public class AddRect {
static void showMenu() {
Main.btnClicked = true;
}
}
However, this isn't working, and I don't know why. Can somebody help me? I don't even know if this is the best way to do it (Using two classes), but if there is a better way please let me know. Thanks in advance!

I figured it out! I just had to move the conditional inside the EventHandler
btn.addEventHandler(MouseEvent.MOUSE_CLICKED,(MouseEvent e) ->{
AddRect.showMenu();
if(btnClicked == true) {
menu.setOpacity(1);
}
});

Related

Cannot add CSS classes to my JavaFX Label

I am making a simple todolist app. Here is a watered-down version of it:
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
ListView<Label> todoListView = new ListView();
Label doneTodo = new Label ("This todo is meant to be finished and struck through");
doneTodo.getStyleClass().add("done"); // Add the "done" class to this to-do
Label undoneTodo = new Label("This todo is meant to be undone and therefore isn't struck through");
// Add both to-dos to the listview
addTodo(doneTodo, todoListView);
addTodo(undoneTodo, todoListView);
// Set the listview as the scene, and add the stylesheet
Scene scene = new Scene(todoListView, 600, 550);
scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
primaryStage.setTitle("Label not taking on Strikethrough");
primaryStage.setScene(scene);
primaryStage.show();
}
// Adds the to-do (in this case just a simple Label) to the listview
private static void addTodo(Label todo, ListView<Label> todoList) {
todoList.getItems().add(todo);
}
public static void main(String[] args) {
launch(args);
}
}
Here is my CSS class:
.done {
-fx-strikethrough: true;
}
When I run the code, the -fx-strikethrough property does not show up on my Label. Note that although this is a simplified version of my app, the issue remains the same: The text inside the JavaFX Label is not being struck through.
Again, sorry for any inadequacies in my question. I am fairly new to Stack Overflow!
Thanks in advance.
The CSS rule should apply to the text node under the label node:
.done .text {
-fx-strikethrough: true;
}

Refactor JFoenix JFXDialogLayout alert notification code to a simpler form so that it can be reused for other classes

how can I refactor the following code so that only the code in deleteButton.setOnAction(deleteEvent -> {//only this code varies} changes. Everything else will stay the same but the block of code in the lambda expression varies from time to time when I call the class from another class. The block of code that goes through the lambda expression is supposed to be a void method.
public class A {
public void test() {
// ensure that user can't close the alert
Stage primaryStage = (Stage) RootLayoutController.getRootLayout().getScene().getWindow();
JFXAlert<javafx.scene.control.ButtonType> alert = new JFXAlert<>(primaryStage);
alert.initModality(Modality.APPLICATION_MODAL);
alert.setOverlayClose(false);
//create font awesome icon
String ICON = "\uf071";
Label labelIcon = new Label(ICON);
labelIcon.setStyle("-fx-font-family: 'FontAwesome'; -fx-font-size: 60px; -fx-text-fill: #D34336;");
labelIcon.setPadding(new Insets(0,5,0,0));
// Create the content of the JFXAlert with JFXDialogLayout
JFXDialogLayout layout = new JFXDialogLayout();
Label labelHeading = new Label("Alert Notification");
Label labelBody = new Label("Are you sure you want to delete this?");
layout.setHeading(labelHeading);
layout.setBody(new VBox(new HBox(labelIcon, labelBody)));
// Buttons get added into the actions section of the layout.
JFXButton deleteButton = new JFXButton("Delete");
deleteButton.setDefaultButton(true);
deleteButton.setOnAction(deleteEvent -> {
//only this block of code changes
alert.hideWithAnimation();
});
JFXButton cancelButton = new JFXButton("Cancel");
cancelButton.setCancelButton(true);
cancelButton.setOnAction(closeEvent -> alert.hideWithAnimation());
layout.setActions(deleteButton, cancelButton);
alert.setContent(layout);
alert.showAndWait();
}
}
It is not entirely clear from your question what you are trying to accomplish, but I will take a wild stab at it.
If you are looking to be able to pass a code block to the deleteButton.setOnAction() method, you could use an Interface and pass implementations of that interface to the A class. Then just pass that reference to an internal method for the onAction lambda.
Here is a very quick example of how you could do something like this:
Main.java:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
// Action button
Button btnDoSomething = new Button("Do something...");
btnDoSomething.setOnAction(e -> doTheThings(new ImplDoSomething()));
Button btnDoSomethingElse = new Button("Do something else...");
btnDoSomethingElse.setOnAction(e -> doTheThings(new ImplDoSomethingElse()));
VBox mainPane = new VBox(5);
mainPane.setAlignment(Pos.CENTER);
mainPane.setPadding(new Insets(10));
mainPane.getChildren().addAll(btnDoSomething, btnDoSomethingElse);
primaryStage.setScene(new Scene(mainPane));
primaryStage.show();
}
private void doTheThings(IParameterMethod parameterMethod) {
parameterMethod.call();
}
}
The IParameterMethod.java Interface:
public interface IParameterMethod {
void call();
}
Then you can create as many classes as you like that implement that interface, each with their own call() method, allowing you to execute different code.
ImplDoSomething.java
public class ImplDoSomething implements IParameterMethod {
#Override
public void call() {
System.out.println("Doing something!");
}
}
ImplDoSomethingElse.java:
public class ImplDoSomethingElse implements IParameterMethod {
#Override
public void call() {
System.out.println("Doing something else!");
}
}
This should be easily adapted for your project.

JavaFX Textfield - Override default behaviour when up key is released

I am trying to set TextField's cursor/caret to be at the end when the up key is released:
private void setTextFieldBehaviour() {
_textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent key) {
switch (key.getCode()) {
case UP:
if (key.isControlDown()) {
... // Do something for Ctrl + UP key
break;
} else {
... // Do something for plain old UP key
_textField.end();
// _textField's cursor will go back
// to the front when the UP key is released!
break;
}
default:
break;
}
}
});
}
However, as stated in the above code's comment, because I am only overriding the method setOnKeyPressed, this means that the default behaviour when the keys are released will kick in for JavaFX's TextField,
Consequently, this makes it difficult to set the TextField cursor at the end when the UP key is released.
I thought of overriding setOnKeyReleased just for UP (as shown below), but this has the ugly effect of the cursor jumping to the front of the TextField, then jumping back at the end.
_textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent key) {
switch(key.getCode()) {
case UP:
if(!(key.isControlDown())) {
... // Do something for plain old UP key
_textField.end();
// _textField's cursor will go back
// to the front when the UP key is released
// then jump back to the end again!
break;
}
default:
break;
}
}
});
Or visually, this is how it appears:
Before pressing anything: // Cursor is placed at the end of the string
TextField[oldString |]
Press UP: // Cursor is in front of string
TextField[| oldString]
Release UP: // Cursor is back of string
TextField[newString |]
So currently, I am able to set it to go to the back, but this entails the cursor jumping around. Is there anyway to do it better?
look at those sample application that show 2 way to achieve this:
by setting the event Handler and consuming the event.
public class main extends Application {
#Override
public void start(Stage primaryStage) {
HBox root = new HBox();
TextField textField = new TextField();
textField.setOnKeyPressed(e -> {
if (e.getCode().equals(KeyCode.UP)) {
textField.end();
e.consume(); // this stop propagating the event
}
});
root.getChildren().add(textField);
Scene scene = new Scene(root, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
or by filtering the event and stop its propagation
public class main extends Application {
#Override
public void start(Stage primaryStage) {
HBox root = new HBox();
TextField textField = new TextField();
textField.addEventFilter(KeyEvent.KEY_PRESSED, e -> { //event filter first catch the event
if(e.getCode().equals(KeyCode.UP)){
textField.end();
e.consume(); // this stop propagating the event
}
});
root.getChildren().add(textField);
Scene scene = new Scene(root, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
You should look at http://docs.oracle.com/javase/8/javafx/events-tutorial/events.htm to learn how event are processed in javaFX.

How can I change the GUI of JavaFX outside start()?

I am crazy about the feature of JavaFX, in Swing, I could do,
#Override
public void onPluginRegistered(final GamePlugin plugin) {
JRadioButtonMenuItem gameMenuItem = new JRadioButtonMenuItem(plugin.getGameName());
gameMenuItem.setSelected(false);
gameMenuItem.addActionListener(event -> {
if (core.getPlayers().isEmpty()) {
// Can't start a game with no players.
showErrorDialog(frame, ERROR_NO_PLAYERS_TITLE, ERROR_NO_PLAYERS_MSG);
gameGroup.clearSelection();
} else {
core.startNewGame(plugin);
}
});
gameGroup.add(gameMenuItem);
newGameMenu.add(gameMenuItem);
}
if I want to add a radio item whenever a plugin has registered.
However in JavaFX, it seems, you can't declare any global item of JavaFX, because once the start() is called, it starts a new constructor and everything you've done before is nothing (there is no variable share to me).
Here is my Javafx code.
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 500, 500);
scene.getStylesheets().add("./Buttons.css");
Region spacer = new Region();
spacer.setMinWidth(10);
primaryStage.setScene(scene);
primaryStage.show();
TabPane tabPane = new TabPane();
Tab tabData = new Tab("Get your data");
tabPane.getTabs().add(tabData);
Tab tabDisplay = new Tab("Visualize your data");
tabPane.getTabs().add(tabDisplay);
pluginGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
#Override
public void changed(ObservableValue<? extends Toggle> ov,
Toggle old_toggle, Toggle new_toggle) {
if (pluginGroup.getSelectedToggle() != null) {
RadioButton chk = (RadioButton) new_toggle.getToggleGroup().getSelectedToggle();
chk.getText();
}
}
});
root.setCenter(tabPane);
FlowPane inputPanel = new FlowPane();
TextField source = new TextField ();
Button confirmButton = new Button("Get Your Resource!");
confirmButton.getStyleClass().add("GREEN");
inputPanel.getChildren().addAll(new Label("Input your source:"),
spacer, source, confirmButton);
root.setBottom(inputPanel);
RadioButton defaultBtn = new RadioButton("No data plugin are registered");
FlowPane pane = new FlowPane();
pane.getChildren().addAll(new Label("Select your data source"), spacer);
if (radioButtonBox != null) {
pane.getChildren().add(radioButtonBox);
}
tabData.setContent(pane);
}
#Override
public void onPluginRegistered(DataPlugin plugin) {
RadioButton button = new RadioButton(plugin.getName());
button.setToggleGroup(pluginGroup);
radioButtonBox.getChildren().add(button);
}
public void caller(String[] args) {
launch(args);
}
I want to initialize the javafx program from,
public static void main(String[] args) throws Exception {
DataFramework core = new ConcreteDataFramework();
GuiFramework gui = new GuiFramework(core);
core.addGuiListener(gui);
gui.caller(args);
core.registerPlugin(new CsvData());
}
It is weird that I can't add any radio button to the existing radioButtonBox every time I call onPluginRegistered(DataPlugin plugin) (The new radiobutton does not show up)
You should consider the start() method as the replacement for the main method. If your application needs access to some kind of service or model, create it in the start() (or init()) method. I would actually recommend making the Application subclass (which is inherently not reusable) as minimal as possible - it should just do the startup work - and factoring the remaining GUI code into a separate class. (If you use FXML, the FXML file can define the UI, and the Application subclass is then already pretty minimal: it just loads and displays the FXML.)
You haven't really provided enough context to make it clear what's going on here, but I'm guessing GuiFramework is the Application subclass you've shown part of, and DataFramework is an interface of some kind. I also assume GuiFramework is implementing some interface that defines the onPluginRegistered method.
So I would do:
public class GuiFramework implements PluginAware {
private final BorderPane root ;
private final DataFramework dataFramework ;
public GuiFramework(DataFramework dataFramework) {
this.dataframework = dataFramework ;
this.root = new BorderPane();
TabPane tabPane = new TabPane();
Tab tabData = new Tab("Get your data");
tabPane.getTabs().add(tabData);
// etc etc (remaining code from your start() method)
}
public Parent getView() {
return root ;
}
#Override
public void onPluginRegistered(DataPlugin plugin) {
RadioButton button = new RadioButton(plugin.getName());
button.setToggleGroup(pluginGroup);
radioButtonBox.getChildren().add(button);
}
}
and define a Main class for starting the application:
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
DataFramework core = new ConcreteDataFramework();
GuiFramework gui = new GuiFramework(core);
core.addGuiListener(gui);
Scene scene = new Scene(gui.getView(), 500, 500);
scene.getStylesheets().add("./Buttons.css");
primaryStage.setScene(scene);
primaryStage.show();
core.registerPlugin(new CsvData());
}
// for environments not supporting JavaFX launch automatically:
public static void main(String[] args) {
launch(args);
}
}

JavaFX: modify button text in real time

I have a button with a ContextMenu which contains a MenuItem rename.
rename.setOnMouseClicked is supposed to give the possibility to change the text in the button : The user can actually delete a letter in the text or write it then press enter to validate what he was typing, somehow working as textField.
Is it possible to do this ? if so, how can i ? Thanks!
Yes
Here's how:
public class EditableButtonApp extends Application {
#Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
root.setCenter(new EditableButton("Editable Button"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
class EditableButton extends Button {
TextField tf = new TextField();
public EditableButton(String text) {
setText(text);
setOnMouseClicked(e -> {
tf.setText(getText());
setText("");
setGraphic(tf);
});
tf.setOnAction(ae -> {
// if (validateText(tf.getText())) {// this is where you would validate the text
setText(tf.getText());
setGraphic(null);
// }
});
}
}
public static void main(String[] args) { launch(args); }
}

Categories

Resources