JavaFx Transition Animation with multiple nodes - java

I am trying to implement a Transition effect on a node, below is SSCE,
public class GridPaneExperiments extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Experiment");
Button button2 = new Button("Expand");
Button button3 = new Button("Button 3");
Button button4 = new Button("Button 4");
GridPane gridPane = new GridPane();
ToolBar bar = new ToolBar();
bar.getItems().addAll(button3, button4);
gridPane.add(button2, 0, 0, 1, 1);
gridPane.add(bar, 1, 0, 1, 1);
//Handle Mouse on Button
button2.setOnMouseEntered((MouseEvent event) -> {
TranslateTransition openNav = new TranslateTransition(new Duration(350), bar);
openNav.setToX(0);
if (bar.getTranslateX() != 0) {
openNav.play();
}
});
button2.setOnMouseExited((MouseEvent event) -> {
TranslateTransition closeNav = new TranslateTransition(new Duration(350), bar);
closeNav.setToX(-(((GridPane) gridPane).getWidth()));
closeNav.play();
});
//Handle Mouse on ToolBar
bar.setOnMouseExited((MouseEvent event) -> {
TranslateTransition closeNav = new TranslateTransition(new Duration(350), bar);
closeNav.setToX(-(((GridPane) gridPane).getWidth()));
closeNav.play();
});
bar.setOnMouseEntered((MouseEvent event) -> {
TranslateTransition openNav = new TranslateTransition(new Duration(350), bar);
openNav.setToX(0);
if (bar.getTranslateX() != 0) {
openNav.play();
}
});
Scene scene = new Scene(gridPane, 240, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
What I am trying to achieve is:
When mouse entered the Button "Expand" a Node will be opened to its right, after which if mouse entered the opened node it should not get close .
When mouse entered the Button "Expand" a Node will be opened and mouse exited from Button "Expand" (but not entered the opened node) , then the opened node should be closed.
Currently I am playing the Transition animation in Mouse events of both Button and Node.
How can I achieve this?

Just use a single animation for closing and opening. This way you can reverse the animation easily, don't run the risk of starting multiple animations in parallel and starting a closing animation is not an issue, since you change the animation to a opening animation when entering one of the nodes:
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Experiment");
Button button2 = new Button("Expand");
Button button3 = new Button("Button 3");
Button button4 = new Button("Button 4");
GridPane gridPane = new GridPane();
ToolBar bar = new ToolBar();
bar.getItems().addAll(button3, button4);
bar.setTranslateX(-10000); // somewhere outside view
gridPane.add(button2, 0, 0, 1, 1);
gridPane.add(bar, 1, 0, 1, 1);
TranslateTransition transition = new TranslateTransition(Duration.millis(300), bar);
transition.setToX(0);
EventHandler<MouseEvent> enterHandler = (MouseEvent event) -> {
transition.setFromX(-gridPane.getWidth());
Duration time = transition.getCurrentTime();
transition.setRate(1);
transition.playFrom(time);
};
EventHandler<MouseEvent> exitHandler = (MouseEvent event) -> {
if (!(button2.isHover() || bar.isHover())) {
Duration time = transition.getCurrentTime();
transition.setRate(-1);
transition.playFrom(time);
}
};
//Handle Mouse on Button
button2.setOnMouseEntered(enterHandler);
bar.setOnMouseEntered(enterHandler);
button2.setOnMouseExited(exitHandler);
bar.setOnMouseExited(exitHandler);
Scene scene = new Scene(gridPane, 240, 100);
primaryStage.setScene(scene);
primaryStage.show();
}

you can simply achive your task using a helper HBox and setting setOnMouseExited to only that:
public class GridPaneExperiments extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Experiment");
Button button2 = new Button("Expand");
Button button3 = new Button("Button 3");
Button button4 = new Button("Button 4");
ToolBar bar = new ToolBar();
bar.getItems().addAll(button3, button4);
GridPane gridPane = new GridPane();
HBox hbox = new HBox(button2, bar);
hbox.setStyle("-fx-border-color: red");
gridPane.add(hbox, 0, 0);
//Handle Mouse on Button
button2.setOnMouseEntered((MouseEvent event) -> {
TranslateTransition openNav = new TranslateTransition(new Duration(350), bar);
openNav.setToX(0);
if (bar.getTranslateX() != 0) {
openNav.play();
}
});
// button2.setOnMouseExited((MouseEvent event) -> {
// TranslateTransition closeNav = new TranslateTransition(new Duration(350), bar);
// closeNav.setToX(-(((GridPane) gridPane).getWidth()));
// closeNav.play();
// });
//Handle Mouse on ToolBar
hbox.setOnMouseExited((MouseEvent event) -> {
TranslateTransition closeNav = new TranslateTransition(new Duration(350), bar);
closeNav.setToX(-(((GridPane) gridPane).getWidth()));
closeNav.play();
});
// bar.setOnMouseEntered((MouseEvent event) -> {
// TranslateTransition openNav = new TranslateTransition(new Duration(350), bar);
// openNav.setToX(0);
// if (bar.getTranslateX() != 0) {
// openNav.play();
// }
// });
Scene scene = new Scene(gridPane, 240, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}

Related

setText() in two textfeilds, depending on where user is clicked on

I have an assignment where I have to create a functional calculator using JavaFX.
We need to create 3 text fields, one for the first num, one for the second num, and one for the result.
I'm almost done. but how do I make the program paste PI on the textfield being edited by the user? this is my code:
please help
public class Calculator extends Application{
double ans;
int t=6;
#Override
public void start(Stage primaryStage){
GridPane pane = new GridPane();
pane.setHgap(10);
pane.setVgap(10);
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(10, 10, 10, 10));
HBox hbox = new HBox();
TextField t1 = new TextField();
TextField t2 = new TextField();
TextField t3 = new TextField();
if(t1.getText().length() > 5){
t1.setText(t1.getText().substring(0, t));
t3.setText("Error! maximum 5 digits");
}
hbox.setAlignment(Pos.TOP_CENTER);
Button b1 = new Button("AC"); b1.setAlignment(Pos.CENTER); pane.add(b1,0,3);
Button b2 = new Button("PI");b2.setAlignment(Pos.CENTER); pane.add(b2,1,3);
Button b3 = new Button("Sqrt");b3.setAlignment(Pos.CENTER);pane.add(b3,2,3);
Button b4 = new Button("DEL");b4.setAlignment(Pos.CENTER);pane.add(b4,3,3);
Button b5 = new Button("7");b4.setAlignment(Pos.CENTER);pane.add(b5,0,4);
Button b6 = new Button("8");b6.setAlignment(Pos.CENTER);pane.add(b6,1,4);
Button b7 = new Button("9");b7.setAlignment(Pos.CENTER);pane.add(b7,2,4);
Button b8 = new Button("/");b8.setAlignment(Pos.CENTER);pane.add(b8,3,4);
Button b9 = new Button("4");b9.setAlignment(Pos.CENTER);pane.add(b9,0,5);
Button b10 = new Button("5");b10.setAlignment(Pos.CENTER);pane.add(b10,1,5);
Button b11 = new Button("6");b11.setAlignment(Pos.CENTER);pane.add(b11,2,5);
Button b12 = new Button("*");b12.setAlignment(Pos.CENTER);pane.add(b12,3,5);
Button b13 = new Button("1");b13.setAlignment(Pos.CENTER);pane.add(b13,0,6);
Button b14 = new Button("2");b14.setAlignment(Pos.CENTER);pane.add(b14,1,6);
Button b15 = new Button("3");b15.setAlignment(Pos.CENTER);pane.add(b15,2,6);
Button b16= new Button("-");b16.setAlignment(Pos.CENTER);pane.add(b16,3,6);
Button b17 = new Button("=");b17.setAlignment(Pos.CENTER);pane.add(b17,0,7);
Button b18 = new Button("0");b18.setAlignment(Pos.CENTER); pane.add(b18,1,7);
Button b19 = new Button(".");b19.setAlignment(Pos.CENTER);pane.add(b19,2,7);
Button b20 = new Button("+");b20.setAlignment(Pos.CENTER);pane.add(b20,3,7);
b1.setOnAction((ActionEvent e) -> {
t1.setText("");
t2.setText("");
t3.setText("");
});
b4.setOnAction((ActionEvent e) -> {
t3.setText("");
});
b3.setOnAction((ActionEvent e) -> {
ans=(Math.sqrt(Double.parseDouble(t1.getText())));
t3.setText(ans + "");
});
b20.setOnAction((ActionEvent e) -> {
ans=Double.parseDouble(t1.getText()) +Double.parseDouble(t2.getText());
t3.setText(ans + "");
});
b16.setOnAction((ActionEvent event) -> {
ans=Double.parseDouble(t1.getText()) - Double.parseDouble(t2.getText());
t3.setText(ans + "");
});
b12.setOnAction((ActionEvent event) -> {
ans=Double.parseDouble(t1.getText()) * Double.parseDouble(t2.getText());
t3.setText(ans + "");
});
b8.setOnAction((ActionEvent event) -> {
ans=Double.parseDouble(t1.getText()) /Double.parseDouble(t2.getText());
t3.setText(ans + "");
});
b2.setOnAction((ActionEvent event) -> {
ans= (Math.PI);
if(t1.isFocused()){
t1.setText(String.valueOf(ans));}
else if(t2.isFocused()){
t2.setText(String.valueOf(ans));
}
});
/*
t1.setOnMouseClicked((event) -> {
if(b2.isPressed()){
ans= (Math.PI);
t1.setText(String.valueOf(ans));}
});
t2.setOnMouseClicked((event) -> {
if(b2.isPressed()){
ans= (Math.PI);
t2.setText(String.valueOf(ans));}
});
*/
hbox.getChildren().addAll(t1,t2,t3);
BorderPane bp = new BorderPane();
bp.setTop(hbox);
bp.setCenter(pane);
Scene scene = new Scene(bp);
primaryStage.setTitle("Nora's Functional Calculator");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch (args);
}
}
Add something to track which field last had the focus. A field:
private TextField lastFocused;
And some listeners:
t1.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean lost, Boolean gained) -> {
if (gained) {
lastFocused = t1;
}
});
t2.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean lost, Boolean gained) -> {
if (gained) {
lastFocused = t2;
}
});
Then you can just set the text in the last focused field:
if (lastFocused != null) {
lastFocused.setText(String.valueOf(ans));
}
You might also want to initialize lastFocused to t1, and automatically toggle between t1 and t2 whenever you set the value. Make the text fields member variables and use a method to inject to the correct field:
private void setTextOfLastFocusedField(String val) {
if (lastFocused != null) {
lastFocused.setText(val);
}
if (lastFocused == t1) {
lastFocused = t2;
} else {
lastFocused = t1;
}
}

JavaFX: Disable all components while a process is running and show progress indicator

I have a method that read values from the the database and returns a Map<Integer,String>. This method takes some time to return the map.
Till the time values are getting read I want a progress indicator(only loading ring like indicator will be enough,no need for progress bar) to be displayed on screen and all other components should be disabled till the time progress bar is shown.
public void scanDevice() {
ObservableList<TextField> list = FXCollections.observableArrayList(vehicleId, vehicleName, deviceType,
offboardBroker1, offboardBroker2, postfixQueue, pKIServer);
editedValuesMap.clear();
// devicePlugged = true;
if (cbChooseProject.getSelectionModel().getSelectedItem() != null) {
try {
devicePlugged = dsAdapter.getAdapter();
if (devicePlugged) {
if (bScanDevice.isFocused()) {
readMap = new HashMap<Integer, String>();
//Process Start
readMap = dsAdapter.initScan();
//Process End
if (!readMap.isEmpty() && readMap != null) {
isWritten = true;
isDeviceSideEnabled();
editDeviceContents.setDisable(false);
vehicleId.setText(readMap.get(0));
vehicleName.setText(readMap.get(1));
deviceType.setText(readMap.get(2));
offboardBroker1.setText(readMap.get(3));
offboardBroker2.setText(readMap.get(4));
postfixQueue.setText(readMap.get(5));
pKIServer.setText(readMap.get(6));
lContentsSerialNo.setText(readMap.get(7));
}
}
}
You could disabled all nodes with a method like the following but if you are also wanting to wait while something is happening an overlay using StackPanes may be the preferred choice.
public void setNodesDiabled(boolean disable, Node... nodes) {
for(Node node : nodes) {
node.setDisable(disable);
}
}
With an arbitrary node count, you can disable and re-enable as many nodes that are relevant to the process. It also helps to clean up as you won't have several node.setDisable(true); node2.setDisable(true); and so on.
Here in this example you won't need setNodesDisabled() because the StackPane overlay prevents clicking anything other than what's inside it. The background color is gray with 70% alpha so that you can tell it's an overlay.
public class ProgressExample extends Application {
public StackPane layout, main, progress;
public StackPane createProgressPane() {
ProgressIndicator indicator = new ProgressIndicator();
indicator.setMaxHeight(50);
indicator.setMaxWidth(50);
StackPane pane = new StackPane();
pane.setAlignment(Pos.CENTER);
pane.setStyle("-fx-background-color: rgba(160,160,160,0.7)");
pane.getChildren().add(indicator);
Task<Void> task = new Task<Void>(){
protected Void call() throws Exception {
// Your process here.
// Any changes to UI components must be inside Platform.runLater() or else it will hang.
Thread.sleep(2000);
Platform.runLater(() -> {
layout.getChildren().remove(pane);
});
return null;
}
};
new Thread(task).start();
return pane;
}
public StackPane createMainPane() {
Label label = new Label("Hello World!");
label.setFont(Font.font("Tahoma", FontWeight.SEMI_BOLD, 16));
Button start = new Button("Start Process");
start.setOnAction(action -> {
progress = createProgressPane();
layout.getChildren().add(progress);
});
VBox vbox = new VBox(10);
vbox.setAlignment(Pos.CENTER);
vbox.getChildren().addAll(label, start);
vbox.setPadding(new Insets(10,10,10,10));
StackPane pane = new StackPane();
pane.getChildren().add(vbox);
return pane;
}
public void start(Stage stage) throws Exception {
main = createMainPane();
layout = new StackPane();
layout.getChildren().add(main);
Scene scene = new Scene(layout, 900, 550);
stage.setScene(scene);
stage.setTitle("Progress Example");
stage.setOnCloseRequest(e -> {
Platform.exit();
System.exit(0);
});
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I believe the problem is that you are trying to change the values of TextFields inside the Task which is not the FX application thread which is why you are getting Not on FX application thread. To fix this you need to put your lines that modify nodes inside a Platform.runLater() like the following to your if statement.
if (readMap != null && !readMap.isEmpty()) { // Swap the order, can't check empty if it's null.
isWritten = true;
isDeviceSideEnabled();
Platform.runLater(() -> {
editDeviceContents.setDisable(false);
vehicleId.setText(readMap.get(0));
vehicleName.setText(readMap.get(1));
deviceType.setText(readMap.get(2));
offboardBroker1.setText(readMap.get(3));
offboardBroker2.setText(readMap.get(4));
postfixQueue.setText(readMap.get(5));
pKIServer.setText(readMap.get(6));
lContentsSerialNo.setText(readMap.get(7));
});
}
Here is an SSCCE:
It uses a Service that can be started more than once. It is not completebut something to start with.
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 400, 400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Service<Void> serv = new Service<Void>() {
#Override
protected Task<Void> createTask() {
return new Task<Void>() {
#Override
protected Void call() throws Exception {
int maxWork = 10;
for (int i = 0; i < maxWork; i++) {
Thread.sleep(1000);
updateProgress(i + 1, maxWork);
}
return null;
}
#Override
protected void succeeded() {
super.succeeded();
updateProgress(1, 1);
}
#Override
protected void cancelled() {
super.cancelled();
updateProgress(1, 1);
}
#Override
protected void failed() {
super.failed();
updateProgress(1, 1);
}
};
}
};
ProgressIndicator pi = new ProgressIndicator();
pi.progressProperty().bind(serv.progressProperty());
Button bStart = new Button("Start");
bStart.setOnAction(e -> {
serv.reset();
serv.start();
});
root.setCenter(bStart);
root.setBottom(pi);
primaryStage.setScene(scene);
primaryStage.show();
pi.getScene().getRoot().disableProperty().bind(serv.runningProperty());
}
public static void main(String[] args) {
launch(args);
}
}
In CSS I added:
.progress-indicator:disabled {
-fx-opacity: 1;
}

When trying to add items to a JavaFX TableView through an ObservableList, the TableView items won't show

I'm making a grocery list app, and it has the feature to add items to the list(a tableview) along with the # of items and price. THe thing is, it works(because the tableview doesnt say "No content in table") But the table is blank. Any suggestions?
Code:
package listproject;
public class Main extends Application{
public Node getNodeByRowColumnIndex(final int row,final int column,GridPane gridPane) {
Node result = null;
ObservableList<Node> childrens = gridPane.getChildren();
for(Node node : childrens) {
if(gridPane.getRowIndex(node) == row && gridPane.getColumnIndex(node) == column) {
result = node;
break;
}
}
return result;
}
ObservableList<Product> data =
FXCollections.observableArrayList(
);
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Grocery List");
primaryStage.setWidth(450);
primaryStage.setHeight(800);
//menu buttons
Button addLinkMain = new Button("Add an Item");
Button addLinkTitle = new Button("Add an Item");
Button mainLinkAdd = new Button("Back");
Button mainLinkTitle = new Button("Back");
Button addItem = new Button("Add");
Button setTitleMenu = new Button("Set Title");
Button setTitleAdd = new Button("Set Title");
Button clear = new Button("Clear");
//items for the item addition screen
TextField title = new TextField("Name");
TextField quantity = new TextField("#");
TextField price = new TextField("$");
HBox addBox = new HBox();
BorderPane addBorder = new BorderPane();
HBox addMenu = new HBox();
//mainScene items
BorderPane pane = new BorderPane();
HBox mainscenemenu=new HBox();
TableView<Product> tableview = new TableView<Product>();
//tableview items
TableColumn itemTitle=new TableColumn("Product");
TableColumn itemQuantity=new TableColumn("Quantity");
TableColumn itemPrice=new TableColumn("Price");
//settitle items
TextField listTitle= new TextField("Rename the List");
Button setTittle = new Button("Set List Title");
BorderPane setTitlePane=new BorderPane();
StackPane setTitleFieldContainer=new StackPane();
HBox setTitleOwnMenu = new HBox();
//Scene declaration
Scene mainScene = new Scene(pane);
primaryStage.setScene(mainScene);
Scene addScene = new Scene(addBorder);
Scene setTitleScene = new Scene(setTitlePane);
//Main Scene
pane.setCenter(tableview);
pane.setBottom(mainscenemenu);
tableview.getColumns().addAll(itemTitle,itemQuantity,itemPrice);
mainscenemenu.getChildren().addAll(setTitleMenu,clear,addLinkMain);
tableview.setItems(data);
//Item Addition Scene
addBox.getChildren().addAll(title,quantity,price);
addMenu.getChildren().addAll(mainLinkAdd,addItem,setTitleAdd);
addBorder.setCenter(addBox);
addBorder.setBottom(addMenu);
//Set Title Scene
setTitleFieldContainer.getChildren().addAll(listTitle);
setTitleOwnMenu.getChildren().addAll(addLinkTitle,setTittle,mainLinkTitle);
setTitlePane.setCenter(setTitleFieldContainer);
setTitlePane.setBottom(setTitleOwnMenu);
//Menu Button Event Handlers
addLinkMain.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
primaryStage.setScene(addScene);
}
});
addLinkTitle.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
primaryStage.setScene(addScene);
}
});
mainLinkAdd.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
primaryStage.setScene(mainScene);
}
});
mainLinkTitle.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
primaryStage.setScene(mainScene);
}
});
setTitleMenu.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
primaryStage.setScene(setTitleScene);
}
});
setTitleAdd.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
primaryStage.setScene(setTitleScene);
}
});
//Adding an Item to the List *unfinished
addItem.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event) {
data.add(new Product(title.getText(),quantity.getText(),price.getText()));
}
});
//Changing the Title of The List
setTittle.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
primaryStage.setTitle(listTitle.getText());
}
});
//Clearing the List
clear.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
tableview.getItems().clear();
}
});
//Show the Stage
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
}
You never set the cellValueFactorys for the table's columns. You need to do this to specify the part of the items that should be displayed in the column.
An example with anonymus classes as cellValueFactorys can be found here.
Using PropertyValueFactory could reduce the amount of code needed. More about this here.
A tutorial can be found here: https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/table-view.htm

How do you call graphics class from main class in JavaFX?

I'm a really new programmer so idk if this question sounds really stupid but..
This is my main:
package culminating;
import javafx.application.Application;
& all other necessary imports...
public class CulminatingMAIN extends Application {
//Set Global variables
int count = 0;
String name;
String gender = "Boy";
Label testLabel = new Label(gender + " has been selected");
#Override
public void start(Stage primaryStage) throws Exception {
/**
* ************************ SCENE 1 WORK *************************
*/
TextField nameTextField = new TextField();
nameTextField.setMaxWidth(100);
Label nameLabel = new Label("Please enter your name.");
Label genderLabel = new Label();
Label titleLabel = new Label("Math Adventure!");
titleLabel.setFont(Font.font("Arial", FontWeight.BOLD, 30));
Rectangle titleRectangle = new Rectangle();
titleRectangle.setFill(Color.TOMATO);
titleRectangle.setWidth(280);
titleRectangle.setHeight(60);
titleRectangle.setStroke(Color.BLACK);
titleRectangle.setStrokeWidth(2.0);
StackPane root = new StackPane(titleRectangle, titleLabel);
//Set VBox properties
VBox vbox1 = new VBox(25);
vbox1.setAlignment(Pos.TOP_CENTER);
vbox1.setPadding(new Insets(60, 0, 0, 0));
vbox1.setStyle("-fx-background-color: lightskyblue");
HBox genderBtnBox = new HBox(25);
genderBtnBox.setAlignment(Pos.CENTER);
//Set Scene 1 buttons
Button enterNameBtn = new Button("Enter");
Button goToScene2Btn = new Button("Continue");
//Set Radio Button functionality here
final ToggleGroup genderGroup = new ToggleGroup();
RadioButton rb1 = new RadioButton("Boy");
rb1.setToggleGroup(genderGroup);
rb1.setUserData("Boy");
rb1.setSelected(true);
RadioButton rb2 = new RadioButton("Girl");
rb2.setToggleGroup(genderGroup);
rb2.setUserData("Girl");
//Add panes, labels and buttons to the VBox
vbox1.getChildren().addAll(root, nameLabel, nameTextField, enterNameBtn, genderLabel, genderBtnBox);
Scene scene = new Scene(vbox1, 500, 500);
primaryStage.setScene(scene);
primaryStage.setTitle("Culminating Project");
primaryStage.show();
/**
* ************************ SCENE 2 WORK *************************
*/
//THIS IS ROUGH WORK SO FAR
//Here, testing out new scene to see that it loads properly (and it does)
Circle testCircle = new Circle();
testCircle.setRadius(30);
testCircle.setFill(Color.YELLOW);
StackPane testPane = new StackPane(testCircle, testLabel);
Scene scene2 = new Scene(testPane, 500, 500);
/**
* ************************ EVENTS *************************
*/
//Stores user-entered name and prompts for user gender. Adds Continue button
enterNameBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
if ((count < 1) && (!nameTextField.getText().isEmpty())) {
name = nameTextField.getText();
genderLabel.setText("Hi " + name + "! Please select whether you are a boy or girl.");
genderBtnBox.getChildren().addAll(rb1, rb2);
vbox1.getChildren().add(goToScene2Btn);
count++;
}
}
});
//When pressed, changes the scene so that scene 2 is set instead
goToScene2Btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
primaryStage.setScene(scene2);
}
});
//Radio button selection is stored in gender variable
genderGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
#Override
public void changed(ObservableValue<? extends Toggle> ov,
Toggle old_toggle, Toggle new_toggle) {
if (genderGroup.getSelectedToggle() != null) {
gender = genderGroup.getSelectedToggle().getUserData().toString();
testLabel.setText(gender + " has been selected");
}
}
});
if (gender.equals("boy")){
{
}
}
else if (gender.equals("girl")){
{
}
}
}
public static void main(String[] args) {
launch(args);
}
}
Now I have another class called CharacterGraphic, which I want to call and make the graphic I created in it appear.
package culminating;
& all the other imports
public class CharacterGraphic extends Culminating_JavaFX {
public void start(Stage primaryStage) throws Exception {
String gender = "boy";
Pane pane = new Pane();
pane.setStyle("-fx-background-color: LIGHTBLUE");
pane.setPrefSize(200, 200);
Circle head = new Circle();
head.setRadius(50);
head.setCenterX(240);
head.setCenterY(120);
head.setFill(Color.BURLYWOOD);
etc etc (all other graphics i made)
How do I do this???? And where would I do this?? Any answers really, really appreciated!

JavaFX ChoiceBox ContextMenu Position

I am new to stackoverflow and also to Java programming, though I do have some small programming background.
I am building a little program using JavaFX 8.0.0-b132 and JavaSE 8...
The program has a GUI and this GUI has a ChoiceBox. The architecture for the layout is:
AnchorPane - VBox - TitledPane - GridPane - HBox - ChoiceBox
I have modified the ChoiceBox extensively using CSS.
Everything is working as it should except for one problem:
When I start the program and open the ChoiceBox (per mouse click or with show()) for the first time, it will display it's ContextMenu at the bottom edge as it should; however, when I close and open it again (with mouse or programmatically) it will display the ContextMenu on top of the ChoiceBox open-button (wrong y position), such that the bottom of the currently selected cell coincides with the bottom of the open-button (meaning it will change position after I select another cell and open it again).
How do I prevent this and make the ContextMenu appear always at the bottom as it does at first?
Here is a snippet of the GUI part of the program with the affected control:
EDIT: problem solved: apparently this behavior is the intended ChoiceBox behavior; I posted in the JavaFX forums that it would be better to make the popup behave like that from the start i.e. the first time it is opened so as not to seem buggy.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.input.MouseEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import javafx.collections.*;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Side;
public class bugdemo extends Application {
// Elements needed in both threads
RadioButton radioBoxersOne = new RadioButton("One");
RadioButton radioBoxersTwo = new RadioButton("Two");
TextField warmupText = new TextField("skip warm up");
TextField noCyclesText = new TextField("3");
TextField cycleTimeText = new TextField("45");
TextField restingTimeText = new TextField("30");
String[] warmupTFtext = {"skip warm up", "\u00FCberspringen"};
public static void main(String... args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
// root container
AnchorPane anchor = new AnchorPane();
// Options TitledPane
GridPane trainingOptions = new GridPane();
ToggleGroup toggleGroup = new ToggleGroup();
radioBoxersOne.setToggleGroup(toggleGroup);
radioBoxersTwo.setToggleGroup(toggleGroup);
radioBoxersOne.setSelected(true);
HBox hbToggle = new HBox(15, radioBoxersOne, radioBoxersTwo);
hbToggle.setPadding(new Insets(5,0,0,0));
ObservableList<String> langList = FXCollections.observableArrayList("English", "Deutsch");
ChoiceBox<String> langBox = new ChoiceBox<String>(langList);
langBox.setValue("English");
HBox hbLangBox = new HBox(0, langBox);
hbLangBox.setPadding(new Insets(5,0,0,0));
Label warmupOptionsLabel = new Label("Warm up time (min): ");
Label noCyclesOptionsLabel = new Label("Number of cycles: ");
Label cycleOptionsLabel = new Label("Cycle time (sec): ");
Label restingOptionsLabel = new Label("Resting time (sec): ");
Label noBoxersOptionsLabel = new Label("Number of boxers: ");
Label langOptionsLabel = new Label("Language: ");
trainingOptions.add(warmupOptionsLabel, 0, 0);
trainingOptions.add(noCyclesOptionsLabel, 0, 1);
trainingOptions.add(cycleOptionsLabel, 0, 2);
trainingOptions.add(restingOptionsLabel, 0, 3);
trainingOptions.add(noBoxersOptionsLabel, 0, 4);
trainingOptions.add(langOptionsLabel, 0, 5);
trainingOptions.add(warmupText, 1, 0);
trainingOptions.add(noCyclesText, 1, 1);
trainingOptions.add(cycleTimeText, 1, 2);
trainingOptions.add(restingTimeText, 1, 3);
trainingOptions.add(hbToggle, 1, 4);
trainingOptions.add(hbLangBox, 1, 5);
TitledPane TPane_in = new TitledPane("Options", trainingOptions);
// Options TPane and Buttons together as UI (input)
VBox UI = new VBox(10, /* hbButtons ,*/ TPane_in);
anchor.getChildren().add(UI);
AnchorPane.setLeftAnchor(UI, 25.0);
AnchorPane.setTopAnchor(UI, 100.0);
// EventHandlers for the radio-buttons
radioBoxersOne.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
/* if (!singleBoxer){
trainingProgress.getChildren().remove(boxerLabel);
trainingProgress.getChildren().remove(boxerText);
}
changeTextFlow();
singleBoxer = true; */
}
});
radioBoxersTwo.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
/* singleBoxer = false;
trainingProgress.add(boxerLabel, 0, 3);
trainingProgress.add(boxerText, 1, 3);
changeTextFlow(); */
}
});
// "EventHandler" for the language ChoiceBox
langBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
public void changed(ObservableValue ov, Number value, Number new_value) {
/* language = (int)new_value;
changeLanguage(); */
}
});
// initialize scene and show stage
Scene scene = new Scene(anchor, 800, 650);
primaryStage.setScene(scene);
primaryStage.setResizable(false); // resizeable on/off
// import CSS file
// scene.getStylesheets().add(Letsbox.class.getResource("Training.css").toExternalForm());
primaryStage.show();
langBox.show();
langBox.hide();
langBox.show();
}
}
In this example no CSS is used, but the problem persists...
I tried to fix it with:
/* langBox.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
ContextMenu cm = langBox.getContextMenu();
cm.show(cm.getOwnerNode(), Side.BOTTOM, 0.0, 0.0);
}
}); */
but it throws a NullPointerException...
Please tell me how I could fix this :)
public class Main extends Application{
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Scene scene = new Scene(new Group(), 450, 250);
Button notification = new Button();
notification.setPrefSize(80, 60);
notification.setAlignment(Pos.CENTER);
MenuItem item1 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
MenuItem item2 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences");
}
});
MenuItem item3 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
MenuItem item4 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences");
}
});
MenuItem item5 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
MenuItem item6 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences");
}
});
MenuItem item7 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
MenuItem item8 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences");
}
});
final ContextMenu contextMenu = new ContextMenu(item1, item2,item3, item4,item5, item6,item7, item8);
contextMenu.setMaxSize(50, 50);
contextMenu.setOnShowing(new EventHandler<WindowEvent>() {
public void handle(WindowEvent e) {
System.out.println("showing");
}
});
contextMenu.setOnShown(new EventHandler<WindowEvent>() {
public void handle(WindowEvent e) {
System.out.println("shown");
}
});
// contextMenu.hide();
notification.setContextMenu(contextMenu);
GridPane grid = new GridPane();
grid.setVgap(4);
grid.setHgap(10);
grid.setPadding(new Insets(5, 5, 5, 5));
grid.add(new Label("To: "), 0, 0);
grid.add(notification, 1, 0);
Group root = (Group) scene.getRoot();
root.getChildren().add(grid);
stage.setScene(scene);
stage.show();
notification.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent me)->{
if(me.getButton()==MouseButton.PRIMARY ){
System.out.println("Mouse Left Pressed");
System.out.println(notification.getScaleX());
System.out.println(notification.getScaleY());
System.out.println(me.getScreenX());
System.out.println(me.getScreenY());
contextMenu.show(notification,Side.TOP,0,0);
//contextMenu.show(notification,me.getScreenX(),me.getScreenY());
}else{
contextMenu.hide();
}
});
}
}

Categories