2012-05-23 10 views
1

我有一個帶有CONSTRAINED_RESIZE_POLICY列大小調整策略的TableView。 當我手動調整窗口大小時,它工作的很好,但是當我最大化它或從最大化狀態恢復它時,列不會進行調整。TableView並不總是調整列的大小

有沒有辦法在TableView上強制「刷新」,以便在這些情況下調整列的大小?

UPDATE:示例編譯代碼來重現問題

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

請您附上演示代碼。我嘗試過自己,無法觀察場景。另外提供你的環境; javafx版本,os等 –

+0

剛剛添加了示例代碼,我的環境是:JavaFx 2.1,在Windows 7下運行的Netbeans 7.1.2。非常感謝您對Javafx的改進感興趣。我非常感動。 – betaman

+0

順便說一句,一種解決方法,一種「刷新」TableView的方式,直到問題得到解決,將不勝感激。 – betaman

回答

1

我想你想約束名稱,用戶和圖標列的最大和最小寬度的邊界,而主題列需要其餘的可用空間。作爲解決方法,我建議將主題列放在列末尾,即rooms.getColumns().addAll(icons, name, users, topic);

+0

謝謝,我照你說的做了,這是一個很好的解決方法。由於錯誤報告,它將不會在未來的版本中出現問題。 – betaman

相關問題