Automatic row numbering in javafx table

I have some sample code that we use for dynamic line numbers in a Java Swing ie ie JTable . I am new to JavaFX and would like to do the same in JavaFX . Is there a way to set automatic row numbers in a JavaFX table

  class LineNumberTable extends JTable { private JTable mainTable; public LineNumberTable(JTable table) { super(); mainTable = table; setAutoCreateColumnsFromModel(false); setModel(mainTable.getModel()); setAutoscrolls(false); addColumn(new TableColumn()); getColumnModel().getColumn(0).setCellRenderer(mainTable.getTableHeader().getDefaultRenderer()); getColumnModel().getColumn(0).setPreferredWidth(40); setPreferredScrollableViewportSize(getPreferredSize()); } @Override public boolean isCellEditable(int row, int col) { if (col == uneditableColumn) { return false; } return bEdit; } @Override public Object getValueAt(int row, int column) { return Integer.valueOf(row + 1); } @Override public int getRowHeight(int row) { return mainTable.getRowHeight(); } } 
+8
java swing javafx-2
source share
1 answer

In JavaFX, you use TableColumn with CellFactories and CellValueFactories to populate a TableView .

There is an article in JavaFX tutorials that can help you get started.

In one of the approaches that I used, I convert business objects for display into presentation objects and add to them all the necessary properties (for example, in your case, a number).

EDIT: in a second, TableCell approach, you can configure CellFactory to create a TableCell , which displays its own index property in TableCell#updateItem(S, boolean) :

 public class NumberedCell extends TableCell{ protected void updateItem(Object object, boolean selected){ setText(String.valueOf(getIndex()); } } 
+5
source share

All Articles