0

I have a TableView that shows program names (String) and times (LocalDateTime). When I open it, it is automatically sorted by time. But when I click to the new button, add a new item to the row, the new item is the first item in the table. After clicking on the column name of time, it still doesn't put it in its place (it's either the first or last item).

I'm trying to write here all the codes relating to it.

programTable = new TableView<>();
programTable.setEditable(true);

ObservableList<Program> programs = weddingScriptController.getPrograms();
SortedList<Program> sortedData = new SortedList<>(programs);
sortedData.comparatorProperty().bind(programTable.comparatorProperty());

Button newButton = new Button("Új");
newButton.setOnAction(e -> {
    ProgramCreateWindow window = new ProgramCreateWindow();
    window.display(programs);
});

//other stuff

TableColumn<Program, String> nameCol = new TableColumn<>("Név");
nameCol.setCellValueFactory(new PropertyValueFactory<Program, String>("name"));

TableColumn<Program, LocalDateTime> defaultTimeCol = new TableColumn<>("Idő");
defaultTimeCol.setCellValueFactory(new PropertyValueFactory<Program, LocalDateTime>("defaultTime"));
defaultTimeCol.setCellFactory(TextFieldTableCell.forTableColumn(new ProgramDateTimeStringConverter()));
defaultTimeCol.setSortType(TableColumn.SortType.ASCENDING);

//other stuff

programTable.setItems(sortedData);
        
programTable.getSortOrder().add(defaultTimeCol);
programTable.sort();

I've tried deleting that sorted list, and using the programs list, but it doesn't solve the problem.

programTable.setItems(programs);

I've also tried giving that function the sortedList, but it gets error when trying to add the new item to it.

window.display(sortedList);

What should I use to make this new item sorting work? I know that it may be a dumb question, but I don't get it how it works, what am I doing right, what am I doing wrong.

  • 2
    Provide a [mcve]. – jewelsea Feb 05 '23 at 15:05
  • 2
    Makery have a [tutorial on sorted lists](https://code.makery.ch/blog/javafx-8-tableview-sorting-filtering/). See also [sorting by setting a sort order on a table](https://stackoverflow.com/questions/36240142/sort-tableview-by-certain-column-javafx). – jewelsea Feb 05 '23 at 15:11
  • 3
    simply add the item to the backing list, sorting will happen automagically – kleopatra Feb 05 '23 at 17:44
  • Related examples are examined [here](https://stackoverflow.com/q/73265573/230513). – trashgod Feb 05 '23 at 19:21

0 Answers0