如何在表视图中处理具有嵌套标题的信息?

时间:2016-04-15 22:22:43

标签: java javafx-8 scenebuilder

我想在具有嵌套标题的表视图中设置信息,就像Excel表一样。但我花了好几个小时没有得到任何可以帮助我的东西

我需要的结果是这样的:

enter image description here

但目前看起来像这样:

I can't see all information

我在表格中设置信息的代码:

JourSuivi j1=new JourSuivi(1, "30/04/2016", 23, 34, new ArrayList<>(), 43, new ArrayList<>(), 345, 23, 2, new ArrayList<>(), 11);
JourSuivi j2=new JourSuivi(1, "30/04/2016", 23, 34, new ArrayList<>(), 43, new ArrayList<>(), 345, 23, 2, new ArrayList<>(), 11);
List<Tonnage>ls=new ArrayList<>();
ls.add(new Tonnage(ls_ton,"ONCF",33));

j1.setNbWgs(ls);j2.setNbWgs(ls);
List<Double> ls_d=new ArrayList<>();
ls_d.add(12.33);ls_d.add(44.2);

j2.setTocpSA(ls_d);j1.setToncf(ls_d);j1.setToncf(ls_d);j2.setToncf(ls_d);
ObservableList<JourSuivi> data=FXCollections.observableArrayList(j1,j2);
table.getColumns().addAll(datecol, expeditionCol,numTrainCol,nWgsCol,totalcol,toncf,totaloncf,nTraincol,tocp,cumul);
controller.getHb().getChildren().add(table);
//chargement des données
table.setItems(data);

1 个答案:

答案 0 :(得分:0)

我不认为我真的理解这个问题,但这可能会有所帮助:

@FXML
TableView<String> tblView; // your table view here

TableColumn<String, String> columnMain = new TableColumn<>();
columnMain.setCellValueFactory(new PropertyValueFactory<>("someValue"));
columnMain.setText("some text");

TableColumn<String, String> columnChild1 = new TableColumn<>();
columnChild1 .setCellValueFactory(new PropertyValueFactory<>("someValue"));
columnChild1 .setText("some text");

TableColumn<String, String> columnChild2 = new TableColumn<>();
columnChild2 .setCellValueFactory(new PropertyValueFactory<>("someValue"));
columnChild2 .setText("some text");

columnMain.getColumns().add(columnChild1);
columnMain.getColumns().add(columnChild2);

// columnMain now has two child columns columnChild1 and columnChild2

tblView.getColumns().add(columnMain);

如果你想要到达columnChild1:

tblView.getColumns().get(0).getColumns().get(0);

如果你想要到达columnChild2:

tblView.getColumns().get(0).getColumns().get(1);

编辑

如果你想用数据填充表格,这对我来说是最好的方式 - &gt;用ex:

创建一个用表填充表的类

表:

@FXML
TableView<Person> tblView;

TableColumn<String, String> columnMain = new TableColumn<>();
columnMain.setText("Name");

TableColumn<String, String> columnChild1 = new TableColumn<>();
columnChild1 .setCellValueFactory(new PropertyValueFactory<>("firstName"));
columnChild1 .setText("First Name");

TableColumn<String, String> columnChild2 = new TableColumn<>();
columnChild2 .setCellValueFactory(new PropertyValueFactory<>("lastName"));
columnChild2 .setText("Last Name");

columnMain.getColumns().add(columnChild1);
columnMain.getColumns().add(columnChild2);

// columnMain now has two child columns columnChild1 and columnChild2

tblView.getColumns().add(columnMain);

和对象:

public class Person{
      String firstName;
      String lastName;
      public Person(String firstName,String lastName){
            this.firstName=firstName;
            this.lastName=lastName;
     }
}

填写表格:

ArrayList<Person> tableData = new ArrayList<>();
tableData.add(new Person("Elhassani","Ayoub"));
tableData.add(new Person("Abdullah","Asendar"));
tblView.getitems().addAll(tableData);