0
我正在使用smack和javafx開發chatclient。 所以我有一個tableview列出了登錄用戶的聯繫人,它實時獲取聯繫人的狀態,但是當它是時候更新tableview時,它不會這樣做(即使很難我的system.out .println表明它應該完成它)。奇怪的是它只在第一次加載tableview時加載數據。 這裏是我的代碼:爲什麼我的tableview沒有被更新?
package login;
import java.util.Collection;
import java.util.Iterator;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;
import dto.RosterDTO;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.*;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
/** Controls the main application screen */
public class MainViewController {
@FXML
private Button logoutButton;
@FXML
private Label sessionLabel;
@FXML
private TableView<RosterDTO> rosterTable;
@FXML
private TableColumn<RosterDTO, String> grupoColumn;
@FXML
private TableColumn<RosterDTO, String> usuarioColumn;
@FXML
private TableColumn<RosterDTO, String> estatusColumn;
ObservableList<RosterDTO> rosterList;
private LoginManager loginManager;
@FXML
private void initialize() {
// Initialize the person table
grupoColumn
.setCellValueFactory(new PropertyValueFactory<RosterDTO, String>(
"grupo"));
usuarioColumn
.setCellValueFactory(new PropertyValueFactory<RosterDTO, String>(
"usuario"));
estatusColumn
.setCellValueFactory(new PropertyValueFactory<RosterDTO, String>(
"estatus"));
rosterTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
}
public void initSessionID(final LoginManager loginManager, String sessionID) {
this.loginManager = loginManager;
XMPPConnection connection = this.loginManager.getXmppManager().getConnection();
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
Presence p = roster.getPresence("testUser1");
System.out.println("Estatus: " + p.getStatus());
System.out.println("\n\n" + entries.size() + " buddy(ies):");
rosterList = FXCollections.observableArrayList();
for (RosterEntry r : entries) {
RosterDTO rosterDTO = new RosterDTO();
rosterDTO.setUsuario(r.getUser() + "/Smack");
System.out.println("user: " + r.getUser());
System.out.println(r.getName());
System.out.println("group: " + r.getGroups());
for (Iterator<?> i = r.getGroups().iterator(); i.hasNext();) {
RosterGroup rosterObject = (RosterGroup) i.next();
rosterDTO.setGrupo(rosterObject.getName());
}
rosterList.add(rosterDTO);
}
rosterTable.setItems(rosterList);
roster.addRosterListener(new RosterListener() {
public void entriesDeleted(Collection<String> addresses) {
System.out.println("deleted: " + addresses.size());
}
public void entriesUpdated(Collection<String> addresses) {
System.out.println("updated: " + addresses.size());
}
public void entriesAdded(Collection<String> addresses) {
System.out.println("added: " + addresses.size());
}
public void presenceChanged(Presence presence) {
System.out.println("Presence changed: " + presence.getFrom()
+ " " + presence + " " + presence.getStatus());
System.out.println(presence.getProperty("key"));
rosterList.get(0).setUsuario(presence.getStatus());
rosterList.get(0).setEstatus(presence.getStatus());
//rosterTable.setUserData(rosterList);
rosterTable.layout();
rosterTable.setItems(rosterList);
}
});
}
}
爲什麼加載數據的第一時間,但不得遲? 我應該使用除了tableview以外的其他東西嗎? 在此先感謝。
那麼,什麼不起作用? presenceChanged方法?您是否按照PropertyValueFactory的要求在RosterDTO中實現了usarioProperty()和estatusProperty(),以便獲取對模型對象屬性的更改? – jewelsea
不,我沒有設法解決它通過添加\t \t \t \t \t \t element.setUsuario(rosterList.get(i).getUsuario()); \t \t \t \t \t \t rosterList.set(i,element);所以我用方法集編輯了rosterlist – linker85