[英]Auto Updating Tableview in JAVA Fx
In my project when a client will be disconnected, server will delete the name from observable list and the tableview should stop showing the name. 在我的项目中,当客户端断开连接时,服务器将从可观察列表中删除该名称,并且tableview应该停止显示该名称。 But the tableview is not updating.
但是tableview没有更新。 Controller class
控制器类
public class Controller {
@FXML
public TableView tableView;
@FXML
private TableColumn<clientLoginData,String> client;
@FXML
private TableColumn<clientLoginData,String> activeTime;
void initialize(ObservableList<clientLoginData> data)
{
client.setCellValueFactory(new PropertyValueFactory<>("clientName"));
client.setCellFactory(TextFieldTableCell.<clientLoginData>forTableColumn());
activeTime.setCellValueFactory(new PropertyValueFactory<>("time"));
activeTime.setCellFactory(TextFieldTableCell.<clientLoginData>forTableColumn());
tableView.setItems(data);
tableView.setEditable(true);
}
}
main class 主班
public class Main extends Application{
volatile public ObservableList<clientLoginData> data= FXCollections.observableArrayList();
public Controller controller;
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("server.fxml"));
Parent root = loader.load();
data.addAll(new clientLoginData((new SimpleStringProperty("john")),new SimpleStringProperty(ZonedDateTime.now().getHour()+":"+ZonedDateTime.now().getMinute())));
controller=loader.getController();
controller.initialize(data);
primaryStage.setTitle("Server");
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
Thread t=new Thread(new messengerServer(this));
t.start();
}
public static void main(String[] args) {
launch(args);
}
}
updating class 更新类
public class messengerReadThread implements Runnable {
private Thread thr;
private NetworkUtil nc;
public Hashtable<SimpleStringProperty, NetworkUtil> table;
SimpleStringProperty oldName;
Main main;
public messengerReadThread(NetworkUtil nc, Hashtable<SimpleStringProperty, NetworkUtil> table, SimpleStringProperty s, Main main) {
this.nc = nc;
this.thr = new Thread(this);
thr.start();
this.table=table;
oldName=s;
this.main=main;
}
public void run() {
try {
while(true) {
String s1=(String)nc.read();
StringTokenizer st=new StringTokenizer(s1);
if(st.nextToken().equals("Name"))
{
String sn=s1.substring(5,s1.length());
NetworkUtil n1=table.get(oldName);
table.remove(oldName);
oldName=new SimpleStringProperty(sn);
table.put(oldName, n1);
main.data.add(new clientLoginData(oldName,new SimpleStringProperty(ZonedDateTime.now().getHour()+":"+ZonedDateTime.now().getMinute())));
}
else
{
System.out.println("here it is"+s1);
}
}
} catch(Exception e) {
System.out.println("disconnected "+oldName.toString());
main.data.remove(oldName);
//System.out.println(main.data.contains(oldName));
main.controller.tableView.refresh();//the tableview should update
}
nc.closeConnection();
}
}
There are some modification I should to to that code, like avoid using those "static references", by defining the ObservableList and move your Updating Code inside Controller so you can have a 2 classes code, the Main Class and your Controller... but i'll try to keep it simple. 我应该对该代码进行一些修改,例如避免使用那些“静态引用”,方法是定义ObservableList并在Controller内移动您的Updating代码,这样您就可以拥有2类代码,即Main Class和Controller ...但是我会尽量保持简单。
First, you need to define the ObservableList inside you controller. 首先,您需要在控制器内部定义ObservableList。
Then place your "updating" code inside the controller in a method. 然后将您的“更新”代码放入一个方法中的控制器中。 I suggest you to use a Task<> to keep your controller updated in the JavaFX Thread.
我建议您使用Task <>使JavaFX Thread中的控制器保持更新。
Try something like this: 尝试这样的事情:
private void updateTable(){
Task<Void> myUpdatingTask=new Task<Void>() {
@Override
protected Void call() throws Exception {
//Your Updating Code Here
}
}
//and then you run it like this:
Thread hilo=new Thread(myUpdatingTask);
hilo.setDaemon(true);
hilo.start();
}
Then, remove the parameter from your Initialize Method and define it private with the @FXML annotation like this: 然后,从初始化方法中删除参数,并使用@FXML注释将其定义为私有,如下所示:
@FXML
private void initialize(){
//Your Stuff to initialize
//here is were you fill your table like you did in the Main
//and don't forget to call you updateTable Method
this.updateTable();
}
Since this a dirty hack as pointed out by @kleopatra I am going to decorate it as a dirty hack. 由于这是@kleopatra指出的肮脏技巧,因此我将其装饰为肮脏技巧。 ****************************Dirty Hack***************************************** Try hiding a column and displaying it again and your tableview should refresh
****************************肮脏的黑客******************** *********************尝试隐藏一列并再次显示它,您的tableview应该刷新
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.