看这篇文章之前,请先了解TableView的基本使用方法!
达到的效果:
private TableColumn<Downloader, HBox> finishprogress=new TableColumn<>();
从这里可以看出是建了一个新的列,加载到TableView的数据为一个HBox容器。
目标就是在HBox容器中加入部件,然后与TableView的列关联。
/*
写一个需要加入到TableView的容器
*/
public class PartProgress {
private ProgressBar pb;
private Label finishprogress;
private Separator se=new Separator();
public HBox hb=new HBox(); //建立一个HBox容器
/*
在HBox中放置进度条、标签和分离。
*/
PartProgress(){
hb.prefWidth(200); //设置HBox宽度
pb=new ProgressBar();
pb.progressProperty().bind(st.progressProperty()); //将进度条的属性与st(理解为一个线程,可以更新进度)绑定,实现自动更新进度
finishprogress=new Label();
finishprogress.textProperty().bind(st.valueProperty()); //为进度同进度条,绑定数据
se.setPrefWidth(10); //设置分离的宽度,为了更加美观
hb.getChildren().add(pb);
hb.getChildren().add(se);
hb.getChildren().add(finishprogress); //将部件加入到HBox中
hb.setAlignment(Pos.CENTER); //设置位置,根据实际情况
}
pulbic HBox getHbox(){return hb;}
}
/*
将类型定为HBox
*/
private TableColumn<Downloader, HBox> finishprogress=new TableColumn<>();
/*
这是用TableView的尝龟操作,注意后面的"hb",必须能够在之前那个HBox中通过Getter方法找到。
*/
finishprogress.setCellValueFactory(new PropertyValueFactory<Downloader,HBox>("hb"));
大功告成!