I have an application with checkBox and 2 animated charts: Pie and Bar charts.
When checkBox is checked, method which adds new data to a chart is fired.
When checkBox is *un*checked, method which removes data from a chart is fired.
Everything is ok when i click a checkbox not very fast. But if i'll click checkbox again before its animation is finished, i recieve this one error:
java.lang.NullPointerException
at javafx.scene.chart.PieChart$7.handle(PieChart.java:370)
at javafx.scene.chart.PieChart$7.handle(PieChart.java:367)
at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:217)
at com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineClipCore.java:158)
at javafx.animation.Timeline.impl_playTo(Timeline.java:182)
at com.sun.scenario.animation.shared.SingleLoopClipEnvelope.timePulse(SingleLoopClipEnvelope.java:119)
at javafx.animation.Animation.impl_timePulse(Animation.java:953)
at com.sun.scenario.animation.shared.AnimationPulseReceiver.timePulse(AnimationPulseReceiver.java:117)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:366)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:289)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:454)
at com.sun.javafx.tk.quantum.QuantumToolkit$8.run(QuantumToolkit.java:325)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:62)
at java.lang.Thread.run(Unknown Source)
And this all happens only with PieChart. BarChart works normally.
Here is a method for checkBox onAction event:
public void OnCheckFired(ActionEvent event) {
Boolean b = checkBox.isSelected();
chart.modifyChart(currentChart,b);
}
Thant is a code, which is fired, for modification of pie chart:
#Override
public void makeModifications(Chart chart, Boolean b) {
PieChart pie = (PieChart) chart;
ObservableList<PieChart.Data> pieChartData = pie.getData();
pieChartData = modify(b, pieChartData);
pie.setData(pieChartData);
}
private ObservableList<Data> modify(Boolean b, ObservableList<PieChart.Data> pieChartData) {
if (b) {
pieChartData.add(new PieChart.Data("newValue", 34));
} else {
int size = pieChartData.size() - 1;
pieChartData.remove(size);
}
return pieChartData;
}
I have modified the code attached to the bug issue which is Sergey mentioned in his answer. Replaced the button with checkbox that is being disabled while the animation is playing and extra half second when the animation finished. It behaves this way every time when the selection changed.
public class Tdesst extends Application {
private void init(Stage primaryStage) {
VBox root = new VBox();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
final ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("Sun", 20),
new PieChart.Data("IBM", 12),
new PieChart.Data("HP", 25),
new PieChart.Data("Dell", 22),
new PieChart.Data("Apple", 30));
final PieChart chart = new PieChart(pieChartData);
final CheckBox chkbox = new CheckBox("add/remove data");
final Timeline animation = new Timeline(
new KeyFrame(Duration.seconds(.5),
new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent actionEvent) {
chkbox.setDisable(false);
}
}));
animation.setCycleCount(1);
chkbox.selectedProperty().addListener(new ChangeListener<Boolean>() {
#Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldVal, Boolean newVal) {
chkbox.setDisable(true);
if (newVal) {
pieChartData.add(new PieChart.Data("newValue", 34));
} else {
pieChartData.remove(pieChartData.size() - 1);
}
animation.play();
}
});
root.getChildren().add(chart);
root.getChildren().add(chkbox);
}
#Override
public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
primaryStage.setTitle(VersionInfo.getRuntimeVersion());
}
public static void main(String[] args) {
launch(args);
}
}
Or you can totally disable animating by
chart.setAnimated(false);
Unfortunately you found a bug. I've filed it as http://javafx-jira.kenai.com/browse/RT-21783
Meanwhile as a workaround you can disable checkbox for a second after each click.
Related
I am using Javafx8 and lets say i have created many ui items (like buttons and so on...). I thought its a good idea to put all EventHandler for these buttons into a separates class. My question is: How can i get access from an EventHandler to any button, e.g. to diable it or to manipulate it on any other way.
Here is a minimum example with two buttons and a separate class for EventHandlers
Let's say this is my Start class:
public class App extends Application
{
#Override
public void start(Stage primaryStage) throws Exception {
Button b1 = new Button();
b1.setOnAction(ListenerClass.createB1Event());
Button b2 = new Button();
b2.setOnAction(ListenerClass.createB2Event());
VBox vbox = new VBox(b1, b2);
Scene scene = new Scene(vbox, 200, 200);
primaryStage.setTitle("App");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main( String[] args )
{
Application.launch(args);
}
}
And my (separate) listener class:
public class ListenerClass {
public static EventHandler<ActionEvent> createB1Event() {
return new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
//Access here to b1 and b2...
//Deactivate B1
//Activate B2
}
};
}
public static EventHandler<ActionEvent> createB2Event() {
return new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
//Access here to b1 and b2...
//Activate B1
//Dectivate B2
}
};
}
}
Thanks
So based on your principal. You want the button that is located in a vbox and got pressed to be disabled and all other buttons from that vbox to be enabled.
And your problem is how to find button that got pressed.
You need to use ActionEvent.getSource() method.
Heres how I'd code it....
This is Start class:
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
VBox vbox = new VBox();
Button b1 = addNewButton("Button1",vbox);
Button b2 = addNewButton("Button2",vbox);
Button b3 = addNewButton("Button3",vbox);
Scene scene = new Scene(vbox, 200, 200);
primaryStage.setTitle("App");
primaryStage.setScene(scene);
primaryStage.show();
}
public static Button addNewButton(String label, VBox ownerVBox){
Button button = new Button(label);
ownerVBox.getChildren().add(button);
button.setOnAction(ListenerClass.createBEvent());
return button;
}
public static void main(String[] args) {
launch(args);
}
}
Listener class:
public class ListenerClass {
public static EventHandler<ActionEvent> createBEvent() {
return new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
Button b = (Button) t.getSource();
VBox vbox =(VBox) b.getParent();
vbox.getChildren().forEach(button-> {
button.setDisable(false);
});
b.setDisable(true);
}
};
}
}
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;
}
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
This is my first question here so donĀ“t be to mad at me if something is wrong. I am currently programming an idle like game for java with help of JavaFx.
The code here under was written with eclipse mars and JavaFx. Eclipse shows to me that the code is right but when I want to start the program it throws this block of error messages:
public class Main extends Application{
private static final Color color = Color.web("#464646");
Button button3 = new Button("D");
DropShadow shadow = new DropShadow();
Label label = new Label();
public static void main(String[] args) throws Exception {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
Scene scene = new Scene(new Group());
primaryStage.setTitle("Button Sample");
primaryStage.setWidth(300);
primaryStage.setHeight(190);
label.setFont(Font.font("Times New Roman", 22));
label.setTextFill(color);
Image imageDecline = new Image(getClass().getResourceAsStream("../not.png"));
Image imageAccept = new Image(getClass().getResourceAsStream("../ok.png"));
VBox vbox = new VBox();
vbox.setLayoutX(20);
vbox.setLayoutY(20);
HBox hbox1 = new HBox();
HBox hbox2 = new HBox();
Button button1 = new Button("Accept");
button1.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
button1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
label.setText("Accepted");
}
});
Button button2 = new Button("Accept");
button2.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
label.setText("Accepted");
}
});
button3.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
label.setText("Declined");
}
});
button3.addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
button3.setEffect(shadow);
}
});
button3.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
button3.setEffect(null);
}
});
hbox1.getChildren().add(button2);
hbox1.getChildren().add(button3);
hbox1.getChildren().add(label);
hbox1.setSpacing(10);
hbox1.setAlignment(Pos.BOTTOM_CENTER);
Button button4 = new Button();
button4.setGraphic(new ImageView(imageAccept));
button4.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
label.setText("Accepted");
}
});
Button button5 = new Button();
button5.setGraphic(new ImageView(imageDecline));
button5.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
label.setText("Declined");
}
});
hbox2.getChildren().add(button4);
hbox2.getChildren().add(button5);
hbox2.setSpacing(25);
vbox.getChildren().add(button1);
vbox.getChildren().add(hbox1);
vbox.getChildren().add(hbox2);
vbox.setSpacing(10);
((Group) scene.getRoot()).getChildren().add(vbox);
primaryStage.setScene(scene);
primaryStage.show();
}}
I hope anyone here has a hint or a solution for this problem :) Would be very nice from you.
Best Greetings,
Mike
I think your problem has to do with the input streams you use for creating your Image objects:
... = new Image(getClass().getResourceAsStream("../not.png"));
When I start your application in Eclipse, I get an InvocationTargetException as well. It is caused by a NullPointerException because the PNG files cannot be found.
Try to start your application without creating the Image objects and see if you still get any exceptions.
I'm trying to add a MouseClicked event listener to a Tree Cell so that It fires when a user clicks on either the Graphic or the String value of the cell.
TestApp.java
public class TestApp extends Application {
#Override
public void start(Stage stage) throws Exception {
Image image = new Image(getClass().getResourceAsStream("icon.png"));
TreeItem<String> root = new TreeItem<String>("Root");
root.setGraphic(new ImageView(image));
TreeItem<String> child = new TreeItem<String>("Child");
root.getChildren().add(child);
TreeView<String> tree = new TreeView<String>(root);
tree.setCellFactory(new TestTreeCellFactory());
StackPane pane = new StackPane();
pane.getChildren().add(tree);
stage.setScene(new Scene(pane, 300, 250));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
private class TestTreeCellFactory implements Callback<TreeView<String>, TreeCell<String>> {
#Override
public TreeCell<String> call(TreeView<String> treeView) {
CheckBoxTreeCell<String> tc = new CheckBoxTreeCell<String>();
tc.setOnMouseClicked((event) -> mousePressed(event, treeView));
return tc;
}
public void mousePressed(MouseEvent event, TreeView<String> treeView) {
if(event.getButton().equals(MouseButton.SECONDARY)) {
System.out.println("Right Mouse Button Clicked!");
}
}
}
}
The mouse event is currently firing when I click on the String Value but is not firing when I click on the Graphic icon. Upon trying to debug the issue, here are some of my observations:
It seems like the Graphic is somehow tied to the Checkbox (clicking on the Graphic will toggle the checkbox value).
Clicking the Graphic will not select the row while clicking on the text value will.
Is there a way to attach the listener to the Tree Cell's graphic once it gets populated or am I thinking about this in the wrong way?