有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

tablecell中的java Progressbar和标签

我需要放入一个表格单元格、一个标签和进度条

对于progressbar,我使用了:

@FXML
private TableView tableView;

@FXML
private TableColumn columTabela;

@FXML
private TableColumn columSituacao;   

private List<Tabela> lista = new ArrayList<Tabela>();

public List<Tabela> getLista() {
    return lista;
}

public void setLista(List<Tabela> lista) {
    this.lista = lista;
}

   private void test() {

    getLista().add(new Tabela("test", -1.0));
    getLista().add(new Tabela("test1", null));
    columTabela.setCellValueFactory(new PropertyValueFactory<Tabela, String>("nome"));
    columSituacao.setCellValueFactory(new PropertyValueFactory<Tabela, Double>      ("progresso"));
    columSituacao.setCellFactory(ProgressBarTableCell.forTableColumn()); 
    tableView.getItems().addAll(FXCollections.observableArrayList(lista));

但现在有必要在单元格内添加一个beyond progressbar标签,无法找到解决方案

类别表:

公共类Tabela{

private String nome;

private Double progresso;

public Tabela(String nome, Double progresso) {
    this.nome = nome;
    this.progresso = progresso;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public Double getProgresso() {
    return progresso;
}

public void setProgresso(Double progresso) {
    this.progresso = progresso;
 }

}

当我的进程运行时,表的单元格中将有一个progressbar,标签将在其中更改

我感谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    您需要编写自己的单元工厂:

    Callback<TableColumn<Tabela, Double>, TableCell<Tabela, Double>> cellFactory =
        new Callback<TableColumn<Tabela, Double>, TableCell<Tabela, Double>>() {
    public TableCell call(TableColumn<Tabela, Double> p) {
        return new TableCell<Tabela, Double>() {
    
            private ProgressBar pb = new ProgressBar();
            private Text txt = new Text();
            private HBox hBox = HBoxBuilder.create().children(pb, txt).alignment(Pos.CENTER_LEFT).spacing(5).build();
            @Override
            public void updateItem(Double item, boolean empty) {
                super.updateItem(item, empty);
                if (empty) {
                    setText(null);
                    setGraphic(null);
                } else {
                    pb.setProgress(item);
                    txt.setText("value: " + item);
                    setGraphic(hBox);
                    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                }
            }
        };
    }
    };
    

    然后用它,

    columSituacao.setCellFactory(cellFactory);