質問編集履歴

1

サンプル作成

2017/03/15 07:15

投稿

oscikonome
oscikonome

スコア16

test CHANGED
File without changes
test CHANGED
@@ -59,3 +59,187 @@
59
59
  }
60
60
 
61
61
  });
62
+
63
+
64
+
65
+ *その後
66
+
67
+
68
+
69
+ 回答を参考に、以下のようにしておおよその実装はできましたが、まだうまくいっていない点があります。
70
+
71
+ ・カラム幅を変更しないとデータバーが表示されない。(初期描画時点ではデータバーが表示されず)
72
+
73
+ ・カラム幅を画面幅まで広げたあと縮めると、少し広げても30の数字が表示されない。(画面幅まで広げないと表示されない)
74
+
75
+ ・カラム幅を変更してからスクロールすると、一部データバーの長さがおかしい。(カラム幅を変更するとなおる)
76
+
77
+ いずれも幅変更とスクロールで解決する表示の問題ですが、都度変更しなくてもきれいに表示されるようにすることは可能でしょうか。
78
+
79
+
80
+
81
+ よろしくお願いいたします。
82
+
83
+
84
+
85
+ import javafx.application.Application;
86
+
87
+ import javafx.beans.property.IntegerProperty;
88
+
89
+ import javafx.beans.property.SimpleIntegerProperty;
90
+
91
+ import javafx.collections.FXCollections;
92
+
93
+ import javafx.collections.ObservableList;
94
+
95
+ import javafx.scene.Scene;
96
+
97
+ import javafx.scene.control.Label;
98
+
99
+ import javafx.scene.control.TableCell;
100
+
101
+ import javafx.scene.control.TableColumn;
102
+
103
+ import javafx.scene.control.TableView;
104
+
105
+ import javafx.scene.layout.AnchorPane;
106
+
107
+ import javafx.scene.paint.Paint;
108
+
109
+ import javafx.scene.shape.Rectangle;
110
+
111
+ import javafx.stage.Stage;
112
+
113
+
114
+
115
+ public class Main extends Application {
116
+
117
+ @Override
118
+
119
+ public void start(Stage primaryStage) {
120
+
121
+ TableView<DataClass> table = new TableView<DataClass>();
122
+
123
+ TableColumn<DataClass, Integer> column = new TableColumn<DataClass, Integer>("VALUE");
124
+
125
+ column.setCellValueFactory(feature -> feature.getValue().getValue().asObject());
126
+
127
+ column.setPrefWidth(200);
128
+
129
+ column.setCellFactory(tableColumn -> new TableCell<DataClass, Integer>() {
130
+
131
+ private AnchorPane pane = new AnchorPane(); //セルに画像として表示するベース
132
+
133
+ private AnchorPane barArea = new AnchorPane(); //バー領域
134
+
135
+ private Rectangle bar = new Rectangle(); //バー
136
+
137
+ private Label label = new Label(); //テキスト領域
138
+
139
+ {
140
+
141
+ //label.setPadding(new Insets(3));
142
+
143
+ barArea.getChildren().addAll(bar);
144
+
145
+ pane.getChildren().addAll(barArea, label);
146
+
147
+ AnchorPane.setRightAnchor(label, 0.0);
148
+
149
+ AnchorPane.setLeftAnchor(barArea, 0.0);
150
+
151
+ AnchorPane.setRightAnchor(barArea, 30.0); //右30はテキスト領域として固定で空けておく
152
+
153
+ }
154
+
155
+ public void updateItem(Integer value, boolean empty) {
156
+
157
+ super.updateItem(value, empty);
158
+
159
+ if (value == null || empty) {
160
+
161
+ setText(null);
162
+
163
+ } else {
164
+
165
+ bar.setWidth(barArea.getWidth() * value/30); //幅設定
166
+
167
+ column.widthProperty().addListener((self, old, val) -> { //幅の変更に合わせてデータバーも変更
168
+
169
+ bar.setWidth(barArea.getWidth() * value/30); //幅設定
170
+
171
+ });
172
+
173
+ bar.setHeight(16); //データバーの高さ
174
+
175
+ bar.setFill(Paint.valueOf("#8f8f8f8f"));
176
+
177
+ label.setText(Integer.toString(value)); //ラベルに文字列をセット
178
+
179
+ label.setStyle("-fx-alignment: center-right;"); //文字列ラベルを右寄せ
180
+
181
+ setGraphic(pane);
182
+
183
+ setText(null);
184
+
185
+ }
186
+
187
+ }
188
+
189
+ });
190
+
191
+ table.getColumns().add(column);
192
+
193
+ ObservableList<DataClass> dataClassList = FXCollections.observableArrayList();
194
+
195
+ for (int i = 0; i <= 30; i++){
196
+
197
+ DataClass data = new DataClass(i);
198
+
199
+ dataClassList.add(data);
200
+
201
+ }
202
+
203
+ table.setItems(dataClassList);
204
+
205
+ Scene scene = new Scene(table,400,400);
206
+
207
+ primaryStage.setScene(scene);
208
+
209
+ primaryStage.show();
210
+
211
+ }
212
+
213
+ public static void main(String[] args) {
214
+
215
+ launch(args);
216
+
217
+ }
218
+
219
+ }
220
+
221
+
222
+
223
+ class DataClass{ //データクラス
224
+
225
+ private IntegerProperty value = new SimpleIntegerProperty();
226
+
227
+ public DataClass(int value){
228
+
229
+ setValue(value);
230
+
231
+ }
232
+
233
+ public IntegerProperty getValue() {
234
+
235
+ return value;
236
+
237
+ }
238
+
239
+ public void setValue(int value) {
240
+
241
+ this.value.set(value);
242
+
243
+ }
244
+
245
+ }