-
-
Save LukeLR/a2806cb26f7c7bef3a20e0eec14e69c6 to your computer and use it in GitHub Desktop.
JavaFX TableView sorting problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package meta; | |
import java.io.IOException; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
import java.io.ObjectStreamException; | |
import java.io.Serializable; | |
import java.time.Duration; | |
import java.time.Instant; | |
import java.time.LocalDateTime; | |
import java.time.ZoneId; | |
import java.time.format.DateTimeFormatter; | |
import java.time.temporal.TemporalField; | |
import javafx.beans.property.SimpleIntegerProperty; | |
import javafx.beans.property.SimpleLongProperty; | |
import javafx.beans.property.SimpleObjectProperty; | |
import javafx.beans.property.SimpleStringProperty; | |
public class HighscoreEntry { | |
private SimpleStringProperty name; | |
private SimpleIntegerProperty number, moves, fieldWidth, fieldHeight, mines; | |
private SimpleObjectProperty<LocalDateTime> startTime; | |
private SimpleObjectProperty<Duration> duration; | |
public HighscoreEntry(int number, String name, LocalDateTime startTime, Duration duration, | |
int moves, int fieldWidth, int fieldHeight, int mines){ | |
this.number = new SimpleIntegerProperty (number); | |
this.name = new SimpleStringProperty (name); | |
this.startTime = new SimpleObjectProperty<LocalDateTime> (startTime); | |
this.duration = new SimpleObjectProperty<Duration> (duration); | |
this.moves = new SimpleIntegerProperty (moves); | |
this.fieldWidth = new SimpleIntegerProperty (fieldWidth); | |
this.fieldHeight = new SimpleIntegerProperty (fieldHeight); | |
this.mines = new SimpleIntegerProperty (mines); | |
} | |
public HighscoreEntry(String name, LocalDateTime startTime, Duration duration, | |
int moves, int fieldWidth, int fieldHeight, int mines){ | |
this(0, name, startTime, duration, moves, fieldWidth, fieldHeight, mines); | |
} | |
public HighscoreEntry(int number, String name, LocalDateTime startTime, Duration duration, int moves){ | |
this(number, name, startTime, duration, moves, 0, 0, 0); | |
} | |
public HighscoreEntry(String name, LocalDateTime startTime, Duration duration, int moves){ | |
this(0, name, startTime, duration, moves, 0, 0, 0); | |
} | |
public int getNumber(){ | |
return number.get(); | |
} | |
public void setNumber(int number){ | |
this.number.setValue(number); | |
} | |
public String getName(){ | |
return name.get(); | |
} | |
public void setName(String name){ | |
this.name.setValue(name); | |
} | |
public String getStartTime(){ | |
return startTime.getValue().format(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm")); | |
} | |
public void setStartTime(String startTime){ | |
//TODO: Is it needed? | |
} | |
public String getDuration(){ | |
return String.format("%d:%02d.%03d", duration.getValue().getSeconds() / 60, (duration.getValue().getSeconds() % 60), duration.getValue().getNano() / 1000000); | |
} | |
public void setDuration(String duration){ | |
//TODO: Is it needed? | |
} | |
public int getMoves(){ | |
return moves.get(); | |
} | |
public void setMoves(int moves){ | |
this.moves.setValue(moves); | |
} | |
public int getFieldWidth(){ | |
return fieldWidth.get(); | |
} | |
public void setFieldWidth(int fieldWidth){ | |
this.fieldWidth.setValue(fieldWidth); | |
} | |
public int getFieldHeight(){ | |
return fieldHeight.get(); | |
} | |
public void setFieldHeight(int fieldHeight){ | |
this.fieldHeight.setValue(fieldHeight); | |
} | |
public int getMines(){ | |
return mines.get(); | |
} | |
public void setMines(int mines){ | |
this.mines.setValue(mines); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gui; | |
import java.time.LocalDateTime; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.geometry.Insets; | |
import javafx.geometry.Pos; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Tab; | |
import javafx.scene.control.TabPane; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.scene.layout.AnchorPane; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.GridPane; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.Priority; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.text.Font; | |
import javafx.scene.text.Text; | |
import javafx.stage.Stage; | |
import meta.Data; | |
import meta.DataManager; | |
import meta.HighscoreEntry; | |
import meta.HighscoreList; | |
public class HighscoreStage extends Stage { | |
private HighscoreTableView easyTable; | |
private HighscoreTableView intermediateTable; | |
private HighscoreTableView hardTable; | |
private HighscoreTableView customTable; | |
private Tab easyTab; | |
private Tab intermediateTab; | |
private Tab hardTab; | |
private Tab customTab; | |
public HighscoreStage(){ | |
super.setTitle("Highscore"); | |
super.setScene(createScene()); | |
super.showAndWait(); | |
} | |
private Scene createScene(){ | |
Scene scene = new Scene(createRoot(), 500, 500); | |
return scene; | |
} | |
private BorderPane createRoot(){ | |
BorderPane borderPane = new BorderPane(); | |
// borderPane.setPadding(new Insets(10, 10, 10, 10)); | |
borderPane.setCenter(createCenter()); | |
borderPane.setBottom(createBottom()); | |
return borderPane; | |
} | |
private GridPane createCenter(){ | |
GridPane gridPane = new GridPane(); | |
gridPane.setVgap(10d); | |
gridPane.setHgap(10d); | |
Text title = new Text("Highscore tables:"); | |
title.setFont(Font.font("Verdana", 26)); | |
gridPane.add(title, 0, 0); | |
gridPane.setMargin(title, new Insets(10, 10, 10, 10)); | |
TabPane tabPane = createTabPane(); | |
gridPane.add(tabPane, 0, 1); | |
gridPane.setHgrow(tabPane, Priority.ALWAYS); | |
gridPane.setVgrow(tabPane, Priority.ALWAYS); | |
return gridPane; | |
} | |
private TabPane createTabPane(){ | |
TabPane tabPane = new TabPane(); | |
easyTab = new Tab("Easy"); | |
intermediateTab = new Tab("Intermediate"); | |
hardTab = new Tab("Hard"); | |
customTab = new Tab("Custom"); | |
easyTab.setClosable(false); | |
intermediateTab.setClosable(false); | |
hardTab.setClosable(false); | |
customTab.setClosable(false); | |
tabPane.getTabs().addAll(easyTab, intermediateTab, hardTab, customTab); | |
easyTable = new HighscoreTableView(false); | |
intermediateTable = new HighscoreTableView(false); | |
hardTable = new HighscoreTableView(false); | |
customTable = new HighscoreTableView(true); | |
easyTab.setContent(easyTable); | |
intermediateTab.setContent(intermediateTable); | |
hardTab.setContent(hardTable); | |
customTab.setContent(customTable); | |
loadData(); | |
return tabPane; | |
} | |
private void loadData(){ | |
easyTable.fillWithData(DataManager.getData().getHighscoresEasy()); | |
intermediateTable.fillWithData(DataManager.getData().getHighscoresIntermediate()); | |
hardTable.fillWithData(DataManager.getData().getHighscoresHard()); | |
customTable.fillWithData(DataManager.getData().getHighscoresCustom()); | |
} | |
private HBox createBottom(){ | |
HBox hBox = new HBox(); | |
hBox.setAlignment(Pos.CENTER_RIGHT); | |
hBox.setPadding(new Insets(10, 10, 10, 10)); | |
Button ok = new Button("OK"); | |
hBox.getChildren().add(ok); | |
ok.setOnAction((event) -> { | |
close(); | |
}); | |
Button reset = new Button("Reset Highscores"); | |
hBox.getChildren().add(reset); | |
reset.setOnAction((event) -> { | |
DataManager.getData().newHighscores(); | |
loadData(); | |
}); | |
return hBox; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*/ | |
package gui; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableColumn.SortType; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import meta.HighscoreEntry; | |
import meta.HighscoreList; | |
public class HighscoreTableView extends TableView<HighscoreEntry> { | |
private HighscoreList list; | |
private boolean isCustomHighscoreTable = false; | |
private TableColumn<HighscoreEntry, Integer> number, moves, fieldWidth, fieldHeight, mines; | |
private TableColumn<HighscoreEntry, String> name, startTime, duration; | |
public HighscoreTableView(){ | |
this(null); | |
} | |
public HighscoreTableView(HighscoreList list, boolean isCustomHighscoreTable){ | |
super(); | |
this.list = list; | |
this.isCustomHighscoreTable = isCustomHighscoreTable; | |
this.initColumns(); | |
} | |
public HighscoreTableView(HighscoreList list){ | |
this(list, false); | |
} | |
public HighscoreTableView(boolean isCustomHighscoreTable){ | |
this(null, isCustomHighscoreTable); | |
} | |
private void initColumns(){ | |
number = new TableColumn<HighscoreEntry, Integer>("#"); | |
name = new TableColumn<HighscoreEntry, String>("Name:"); | |
startTime = new TableColumn<HighscoreEntry, String>("Start time:"); | |
duration = new TableColumn<HighscoreEntry, String>("Duration:"); | |
moves = new TableColumn<HighscoreEntry, Integer>("Moves:"); | |
this.getColumns().addAll(number, name, startTime, duration, moves); | |
if (this.isCustomHighscoreTable){ | |
fieldWidth = new TableColumn<HighscoreEntry, Integer>("Field width:"); | |
fieldHeight = new TableColumn<HighscoreEntry, Integer>("Field heighth:"); | |
mines = new TableColumn<HighscoreEntry, Integer> ("Mines:"); | |
this.getColumns().addAll(fieldWidth, fieldHeight, mines); | |
} | |
number.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, Integer>("number")); | |
name.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, String>("name")); | |
startTime.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, String>("startTime")); | |
duration.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, String>("duration")); | |
moves.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, Integer>("moves")); | |
if (this.isCustomHighscoreTable){ | |
fieldWidth.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, Integer>("fieldWidth")); | |
fieldHeight.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, Integer>("fieldHeight")); | |
mines.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, Integer>("mines")); | |
} | |
duration.setSortType(TableColumn.SortType.DESCENDING); | |
this.getSortOrder().add(duration); | |
duration.setSortable(true); | |
this.sort(); | |
} | |
public void fillWithData(){ | |
// if (list == null){}//TODO: Fehlermeldung! | |
// else { | |
// for (HighscoreEntry i : list){ | |
// number.cellValueFactoryProperty().set | |
// } | |
// } | |
this.setItems(list.getObservableList()); | |
} | |
public void fillWithData(HighscoreList list){ | |
this.list = list; | |
fillWithData(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment