Run the code:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class JavaCharts extends Application {
public static void main(String[] args) {
launch(args);
}
PieChart testedPieChart = new PieChart();
@Override
public void start(Stage stage) throws Exception {
ObservableList<PieChart.Data> data = FXCollections.<PieChart.Data>observableArrayList();
for (int i = 0; i < 10; i++) {
data.add(new PieChart.Data("Data item " + i, i * 100));
}
testedPieChart.setData(data);
testedPieChart.setTitle("PieChart");
testedPieChart.setStyle("-fx-border-color: darkgray;");
testedPieChart.setPrefSize(300, 600);
Pane pane = new Pane();
pane.getChildren().add(testedPieChart);
VBox vb = new VBox();
vb.getChildren().addAll(pane);
Slider slider1 = new Slider(0, 600, 300);
slider1.setShowTickLabels(true);
slider1.setShowTickMarks(true);
slider1.valueProperty().bindBidirectional(testedPieChart.prefHeightProperty());
vb.getChildren().addAll(new Label("Pref height"), slider1);
Slider slider2 = new Slider(0, 600, 300);
slider2.setShowTickLabels(true);
slider2.setShowTickMarks(true);
slider2.valueProperty().bindBidirectional(testedPieChart.prefWidthProperty());
vb.getChildren().addAll(new Label("Pref width "), slider2);
vb.getChildren().addAll(getAddItemHBox());
pane.setPrefSize(600, 600);
Scene scene = new Scene(vb, 700, 700);
stage.setScene(scene);
stage.show();
}
public HBox getAddItemHBox() {
HBox hb = new HBox();
Label lb = new Label("Add item");
final TextField tf = TextFieldBuilder.create().prefWidth(150).build();
Label namedLabel = new Label(" named ");
final TextField name = TextFieldBuilder.create().prefWidth(150).build();
Label atLb = new Label("at pos");
final TextField tfPos = TextFieldBuilder.create().prefWidth(150).build();
Button bt = ButtonBuilder.create().text("Add!").build();
bt.setOnAction(new EventHandler() {
@Override
public void handle(Event t) {
int index = Integer.parseInt(tfPos.getText());
PieChart.Data newData = new PieChart.Data("".equals(name.getText()) ? String.valueOf(index) : name.getText(), Double.parseDouble(tf.getText()));
((PieChart) testedPieChart).getData().add(index, newData);
}
});
hb.getChildren().addAll(lb, tf, namedLabel, name, atLb, tfPos, bt);
return hb;
}
}
Type in values:
height: 550
width: 300
Add item: 123
named:Aaaaaaaaaaaaaaa ffffffffffffffffff gggggggggggg
At pos: 2
Click button "Add"
And look at the attached movie.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class JavaCharts extends Application {
public static void main(String[] args) {
launch(args);
}
PieChart testedPieChart = new PieChart();
@Override
public void start(Stage stage) throws Exception {
ObservableList<PieChart.Data> data = FXCollections.<PieChart.Data>observableArrayList();
for (int i = 0; i < 10; i++) {
data.add(new PieChart.Data("Data item " + i, i * 100));
}
testedPieChart.setData(data);
testedPieChart.setTitle("PieChart");
testedPieChart.setStyle("-fx-border-color: darkgray;");
testedPieChart.setPrefSize(300, 600);
Pane pane = new Pane();
pane.getChildren().add(testedPieChart);
VBox vb = new VBox();
vb.getChildren().addAll(pane);
Slider slider1 = new Slider(0, 600, 300);
slider1.setShowTickLabels(true);
slider1.setShowTickMarks(true);
slider1.valueProperty().bindBidirectional(testedPieChart.prefHeightProperty());
vb.getChildren().addAll(new Label("Pref height"), slider1);
Slider slider2 = new Slider(0, 600, 300);
slider2.setShowTickLabels(true);
slider2.setShowTickMarks(true);
slider2.valueProperty().bindBidirectional(testedPieChart.prefWidthProperty());
vb.getChildren().addAll(new Label("Pref width "), slider2);
vb.getChildren().addAll(getAddItemHBox());
pane.setPrefSize(600, 600);
Scene scene = new Scene(vb, 700, 700);
stage.setScene(scene);
stage.show();
}
public HBox getAddItemHBox() {
HBox hb = new HBox();
Label lb = new Label("Add item");
final TextField tf = TextFieldBuilder.create().prefWidth(150).build();
Label namedLabel = new Label(" named ");
final TextField name = TextFieldBuilder.create().prefWidth(150).build();
Label atLb = new Label("at pos");
final TextField tfPos = TextFieldBuilder.create().prefWidth(150).build();
Button bt = ButtonBuilder.create().text("Add!").build();
bt.setOnAction(new EventHandler() {
@Override
public void handle(Event t) {
int index = Integer.parseInt(tfPos.getText());
PieChart.Data newData = new PieChart.Data("".equals(name.getText()) ? String.valueOf(index) : name.getText(), Double.parseDouble(tf.getText()));
((PieChart) testedPieChart).getData().add(index, newData);
}
});
hb.getChildren().addAll(lb, tf, namedLabel, name, atLb, tfPos, bt);
return hb;
}
}
Type in values:
height: 550
width: 300
Add item: 123
named:Aaaaaaaaaaaaaaa ffffffffffffffffff gggggggggggg
At pos: 2
Click button "Add"
And look at the attached movie.