TableView并不总是调整列的大小 [英] TableView does not always resize the columns

查看:75
本文介绍了TableView并不总是调整列的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有CONSTRAINED_RESIZE_POLICY列调整大小策略的TableView. 当我手动调整窗口大小时,它会很好用,但是当我最大化窗口或从最大化状态还原窗口时,列不会调整.

I have a TableView with CONSTRAINED_RESIZE_POLICY column resize policy. It works great when I resize the window manually, but when I maximize it or restore it from a maximized state, the columns do not adjust.

是否有一种方法可以在TableView上强制刷新",以便在这些情况下调整列的大小?

Is there a way to force a "refresh" on the TableView so columns resize in these cases?

更新:示例可编译代码以重现该问题

UPDATE: Sample compilable code to reproduce the issue

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);
    }
}   

}

根据调整大小策略,最后一列用户"必须始终可见,并且如果您手动调整表单大小,则可以正常工作.但是,如果您尝试最大化和还原几次,您会发现它脱离了视图,直到您手动调整窗口大小为止.

According to the resize policy, the last column "Users" must be visible at all times, and if you resize the form manually it works fine. But if you try maximizing and restoring a couple of times, you will see that it comes off view, until you resize the window a bit manually.

推荐答案

我想您想用最大和最小宽度边界约束名称,用户和图标列,而主题列占用其余的可用空间.作为一种解决方法,我建议将主题列放在列的末尾,即rooms.getColumns().addAll(icons, name, users, topic);.

I suppose you want to constraint the name, users and icon columns with max and min width boundaries, while the topic column takes the rest of free space. As a workaround I suggest to put the topic column to the end of columns, i.e. rooms.getColumns().addAll(icons, name, users, topic);.

这篇关于TableView并不总是调整列的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆