Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic row numbering in javafx table

I have a sample code that we use to dynamic row numbers in Java Swing Table i.e JTable. I new to JavaFX and would like to the same in JavaFX. Is there is any way to set automatic row numbers in 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();
            }
        }
like image 484
Ashish Pancholi Avatar asked Nov 19 '12 07:11

Ashish Pancholi


1 Answers

In JavaFX, you use TableColumns with CellFactories and CellValueFactories to populate your TableView.

The JavaFX tutorials have an article that might get you started.

In one approach I have used I convert the business objects to display into presentation objects and add all necessary properties (like in your case, the number) to them.

EDIT: In a second, cleaner approach, you could set your CellFactory to create a TableCell that shows 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());
  }
}
like image 153
Urs Reupke Avatar answered Oct 28 '22 11:10

Urs Reupke