0% found this document useful (0 votes)
26 views

Package Line

This document defines a JavaFX application that displays a line chart and editable table view. It creates observable lists of data for the chart and table, defines columns for the table, and includes an inner class for editable table cells.

Uploaded by

iyus walupi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Package Line

This document defines a JavaFX application that displays a line chart and editable table view. It creates observable lists of data for the chart and table, defines columns for the table, and includes an inner class for editable table cells.

Uploaded by

iyus walupi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

package line_chart;
2.
3. import javafx.application.Application;
4. import static javafx.application.Application.launch;
5. import javafx.collections.FXCollections;
6. import javafx.collections.ObservableList;
7. import javafx.event.EventHandler;
8. import javafx.scene.Group;
9. import javafx.scene.Scene;
10. import javafx.scene.chart.*;
11. import javafx.scene.control.*;
12. import javafx.scene.control.cell.PropertyValueFactory;
13. import javafx.scene.input.KeyCode;
14. import javafx.scene.input.KeyEvent;
15. import javafx.scene.layout.HBox;
16. import javafx.stage.Stage;
17. import javafx.util.Callback;
18. /**
19. *
20. * @author Marc Alvan
21. */
22. public class Line_Chart extends Application {
23.
24. private TableView<XYChart.Data> tableView = new TableView<>();
25.
26. private ObservableList<XYChart.Data> dataList =
27. FXCollections.observableArrayList(
28. new XYChart.Data("January", 0),
29. new XYChart.Data("February", 0),
30. new XYChart.Data("March", 0),
31. new XYChart.Data("April", 0),
32. new XYChart.Data("May", 0),
33. new XYChart.Data("June", 0),
34. new XYChart.Data("July", 0),
35. new XYChart.Data("August", 0),
36. new XYChart.Data("September", 0),
37. new XYChart.Data("October", 0),
38. new XYChart.Data("November", 0),
39. new XYChart.Data("December", 0));
40.
41. /**
42. * @param args the command line arguments
43. */
44. public static void main(String[] args) {
45. launch(args);
46. }
47.
48. @Override
49. public void start(Stage primaryStage) {
50. primaryStage.setTitle("Line Chart");
51.
52. Group root = new Group();
53.
54. tableView.setEditable(true);
55. Callback<TableColumn, TableCell> cellFactory =
56. new Callback<TableColumn, TableCell>() {
57. @Override
58. public TableCell call(TableColumn p) {
59. return new EditingCell();
60. }
61. };
62.
63. TableColumn columnMonth = new TableColumn("Month");
64. columnMonth.setCellValueFactory(
65. new PropertyValueFactory<XYChart.Data,String>("XValue"));
66.
67. TableColumn columnValue = new TableColumn("Value");
68. columnValue.setCellValueFactory(
69. new PropertyValueFactory<XYChart.Data,Number>("YValue"));
70.
71. //--- Add for Editable Cell of Value field, in Number
72. columnValue.setCellFactory(cellFactory);
73.
74. columnValue.setOnEditCommit(
75. new EventHandler<TableColumn.CellEditEvent<XYChart.Data, Number>>() {
76. @Override public void handle(TableColumn.CellEditEvent<XYChart.Data, Number> t) {
77. ((XYChart.Data)t.getTableView().getItems().get(
78. t.getTablePosition().getRow())).setYValue(t.getNewValue());
79. }
80. });
81.
82. //---
83.
84. //--- Prepare LineChart
85. final CategoryAxis xAxis = new CategoryAxis();
86. final NumberAxis yAxis = new NumberAxis();
87.
88. xAxis.setLabel("Month");
89. yAxis.setLabel("Value");
90.
91. final LineChart<String, Number> lineChart = new LineChart<>(xAxis,yAxis);
92.
93. lineChart.setTitle("LineChart");
94. XYChart.Series series = new XYChart.Series(dataList);
95. series.setName("XYChart.Series");
96. lineChart.getData().add(series);
97.
98. //--- Prepare TableView
99.
100. tableView.setItems(dataList);
101. tableView.getColumns().addAll(columnMonth, columnValue);
102.
103. //---
104.
105. HBox hBox = new HBox();
106. hBox.setSpacing(10);
107. hBox.getChildren().addAll(tableView, lineChart);
108.
109. root.getChildren().add(hBox);
110.
111. primaryStage.setScene(new Scene(root, 670, 400));
112. primaryStage.show();
113. }
114.
115. class EditingCell extends TableCell<XYChart.Data, Number> {
116.
117. private TextField textField;
118.
119. public EditingCell() {}
120.
121. @Override
122. public void startEdit() {
123. super.startEdit();
124.
125. if (textField == null) {
126. createTextField();
127. }
128.
129. setGraphic(textField);
130. setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
131. textField.selectAll();
132. }
133.
134. @Override
135. public void cancelEdit() {
136. super.cancelEdit();
137.
138. setText(String.valueOf(getItem()));
139. setContentDisplay(ContentDisplay.TEXT_ONLY);
140. }
141.
142. @Override
143. public void updateItem(Number item, boolean empty) {
144. super.updateItem(item, empty);
145.
146. if (empty) {
147. setText(null);
148. setGraphic(null);
149. } else {
150. if (isEditing()) {
151. if (textField != null) {
152. textField.setText(getString());
153. }
154. setGraphic(textField);
155. setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
156. } else {
157. setText(getString());
158. setContentDisplay(ContentDisplay.TEXT_ONLY);
159. }
160. }
161. }
162.
163. private void createTextField() {
164. textField = new TextField(getString());
165. textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()*2);
166. textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
167.
168. @Override
169. public void handle(KeyEvent t) {
170. if (t.getCode() == KeyCode.ENTER) {
171. commitEdit(Double.parseDouble(textField.getText()));
172. } else if (t.getCode() == KeyCode.ESCAPE) {
173. cancelEdit();
174. }
175. }
176. });
177. }
178.
179. private String getString() {
180. return getItem() == null ? "" : getItem().toString();
181. }
182. }
183.
184. }

You might also like