Search code examples
javafxtableviewjavafx-8fxml

How can i add serial no to my current list which i need to add observable list in javafx so that displays in table view


This is my fxml file code which displays some search criteria and then table view

<Pane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.medplus.posoffline.gui.ReturnsPickListController">
   <children>
      <Pane prefHeight="600.0" prefWidth="800.0">
         <children>
            <Label layoutX="5.0" layoutY="15.0" text="RequestId" />
            <TextField fx:id="requestId" layoutX="75.0" layoutY="15.0" prefWidth="140.0" />
            <Label layoutX="245.0" layoutY="15.0" text="Status" />
            <ComboBox fx:id="returnStatusComb" layoutX="295.0" layoutY="15.0" prefWidth="140.0">
            </ComboBox>
            <Label layoutX="470.0" layoutY="15.0" text="Type" />
            <ComboBox fx:id="returnTypeComb" layoutX="510.0" layoutY="15.0" prefWidth="140.0">
            </ComboBox>
            <Button layoutX="598.0" layoutY="60.0" mnemonicParsing="false" onAction="#searcReturnDetailsfroPickList" text="Search" />
            <TableView fx:id="tableView" layoutY="92.0" prefHeight="500.0" prefWidth="800.0">
              <columns>
                <TableColumn fx:id="requestIds" minWidth="160.0" prefWidth="75.0" text="ReqId" />
                <TableColumn fx:id="status" minWidth="160.0" prefWidth="75.0" text="Status" />
                  <TableColumn fx:id="type" minWidth="160.0" prefWidth="75.0" text="Type" />
                  <TableColumn fx:id="dateCreated" minWidth="160.0" prefWidth="75.0" text="DateCreated" />
                  <TableColumn fx:id="showDetailsBtn" minWidth="160.0" prefWidth="75.0" text="Action" />
              </columns>
            </TableView>
         </children>
      </Pane>
   </children>
</Pane>
if(UtilValidate.isNotEmpty(returnRequests)) {
    observableProductList.addAll(returnRequests);
    requestIds.setCellValueFactory(new PropertyValueFactory<ReturnRequest, Long>("requestId"));
    status.setCellValueFactory(new PropertyValueFactory<ReturnRequest, ReturnRequestType>("status"));
    type.setCellValueFactory(new PropertyValueFactory<ReturnRequest, ReturnRequestStatus>("type"));
    dateCreated.setCellValueFactory(new PropertyValueFactory<ReturnRequest, Date>("dateCreated"));

    tableView.setItems(observableProductList);
}else {

    alert.setTitle("Info");
    alert.setContentText("No Data found");

    alert.showAndWait();

}
clearData();

I want to add serial numbers to list in order to show how many records got displayed in my table.

Now I'm able to display just data in table perfectly I just to know how many records got display by showing serial number does any method in javafx can display according to their index in list?

or any other alternative solution let me know please

Thanks in advance


Solution

  • If you simply want to display the index in the items list of the TableView in a TableCell, I recommend using a custom TableCell implementation for this purpose:

    public static <T> Callback<TableColumn<T, Void>, TableCell<T, Void>> indexCellFactory() {
        return t -> new TableCell<T, Void>() {
    
            @Override
            public void updateIndex(int i) {
                super.updateIndex(i);
                setText(isEmpty() ? "" : Integer.toString(i));
            }
    
        };
    }
    
    TableColumn<MyItem, Void> indexColumn = new TableColumn<>("Row index");
    indexColumn.setCellFactory(indexCellFactory());