JavaFX中TableView使用
程序员文章站
2022-03-30 21:13:18
...
- TableView初始化代码以及在表格中使用进度条和复选框 -列表的初始化以及导入
package controlller;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.ResourceBundle;
import bean.UserRow;
import comm.DataManagement;
import comm.User;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.ProgressBarTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
@SuppressWarnings("rawtypes")
public class TableViewController extends TableView implements Initializable {
@FXML
private TableView<UserRow> qqInfoTable;
private static ObservableList<UserRow> users;
private static HashMap<String, UserRow> cache = new HashMap<>();
@Override
public void initialize(URL url, ResourceBundle source) {
// qqInfoTable.getSelectionModel().setCellSelectionEnabled(true);
dragHandler();
initTable();
ControllerManager.add("qqInfoTable", this);
}
@SuppressWarnings("unchecked")
private void initTable() {
// System.out.println(this==qqInfoTable);
String[] col_Name = { "是否完成", "线程状态", "QQ", "红包状态", "当前G分", "登陆G分", "收益", "进度" };
String[] col_Property = { "choice", "threadStatus", "qq", "packetStatus", "nowScore", "oldScore", "profit",
"progress" };
//设置复选框组件
TableColumn col = new TableColumn(col_Name[0]);
col.setEditable(true);
col.setMinWidth(115);
col.setCellValueFactory(new PropertyValueFactory<UserRow, CheckBox>(col_Property[0]));
qqInfoTable.getColumns().add(col);
for (int i = 1; i < col_Name.length; ++i) {
col = new TableColumn(col_Name[i]);
col.setMinWidth(115);
col.setCellValueFactory(new PropertyValueFactory<>(col_Property[i]));
if (i == 7) {
col.setCellFactory(ProgressBarTableCell.forTableColumn());
}
qqInfoTable.getColumns().add(col);
}
}
//在表格中实现拖放文件导入
void dragHandler() {
qqInfoTable.setOnDragOver((e)->{
e.acceptTransferModes(TransferMode.MOVE);
});
qqInfoTable.setOnDragDropped((e)->{
Dragboard board=e.getDragboard();
if (!board.hasFiles()) {
return;
}
String url=board.getFiles().get(0).getAbsolutePath();
try {
DataManagement.loadUsers(url);
} catch (IOException e1) {
ControllerManager.appendCmd(null, "加载文件错误");
}
insertIntoTable();
});
}
//把数据插入表格
void insertIntoTable() {
users = FXCollections.observableArrayList();
HashSet<User> list = DataManagement.getAllUsers();
Iterator<User> it = list.iterator();
while (it.hasNext()) {
User user = it.next();
users.add(new UserRow(user));
}
qqInfoTable.setItems(users);
}
TableView<UserRow> getTable() {
return qqInfoTable;
}
//在表格中查找项目
private UserRow find_(User user) {
UserRow now = users.stream().filter(e -> user.equals(e)).reduce((res, next) -> res = next).get();
cache.put(user.getQq(), now);
return now;
}
}
**
-TableView中使用的bean映射
**
public class User {
protected final SimpleStringProperty qq=new SimpleStringProperty("no Login");
protected final SimpleIntegerProperty oldScore=new SimpleIntegerProperty(0);
protected final SimpleIntegerProperty nowScore=new SimpleIntegerProperty(0);
protected final SimpleStringProperty nickName=new SimpleStringProperty("no Login");
protected final SimpleIntegerProperty profit=new SimpleIntegerProperty(0);
public User() {
}
protected User(User user) {
this.qq.bind(user.qq);
this.oldScore.bind(user.oldScore);
this.nowScore.bind(user.nowScore);
this.nickName.bind(user.nickName);
this.profit.bind(user.profit);
}
public String getQq() {
return qq.get();
}
public int getOldScore() {
return oldScore.get();
}
public int getNowScore() {
return nowScore.get();
}
public void setOldScore(int oldScore) {
this.oldScore.set(oldScore);
}
public void setNowScore(int nowScore) {
this.nowScore.set(nowScore);
}
@Override
public int hashCode() {
return qq.hashCode();
}
@Override
public boolean equals(Object b) {
return getQq().equals(((User)b).getQq());
}
public void computeProfit() {
setProfit(nowScore.get()-oldScore.get());
}
public int getProfit() {
return profit.get();
}
public void setProfit(int profit) {
this.profit.set(profit);
}
}
public class UserRow extends User{
private static int count=1;
private final SimpleStringProperty threadStatus=new SimpleStringProperty("no Login");
private final SimpleStringProperty packetStatus=new SimpleStringProperty("no Login");
private final SimpleStringProperty nickName=new SimpleStringProperty("no Login");
private final SimpleDoubleProperty progress=new SimpleDoubleProperty(0);
//private final SimpleBooleanProperty choice=new SimpleBooleanProperty(false);
private final CheckBox choice=new CheckBox();
private int sequence;
{
setSequence(count++);
}
public UserRow(User user){
super(user);
choice.selectedProperty().addListener(e->{
boolean selected=choice.isSelected();
if (selected) {
DataManagement.addSelected(user);
}else {
DataManagement.removeSelected(user);
}
});;
}
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
public String getThreadStatus() {
return threadStatus.get();
}
public SimpleStringProperty threadStatusProperty() {
return threadStatus;
}
public String getPacketStatus() {
return packetStatus.get();
}
public SimpleStringProperty packetStatusProperty() {
return packetStatus;
}
public SimpleIntegerProperty oldScoreProperty() {
return oldScore;
}
public String getQq() {
return qq.get();
}
public String getNickName() {
return nickName.get();
}
public double getProgress() {
return progress.get();
}
public SimpleDoubleProperty progressProperty() {
return progress;
}
public void setProgress(double progress) {
this.progress.set(progress);
}
public void setThreadStatus(String threadStatus) {
this.threadStatus.set(threadStatus);
}
public void setPacketStatus(String packetStatus) {
this.packetStatus.set(packetStatus);
}
public SimpleIntegerProperty nowScoreProperty() {
return nowScore;
}
public SimpleIntegerProperty profitProperty() {
return profit;
}
/**
* @return the choice
*/
public CheckBox getChoice() {
return choice;
}
public void allStatucSet(String status) {
threadStatus.set(status);
packetStatus.set(status);
}
}
特别需要注意类似public SimpleIntegerProperty nowScoreProperty() ;此种类型的方法”属性名+Property”,属性绑定需要getter和此类方法
-效果截图
—–
推荐阅读
-
node.js中的fs.linkSync方法使用说明_node.js
-
编写一个函数 reverse_string(char * string)(递归实现) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数。
-
WPS文字2013中使用查找功能把邮箱地址一次性全部提取出来
-
在AspNetCore中扩展Log系列 - 介绍开源类库的使用(一)
-
php中的路径问题与set_include_path使用介绍
-
mysql中内存的使用与分配_MySQL
-
js使用for循环查询数组中是否存在某个值_javascript技巧
-
编写一个函数reverse_string(char * string)(递归实现)实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数
-
Springboot使用过程中(Spring)常用注解总结
-
jquery中.add()的使用方法解析