I am new to JavaFX/RCP and trying to execute one sample JavaFX graph in RCP application. It is displaying empty dialogue, without graph. Can anyone help me in identifying the issue ?
#Execute
public void execute(final Shell shell) {
if ( shlInfrastructureReport != null && !shlInfrastructureReport.isDisposed()){
shlInfrastructureReport.close();
}
shlInfrastructureReport = new Shell(shell.getDisplay());
final FXCanvas fxCanvas = new FXCanvas(shlInfrastructureReport, SWT.NONE);
Group group = new Group();
Scene scene = new Scene(group);
final NumberAxis xAxis = new NumberAxis(1, 31, 1);
final NumberAxis yAxis = new NumberAxis();
final AreaChart<Number,Number> areaChat = new AreaChart<Number,Number>(xAxis,yAxis);
areaChat.setTitle("Sample Report");
XYChart.Series series= new XYChart.Series();
series.setName("Sample");
series.getData().add(new XYChart.Data(1, 4));
series.getData().add(new XYChart.Data(3, 10));
series.getData().add(new XYChart.Data(6, 15));
series.getData().add(new XYChart.Data(9, 8));
series.getData().add(new XYChart.Data(12, 5));
series.getData().add(new XYChart.Data(15, 18));
series.getData().add(new XYChart.Data(18, 15));
series.getData().add(new XYChart.Data(21, 13));
series.getData().add(new XYChart.Data(24, 19));
series.getData().add(new XYChart.Data(27, 21));
series.getData().add(new XYChart.Data(30, 21));
areaChat.getData().addAll(series);
group.getChildren().add(areaChat);
fxCanvas.setScene(scene);
Thanks, it got resolution. Issue is i missed to set layout.
final RowLayout layout = new RowLayout();
shlInfrastructureReport.setLayout(layout);
Related
this is a follow up to this question: How to get the color from a series in JavaFX Chart and assign it to a checkbox which has 0 answers.
I want to get my LineChart series stroke generated color, in order to set it as the text color of a checkbox.
This is my sample code (the line chart taken from Oracle tutorial):
public class LineChartSample extends Application {
#Override
public void start(Stage stage) {
HBox box = new HBox();
stage.setTitle("Line Chart Sample");
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Number of Month");
final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);
lineChart.setTitle("Stock Monitoring, 2010");
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
series.getData().add(new XYChart.Data(1, 23));
series.getData().add(new XYChart.Data(2, 14));
series.getData().add(new XYChart.Data(3, 15));
series.getData().add(new XYChart.Data(4, 24));
series.getData().add(new XYChart.Data(5, 34));
series.getData().add(new XYChart.Data(6, 36));
series.getData().add(new XYChart.Data(7, 22));
series.getData().add(new XYChart.Data(8, 45));
series.getData().add(new XYChart.Data(9, 43));
series.getData().add(new XYChart.Data(10, 17));
series.getData().add(new XYChart.Data(11, 29));
series.getData().add(new XYChart.Data(12, 25));
lineChart.getData().add(series);
CheckBox test = new CheckBox("test");
test.setStyle("-fx-text-fill: <series color>;");
box.getChildren().addAll(lineChart, test);
Scene scene = new Scene(box, 800, 600);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I tried to add the series style class to the checkbox styleclass (as suggested in Getting series color from LineChart):
test.getStyleClass().addAll(series.getNode().getStyleClass());
where the series style class is a list with the values: chart-series-line, series0, and default-color0 but it didn't work.
Thanks in advance for your help.
I am trying to get a screenshot of the chart node. So even though the axis values are added to the chart they are not displayed in the screenshot image..
public void saveProfitLineChartToFile(ObservableList<MonthlyRecord> monthlyRecords,String path) throws IOException {
CategoryAxis monthAxis = new CategoryAxis();
monthAxis.setLabel("Month");
NumberAxis profitAxis = new NumberAxis();
profitAxis.setLabel("Profit (Rs)");
LineChart profitLineChart = new LineChart(monthAxis, profitAxis);
XYChart.Series series1 = new XYChart.Series();
int[] profitValues = new int[monthlyRecords.size()];
int[] monthNo = new int[monthlyRecords.size()];
for (int i = 0; i < monthlyRecords.size(); i++) {
profitValues[i] = Integer.parseInt(monthlyRecords.get(i).getProfit());
monthNo[i] = Integer.parseInt(monthlyRecords.get(i).getMonth());
series1.getData().add(new XYChart.Data(monthName[monthNo[i]-1], profitValues[i]));
}
profitLineChart.getData().addAll(series1);
profitLineChart.setLegendVisible(false);
Scene sceneForChart = new Scene(profitLineChart, 800, 600);
WritableImage image = profitLineChart.snapshot(new SnapshotParameters(), null);
File file = new File(path);
try {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
} catch (IOException e) {
e.printStackTrace();
}
}
i found topics with similar theme, but i couldn't resolve my issue.i have a small code for gui but i can not make my window to have that border and title in the smae line. the closest i got was border and just bellow title.Here is my code:
public class NumberAddition extends Application {
BorderedTitledPane root;
GridPane pane = new GridPane();
#Override
public void start(Stage primaryStage) {
Label first_lbl = new Label("First number: ");
pane.setConstraints(first_lbl, 0, 0);
Label second_lbl = new Label("Second number: ");
pane.setConstraints(second_lbl, 0, 1);
Label third_lbl = new Label("Result: ");
pane.setConstraints(third_lbl, 0, 2);
Button btn = new Button();
btn.setText("Add");
btn.setMinWidth(70);
Button btn_1 = new Button();
btn_1.setText("Clear");
btn_1.setMinWidth(70);
Button btn_2 = new Button();
btn_2.setText("Exit");
TextField txt_1 = new TextField();
txt_1.setPrefWidth(200);
pane.setConstraints(txt_1, 1, 0);
TextField txt_2 = new TextField();
txt_2.setPrefWidth(200);
pane.setConstraints(txt_2, 1, 1);
TextField txt_3 = new TextField();
txt_3.setPrefWidth(200);
pane.setConstraints(txt_3, 1, 2);
HBox box = new HBox();
pane.setConstraints(box, 1, 4);
box.setPadding(new Insets(5, 50, 5, 50));
box.setSpacing(50);
box.getChildren().addAll(btn, btn_1);
root = new BorderedTitledPane("Number addition");
root.setPadding(new Insets(20, 20, 20, 20));
root.getStylesheets().add(getClass().getResource("bordered-titled-title.css").toExternalForm());
root.getChildren().add(pane);
pane.setPadding(new Insets(20, 20, 20, 20));
pane.setVgap(5);
pane.setHgap(5);
pane.getChildren().addAll(first_lbl, txt_1, second_lbl, txt_2, third_lbl, txt_3, box);
// root.getChildren().add(btn);
Scene scene = new Scene(root, 450, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
public class BorderedTitledPane extends StackPane{
BorderedTitledPane(String titleString) {
Label title = new Label(" " + titleString + " ");
title.getStyleClass().add("bordered-titled-title");
StackPane.setAlignment(title, Pos.TOP_LEFT);
getStyleClass().add("bordered-titled-border");
this.getChildren().addAll(title);
}
}
and css:
.label {
-fx-font: 12px sans-serif;
}
.bordered-titled-title {
-fx-background-color: white;
-fx-translate-y: -16;
}
.bordered-titled-border {
-fx-content-display: top;
-fx-border-insets: 20 15 15 15;
-fx-background-color: white;
-fx-border-color: black;
-fx-border-width: 2;
}
.bordered-titled-content {
-fx-padding: 26 10 10 10;
}
Use this way
public class NumberAddition extends Application {
GridPane pane = new GridPane();
#SuppressWarnings("static-access")
#Override
public void start(Stage primaryStage) {
Label first_lbl = new Label("First number: ");
pane.setConstraints(first_lbl, 0, 0);
Label second_lbl = new Label("Second number: ");
pane.setConstraints(second_lbl, 0, 1);
Label third_lbl = new Label("Result: ");
pane.setConstraints(third_lbl, 0, 2);
Button btn = new Button();
btn.setText("Add");
btn.setMinWidth(70);
Button btn_1 = new Button();
btn_1.setText("Clear");
btn_1.setMinWidth(70);
Button btn_2 = new Button();
btn_2.setText("Exit");
TextField txt_1 = new TextField();
txt_1.setPrefWidth(200);
pane.setConstraints(txt_1, 1, 0);
TextField txt_2 = new TextField();
txt_2.setPrefWidth(200);
pane.setConstraints(txt_2, 1, 1);
TextField txt_3 = new TextField();
txt_3.setPrefWidth(200);
pane.setConstraints(txt_3, 1, 2);
HBox box = new HBox();
pane.setConstraints(box, 1, 4);
box.setPadding(new Insets(5, 50, 5, 50));
box.setSpacing(50);
box.getChildren().addAll(btn, btn_1);
pane.setVgap(5);
pane.setHgap(5);
pane.getChildren().addAll(first_lbl, txt_1, second_lbl, txt_2, third_lbl, txt_3, box);
final String TITLE = "Number addition";
Pane titledContent = new BorderedTitledPane(TITLE, pane);
titledContent.setStyle("-fx-content-display: top;" + "-fx-border-insets: 20 15 15 15;"
+ "-fx-background-color: #eff1f4;" + "-fx-border-color: black;" + "-fx-border-width: 0.5;");
titledContent.setPrefSize(pane.getMaxWidth(), pane.getMaxHeight());
Scene scene = new Scene(titledContent);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
class BorderedTitledPane extends StackPane {
BorderedTitledPane(String titleString, Node content) {
Label title = new Label(" " + titleString + " ");
title.setStyle("-fx-background-color: #eff1f4;" + "-fx-translate-y: -10;");
title.setFont(Font.font(null, FontWeight.BOLD, 14));
StackPane.setAlignment(title, Pos.TOP_CENTER);
StackPane contentPane = new StackPane();
content.setStyle("-fx-padding: 20 5 5 5;");
contentPane.getChildren().add(content);
getChildren().addAll(title, contentPane);
}
}
}
Is it possible to make right side of BorderPane to have height of window and bottom/top sides then end with the beginning of right side?
AFAIK there is no way to do this, but you can achieve the desired result using a GridPane
private static void setBackground(Region region, Color color) {
region.setBackground(new Background(new BackgroundFill(color, CornerRadii.EMPTY, Insets.EMPTY)));
}
#Override
public void start(Stage primaryStage) {
GridPane gridPane = new GridPane();
RowConstraints rConstranits1 = new RowConstraints();
rConstranits1.setVgrow(Priority.NEVER);
RowConstraints rConstranits2 = new RowConstraints();
rConstranits2.setVgrow(Priority.ALWAYS);
RowConstraints rConstranits3 = new RowConstraints();
rConstranits3.setVgrow(Priority.NEVER);
ColumnConstraints cConstraints1 = new ColumnConstraints();
cConstraints1.setHgrow(Priority.NEVER);
ColumnConstraints cConstraints2 = new ColumnConstraints();
cConstraints2.setHgrow(Priority.ALWAYS);
ColumnConstraints cConstraints3 = new ColumnConstraints();
cConstraints3.setHgrow(Priority.NEVER);
gridPane.getColumnConstraints().addAll(cConstraints1, cConstraints2, cConstraints3);
gridPane.getRowConstraints().addAll(rConstranits1, rConstranits2, rConstranits3);
Region top = new Region();
top.setPrefSize(300, 100);
setBackground(top, Color.RED);
Region bottom = new Region();
bottom.setPrefSize(400, 50);
setBackground(bottom, Color.YELLOW);
Region center = new Region();
setBackground(center, Color.BLUE);
Region right = new Region();
setBackground(right, Color.LIME);
right.setPrefSize(200, 500);
Region left = new Region();
setBackground(left, Color.BROWN);
left.setPrefSize(200, 400);
gridPane.add(right, 2, 0, 1, 3);
cConstraints3.maxWidthProperty().bind(right.prefWidthProperty());
cConstraints3.minWidthProperty().bind(right.prefWidthProperty());
gridPane.add(top, 0, 0, 2, 1);
rConstranits1.minHeightProperty().bind(top.prefHeightProperty());
rConstranits1.maxHeightProperty().bind(top.prefHeightProperty());
gridPane.add(bottom, 0, 2, 2, 1);
rConstranits3.minHeightProperty().bind(bottom.prefHeightProperty());
rConstranits3.maxHeightProperty().bind(bottom.prefHeightProperty());
gridPane.add(center, 1, 1);
gridPane.add(left, 0, 1);
cConstraints1.minWidthProperty().bind(left.prefWidthProperty());
cConstraints1.maxWidthProperty().bind(left.prefWidthProperty());
Scene scene = new Scene(gridPane);
primaryStage.setScene(scene);
primaryStage.show();
}
Nesting two BorderPanes is another option, which then relies on BorderPane's built in height & width management. Credit to fabian for the example basis and setBackground routine!
private static void setBackground(Region region, Color color) {
region.setBackground(new Background(new BackgroundFill(color, CornerRadii.EMPTY, Insets.EMPTY)));
}
#Override
public void start(Stage primaryStage) {
BorderPane outer = new BorderPane();
BorderPane inner = new BorderPane();
Region top = new Region();
top.setPrefSize(300, 300);
setBackground(top, Color.RED);
Region bottom = new Region();
bottom.setPrefSize(400, 200);
setBackground(bottom, Color.YELLOW);
Region right = new Region();
setBackground(right, Color.BLUE);
right.setPrefSize(200, 500);
inner.setCenter(top);
inner.setBottom(bottom);
outer.setCenter(inner);
outer.setRight(right);
Scene s = new Scene(outer);
primaryStage.setScene(s);
primaryStage.show();
}
I created this very simple example of JavaFX web browser.
StackPane secondaryLayout = new StackPane();
Scene secondScene = new Scene(secondaryLayout, 200, 100);
Stage secondStage = new Stage();
secondStage.setTitle("Second Stage");
secondStage.setScene(secondScene);
WebView browser = new WebView();
WebEngine engine = browser.getEngine();
String url = "http://zoranpavlovic.blogspot.com/";
engine.load(url);
StackPane sp = new StackPane();
sp.getChildren().add(browser);
Scene root = new Scene(sp, 600, 600);
secondStage.setScene(root);
secondStage.show();
I would like to add input filed into the to size of the window and button "Go". Can you help me to implement this?
You can have a HBox with a textfield and a button and on button's action you can load the webengine.
I wrote the code using notepad, so errors may crept in
StackPane secondaryLayout = new StackPane();
Scene secondScene = new Scene(secondaryLayout, 200, 100);
Stage secondStage = new Stage();
secondStage.setTitle("Second Stage");
secondStage.setScene(secondScene);
HBox box = new HBox();
TextField textField = new TextField();
Button go = new Button();
box.getChildren.addAll(textField, go);
WebView browser = new WebView();
WebEngine engine = browser.getEngine();
go.setOnAction(new Eventhandler<ActionEvent>(){
#Override public void handle(ActionEvent e) {
String url = textField.getText();
engine.load(url);
}
});
BorderPane sp = new BorderPane();
sp.setTop(box);
sp.setCenter(browser);
Scene root = new Scene(sp, 600, 600);
secondStage.setScene(root);
secondStage.show();