首页 归档 关于 文件 Github
×

JavaFx之动态添加组件

2021-03-03 10:24:50
JavaFx
  • VBox
本文总阅读量(次):
本文字数统计(字):1.4k
本文阅读时长(分):7

本例主要是构建动态模板,采用VBox模式,
效果如下:
20210303103350
按钮操作 - 添加栏位
20210303103536
按钮操作 - 上移
20210303103643
按钮操作 - 下移
20210303103713

按钮添加鼠标事件setOnMouseClicked

1
2
3
4
5
6
Button addBtn = new Button("添加栏位");
addBtn.setOnMouseClicked(addBtnMouseClicked());
Button upBtn = new Button("上移");
upBtn.setOnMouseClicked(upBtnMouseClicked());
Button downBtn = new Button("下移");
downBtn.setOnMouseClicked(downBtnMouseClicked());

将内容面板content添加到滚动面板

1
2
3
4
ScrollPane scrollPane = new ScrollPane(content);
scrollPane.setPannable(true);
scrollPane.setStyle("-fx-background-color: #FFF;");
BorderPane.setAlignment(scrollPane,Pos.CENTER);

寻找面板上的组件,只有类型对应,强转也无报错

1
2
3
4
5
6
7
ObservableList<Node> templateAttrChildren = content.getChildren();
if (templateAttrChildren != null && templateAttrChildren.size() > 0) {
for (Node node : templateAttrChildren) {
CheckBox attrFieldCheckbox = (CheckBox) node.lookup("#attrFieldCheckbox");
System.out.println(attrFieldCheckbox.getUserData());
}
}

移动操作:先获取当前组件对象,通过原始位置index进行remove,在进行位置add

1
2
3
4
5
6
7
8
9
10
11
12
13
14
上移:当移到最上层时index=1
HBox hBox = (HBox) content.lookup("#" + checkBox.getParent().getId());
int index = content.getChildren().indexOf(hBox);
if (index > 0) {
content.getChildren().remove(index);
content.getChildren().add(index - 1, hBox);
}
下移:当移到最下层时index=最大值
HBox hBox = (HBox) content.lookup("#" + checkBox.getParent().getId());
int index = content.getChildren().indexOf(hBox);
if (index < content.getChildren().size() - 1) {
content.getChildren().remove(index);
content.getChildren().add(index + 1, hBox);
}

事件EventHandler

1
2
3
4
5
private EventHandler<MouseEvent> btnMouseClicked(){
return event -> {
System.out.println("事件执行");
};
}

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package sample;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
* @ClassName: VBox动态添加组件
* @Description:
* @Author: Demon
* @Date: 2021/3/2 16:16
*/
public class 动态添加组件 extends Application {

private static VBox content = new VBox();
private static String[] attrType = new String[]{"常量", "变量"};

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
// Center - 动态内容
content.setAlignment(Pos.TOP_CENTER);
content.setSpacing(3);
// 加载初始数据
getContentHbox(new AttributeVo().setAttrId("123456").setAttrName("测试1").setAttrType(0).setAttrValue("123"));
getContentHbox(new AttributeVo().setAttrId("345678").setAttrName("测试2").setAttrType(1).setAttrValue("321"));

// 初始化
BorderPane borderPane = new BorderPane();
borderPane.setPadding(new Insets(15,8,15,8));
// Top
VBox topVbox = new VBox();
Label title = new Label("栏位列表");
topVbox.getChildren().add(title);
borderPane.setTop(topVbox);
// Center
borderPane.setCenter(getCenterPane());
// Bottom
HBox bottomHbox = new HBox();
bottomHbox.setAlignment(Pos.CENTER);
Button saveBtn = new Button("保存");
saveBtn.setOnMouseClicked(saveBtnMouseClicked());
bottomHbox.getChildren().add(saveBtn);
borderPane.setBottom(bottomHbox);
// 将内容添加到主面板
StackPane root = new StackPane();
root.getChildren().add(borderPane);
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.setResizable(false);
primaryStage.show();
}


private BorderPane getCenterPane(){
BorderPane borderPane = new BorderPane();
// Top - 按钮
HBox topBox = new HBox();
topBox.setAlignment(Pos.CENTER_RIGHT);
topBox.setPadding(new Insets(0,5,5,0));
topBox.setSpacing(10);
Button addBtn = new Button("添加栏位");
addBtn.setOnMouseClicked(addBtnMouseClicked());
Button upBtn = new Button("上移");
upBtn.setOnMouseClicked(upBtnMouseClicked());
Button downBtn = new Button("下移");
downBtn.setOnMouseClicked(downBtnMouseClicked());
topBox.getChildren().addAll(addBtn,upBtn,downBtn);
borderPane.setTop(topBox);
// Center - 表格内容
borderPane.setCenter(getContent());
return borderPane;
}

private BorderPane getContent(){
System.out.println("添加初始化内容");
BorderPane borderPane = new BorderPane();
// Top - 栏位列表
HBox hBox = new HBox();
hBox.setAlignment(Pos.CENTER_LEFT);
hBox.setPrefHeight(30.0);
hBox.setSpacing(5);
Label label1 = new Label("栏位名称");
label1.setPrefWidth(220);
label1.setAlignment(Pos.CENTER);
Label label2 = new Label("栏位类型");
label2.setPrefWidth(100);
label2.setAlignment(Pos.CENTER);
Label label3 = new Label("栏位数值");
label3.setPrefWidth(350);
label3.setAlignment(Pos.CENTER);
hBox.getChildren().addAll(label1,label2,label3);
borderPane.setTop(hBox);
// 将内容添加到滚动面板
ScrollPane scrollPane = new ScrollPane(content);
scrollPane.setPannable(true);
scrollPane.setStyle("-fx-background-color: #FFF;");
BorderPane.setAlignment(scrollPane,Pos.CENTER);

borderPane.setCenter(scrollPane);
return borderPane;
}

/**
* 按钮事件 - 添加栏位
*/
private EventHandler<MouseEvent> addBtnMouseClicked(){
return event -> {
//添加栏位
getContentHbox(new AttributeVo().setAttrId(UUID.randomUUID().toString()).setAttrType(0));
};
}

/**
* 动态区域操作
* @param attributeVo 参数
*/
private void getContentHbox(AttributeVo attributeVo){
System.out.println("添加ID:"+attributeVo.getAttrId());
HBox hBox = new HBox(5);
hBox.setId(UUID.randomUUID().toString());
hBox.setAlignment(Pos.CENTER_LEFT);
//栏位选取
CheckBox attrFieldCheckbox = new CheckBox();
attrFieldCheckbox.setId("attrFieldCheckbox");
attrFieldCheckbox.setUserData(attributeVo.getAttrId());
attrFieldCheckbox.setMaxWidth(30);
attrFieldCheckbox.setOnAction(event -> {
if (attrFieldCheckbox.isSelected()) {
ObservableList<Node> contentChildren = content.getChildren();
if (contentChildren != null && contentChildren.size() > 0) {
for (Node node : contentChildren) {
CheckBox checkBox = (CheckBox) node.lookup("#attrFieldCheckbox");
checkBox.setSelected(false);
}
}
attrFieldCheckbox.setSelected(true);
}
});
//栏位名称
TextField attrName = new TextField(attributeVo.getAttrName());
attrName.setId("attrName");
attrName.setPrefWidth(200);
//栏位类型
ComboBox attrTypeComboBox = new ComboBox();
attrTypeComboBox.setId("attrTypeComboBox");
attrTypeComboBox.setPrefWidth(100);
attrTypeComboBox.setItems(FXCollections.observableArrayList(attrType));
attrTypeComboBox.getSelectionModel().select(attributeVo.getAttrType());
//栏位数组
TextField attrValue = new TextField(attributeVo.getAttrValue());
attrValue.setId("attrValue");
attrValue.setPrefWidth(350);
//操作按钮
Button delBtn = new Button("删除");
delBtn.setMaxWidth(50);
delBtn.setOnMouseClicked(event1 -> content.getChildren().removeAll(hBox));
// 添加组件到hBox
hBox.getChildren().addAll(attrFieldCheckbox,attrName,attrTypeComboBox,attrValue,delBtn);
// 将hBox添加到内容面板
content.getChildren().add(content.getChildren().size(), hBox);
}

/**
* 按钮事件 - 上移栏位
*/
private EventHandler<MouseEvent> upBtnMouseClicked(){
return event -> {
ObservableList<Node> contentChildren = content.getChildren();
if (contentChildren != null && contentChildren.size() > 0) {
for (Node node : contentChildren) {
CheckBox checkBox = (CheckBox) node.lookup("#attrFieldCheckbox");
if (checkBox.isSelected()) {
HBox hBox = (HBox) content.lookup("#" + checkBox.getParent().getId());
int index = content.getChildren().indexOf(hBox);
if (index > 0) {
content.getChildren().remove(index);
content.getChildren().add(index - 1, hBox);
}
break;
}
}
}
};
}

/**
* 按钮事件 - 下移栏位
*/
private EventHandler<MouseEvent> downBtnMouseClicked(){
return event -> {
ObservableList<Node> contentChildren = content.getChildren();
if (contentChildren != null && contentChildren.size() > 0) {
for (Node node : contentChildren) {
CheckBox checkBox = (CheckBox) node.lookup("#attrFieldCheckbox");
if (checkBox.isSelected()) {
HBox hBox = (HBox) content.lookup("#" + checkBox.getParent().getId());
int index = content.getChildren().indexOf(hBox);
if (index < content.getChildren().size() - 1) {
content.getChildren().remove(index);
content.getChildren().add(index + 1, hBox);
}
break;
}
}
}
};
}

/**
* 按钮事件 - 保存栏位
*/
private EventHandler<MouseEvent> saveBtnMouseClicked(){
return event -> {
List<AttributeVo> attributeVoList = new ArrayList<>();
//模板栏位
ObservableList<Node> contentChildren = content.getChildren();
if (contentChildren != null && contentChildren.size() > 0) {
for (Node node : contentChildren) {
// 获取值
CheckBox attrFieldCheckbox = (CheckBox) node.lookup("#attrFieldCheckbox");
TextField attrName = (TextField) node.lookup("#attrName");
ComboBox attrTypeComboBox = (ComboBox) node.lookup("#attrTypeComboBox");
TextField attrValue = (TextField) node.lookup("#attrValue");
// 赋值
AttributeVo attributeVo = new AttributeVo();
attributeVo.setAttrId(String.valueOf(attrFieldCheckbox.getUserData()));
attributeVo.setAttrName(attrName.getText());
attributeVo.setAttrType(attrTypeComboBox.getSelectionModel().getSelectedIndex());
attributeVo.setAttrValue(attrValue.getText());
attributeVoList.add(attributeVo);
}
}
System.out.println(attributeVoList);
};
}

}

/**
* 虚拟实体类
*/
class AttributeVo {
private String attrId;
private String attrName;
private int attrType;
private String attrValue;

public String getAttrId() {
return attrId;
}

public AttributeVo setAttrId(String attrId) {
this.attrId = attrId;
return this;
}

public String getAttrName() {
return attrName;
}

public AttributeVo setAttrName(String attrName) {
this.attrName = attrName;
return this;
}

public int getAttrType() {
return attrType;
}

public AttributeVo setAttrType(int attrType) {
this.attrType = attrType;
return this;
}

public String getAttrValue() {
return attrValue;
}

public AttributeVo setAttrValue(String attrValue) {
this.attrValue = attrValue;
return this;
}

@Override
public String toString() {
return "AttributeVo{" +
"attrId='" + attrId + '\'' +
", attrName='" + attrName + '\'' +
", attrType=" + attrType +
", attrValue='" + attrValue + '\'' +
'}';
}
}
完
JavaFx之CheckBox变成单选模式
JavaFx之TreeView的事件

本文标题:JavaFx之动态添加组件

文章作者:十二

发布时间:2021-03-03 10:24:50

最后更新:2021-03-03 11:10:12

原始链接:https://www.zhuqiaolun.com/2021/03/1614738290477/1614738290477/

许可协议:署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

头像

十二

我想起那天夕阳下的奔跑,那是我逝去的青春。

分类

  • Blog4
  • ElasticSearch13
  • Git2
  • Go-FastDfs2
  • IDEA2
  • J-Package6
  • J-Tools21
  • Java2
  • JavaFx6
  • Kafka4
  • Linux2
  • Logger6
  • Maven5
  • MyBatis6
  • MyCat3
  • MySql2
  • Nginx5
  • OceanBase1
  • RabbitMq4
  • Redis6
  • SVN1
  • SpringBoot16
  • Tomcat6
  • WebService2
  • Windows2
  • kubernetes10

归档

  • 一月 20261
  • 十二月 20253
  • 八月 20252
  • 六月 20251
  • 二月 20251
  • 十二月 20244
  • 八月 202416
  • 六月 20241
  • 九月 20231
  • 八月 20231
  • 七月 20232
  • 八月 20222
  • 三月 202214
  • 二月 20224
  • 十一月 20211
  • 七月 20215
  • 六月 20213
  • 五月 20213
  • 四月 20211
  • 三月 202116
  • 二月 20212
  • 一月 20211
  • 十一月 202014
  • 十月 20201
  • 九月 202014
  • 八月 20205
  • 七月 20204
  • 六月 20208
  • 五月 20208

作品

我的微信 我的文件

网站信息

本站运行时间统计: 载入中...
本站文章字数统计:103.2k
本站文章数量统计:139
© 2026 十二  |  鄂ICP备18019781号-1  |  鄂公网安备42118202000044号
驱动于 Hexo  | 主题 antiquity  |  不蒜子告之 阁下是第个访客
首页 归档 关于 文件 Github