我有一个带有CONSTRAINED_RESIZE_POLICY列调整大小策略的TableView。 当我手动调整窗口大小时,它工作得很好,但是当我最大化它或从最大化状态恢复它时,列不会调整。
有没有办法在TableView上强制“刷新”,以便在这些情况下调整列的大小?
更新:重现问题的可编辑代码示例
public class TableViewResizeTest extends Application {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("TableView resize demo");
ObservableList<Room> roomsList = FXCollections.observableArrayList();
final TableView rooms = new TableView();
TableColumn icons = new TableColumn();
TableColumn name = new TableColumn("Name");
TableColumn topic = new TableColumn("Topic");
TableColumn users = new TableColumn("Users");
rooms.getColumns().addAll(icons, name, topic, users);
name.setCellValueFactory( new PropertyValueFactory<Room,String>("name"));
topic.setCellValueFactory( new PropertyValueFactory<Room,String>("topic"));
users.setCellValueFactory( new PropertyValueFactory<Room,String>("users"));
icons.setCellValueFactory( new PropertyValueFactory<Room,String>("icons"));
name.setMinWidth(50);
name.setMaxWidth(450);
topic.setMinWidth(10);
users.setMinWidth(100);
users.setMaxWidth(150);
icons.setMaxWidth(35);
icons.setMinWidth(35);
// Room resize policy, this is almost perfect
rooms.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
for (int i = 1; i<50; i++) roomsList.add(new Room("Sample Room "+i));
rooms.setItems(roomsList);
BorderPane root = new BorderPane();
root.setCenter(rooms);
primaryStage.setScene(new Scene(root, 500, 460));
primaryStage.show();
}
public class Room {
public Room(String name) {
this.name = new SimpleStringProperty(name);
this.topic= new SimpleStringProperty("This is a sample description text");
this.icon= new SimpleStringProperty("");
nUsers = (int)(Math.random()*1000);
this.users= new SimpleStringProperty(nUsers.toString());
}
Integer nUsers;
private SimpleStringProperty name;
private SimpleStringProperty topic;
private SimpleStringProperty users;
private SimpleStringProperty icon;
public String getName() {
return name.get();
}
public void setName(String name) {
this.name.set(name);
}
public String getTopic() {
return topic.get();
}
public void setTopic(String topic) {
this.topic.set(topic);
}
public String getUsers() {
return nUsers.toString();
}
public void setUsers(String users) {
this.users.set(users);
}
public String getIcon() {
return icon.get();
}
public void setIcon(String icon) {
this.icon.set(icon);
}
}
}
根据调整大小政策,最后一列“用户”必须始终可见,如果您手动调整表单大小,则可以正常工作。但是如果你尝试最大化和恢复几次,你会看到它在视图之外,直到你手动调整窗口大小。
答案 0 :(得分:1)
我想你要约束具有最大和最小宽度边界的名称,用户和图标列,而主题列占用剩余的可用空间。作为一种解决方法,我建议将主题列放在列的末尾,即rooms.getColumns().addAll(icons, name, users, topic);
。