Skip to content

Instantly share code, notes, and snippets.

@LukeLR
Last active June 24, 2016 21:49
Show Gist options
  • Save LukeLR/d404dfceace256d514f2178396f016ed to your computer and use it in GitHub Desktop.
Save LukeLR/d404dfceace256d514f2178396f016ed to your computer and use it in GitHub Desktop.
javafx TableView highscore problem
package meta;
import java.io.Serializable;
import gui.MainWindow;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.beans.value.ObservableDoubleValue;
public class Data implements Serializable {
public static final int EASY = 0;
public static final int INTERMEDIATE = 1;
public static final int HARD = 2;
public static final int CUSTOM = 3;
private static final int xFieldsDefaultEasy = 10;
private static final int yFieldsDefaultEasy = 10;
private static final int minesDefaultEasy = 10;
private static final int xFieldsDefaultIntermediate = 20;
private static final int yFieldsDefaultIntermediate = 20;
private static final int minesDefaultIntermediate = 50;
private static final int xFieldsDefaultHard = 30;
private static final int yFieldsDefaultHard = 30;
private static final int minesDefaultHard = 100;
private int xFields = xFieldsDefaultEasy;
private int yFields = yFieldsDefaultEasy;
private int mines = minesDefaultEasy;
private int flagsSet = 0;
private int width = 1000;
private int height = 1000;
private int mode = 0;
private int hiddenFields = xFields * yFields;
public boolean firstClick = true;
private transient MainWindow mw;
private HighscoreList easy;
private HighscoreList intermediate;
private HighscoreList hard;
private HighscoreList custom;
public Data(){
setMode(EASY);
HighscoreList easy = new HighscoreList();
HighscoreList intermediate = new HighscoreList();
HighscoreList hard = new HighscoreList();
HighscoreList custom = new HighscoreList();
//Testing purposes //TODO: Remove!
System.out.println("Initialising demo entries");
easy.add("asdf", 432151, 237509817, 1324);
easy.add("19823741324", 34532134, 21341324, 3234);
}
public void setMode(int newMode){
mode = newMode;
switch(newMode){
case 0:
xFields = xFieldsDefaultEasy;
yFields = yFieldsDefaultEasy;
setMines(minesDefaultEasy);
hiddenFields = xFields * yFields;
break;
case 1:
xFields = xFieldsDefaultIntermediate;
yFields = yFieldsDefaultIntermediate;
setMines(minesDefaultIntermediate);
hiddenFields = xFields * yFields;
break;
case 2:
xFields = xFieldsDefaultHard;
yFields = yFieldsDefaultHard;
setMines(minesDefaultHard);
hiddenFields = xFields * yFields;
break;
case 3: break;
default: break;
}
}
public void setMode(int newMode, int xFieldsNew, int yFieldsNew, int minesNew){
mode = newMode;
switch(newMode){
case 0:
xFields = xFieldsDefaultEasy;
yFields = yFieldsDefaultEasy;
setMines(minesDefaultEasy);
hiddenFields = xFields * yFields;
break;
case 1:
xFields = xFieldsDefaultIntermediate;
yFields = yFieldsDefaultIntermediate;
setMines(minesDefaultIntermediate);
hiddenFields = xFields * yFields;
break;
case 2:
xFields = xFieldsDefaultHard;
yFields = yFieldsDefaultHard;
setMines(minesDefaultHard);
hiddenFields = xFields * yFields;
break;
case 3: xFields = xFieldsNew; yFields = yFieldsNew; setMines(minesNew); break;
default: break;
}
}
public void setXFields(int xFieldsNew){
xFields = xFieldsNew;
hiddenFields = xFields * yFields;
determineGameMode();
}
public void setYFields(int yFieldsNew){
yFields = yFieldsNew;
hiddenFields = xFields * yFields;
determineGameMode();
}
public void setMines(int minesNew){
mines = minesNew;
hiddenFields = xFields * yFields;
determineGameMode();
}
public void determineGameMode(){
if (xFields == xFieldsDefaultEasy && yFields == yFieldsDefaultEasy && getMines() == minesDefaultEasy){
mode = EASY;
} else if (xFields == xFieldsDefaultIntermediate && yFields == yFieldsDefaultIntermediate && getMines() == minesDefaultIntermediate){
mode = INTERMEDIATE;
} else if (xFields == xFieldsDefaultHard && yFields == yFieldsDefaultHard && getMines() == minesDefaultHard){
mode = HARD;
} else {
mode = CUSTOM;
}
}
public void setMainWindow(MainWindow mwnew){
mw = mwnew;
}
public MainWindow mainWindow(){
return mw;
}
public void setFlagsSet(int flagsSetNew){
if (flagsSet != flagsSetNew){
flagsSet = flagsSetNew;
mw.updateMineNum();
}
}
public int getFlagsSet(){
return flagsSet;
}
public int getXFields() {
return xFields;
}
public int getYFields() {
return yFields;
}
public int getMines() {
return mines;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
height = height;
}
public int getMode() {
return mode;
}
public void resetFlagsSet(){
flagsSet = 0;
}
public void resetFirstClick(){
firstClick = true;
}
public int getHiddenFields() {
return hiddenFields;
}
public void setHiddenFields(int coveredFields) {
hiddenFields = coveredFields;
}
public void resetHiddenFields(){
hiddenFields = xFields * yFields;
}
public HighscoreList getHighscoresEasy() {
if (easy == null) easy = new HighscoreList();
return easy;
}
public void setHighscoresEasy(HighscoreList easy) {
this.easy = easy;
}
public HighscoreList getHighscoresIntermediate() {
if (intermediate == null) intermediate = new HighscoreList();
return intermediate;
}
public void setHighscoresIntermediate(HighscoreList intermediate) {
this.intermediate = intermediate;
}
public HighscoreList getHighscoresHard() {
if (hard == null) hard = new HighscoreList();
return hard;
}
public void setHighscoresHard(HighscoreList hard) {
this.hard = hard;
}
public HighscoreList getHighscoresCustom() {
if (custom == null) custom = new HighscoreList();
return custom;
}
public void setHighscoresCustom(HighscoreList custom) {
this.custom = custom;
}
public boolean firstClick(){
return firstClick;
}
public void firstClicked(){
firstClick = false;
}
}
package meta;
import gui.MainWindow;
public class DataManager {
private static Data data;
private static int i = 0;
// public static Data getData(){
// check();
// return data;
// }
//
// public static void setData(Data newData){
// data = newData;
// }
private static void check(){
if (data == null){ data = new Data(); System.out.println("Created new data object (" + i + ")"); i++;}
}
public static void setMode(int newMode){check(); data.setMode(newMode);}
public static void setMode(int newMode, int xFieldsNew, int yFieldsNew, int minesNew){
check();
data.setMode(newMode, xFieldsNew, yFieldsNew, minesNew);
}
public static void setXFields(int xFieldsNew){check(); data.setXFields(xFieldsNew);}
public static void setYFields(int yFieldsNew){check(); data.setYFields(yFieldsNew);}
public static void setMines(int minesNew){check(); data.setMines(minesNew);}
public static void determineGameMode(){check(); data.determineGameMode();}
public static void setMainWindow(MainWindow mwnew){check(); data.setMainWindow(mwnew);}
public static MainWindow mainWindow(){check(); return data.mainWindow();}
public static void setFlagsSet(int flagsSetNew){check(); data.setFlagsSet(flagsSetNew);}
public static int getFlagsSet(){check(); return data.getFlagsSet();}
public static int getXFields() {check(); return data.getXFields();}
public static int getYFields() {check(); return data.getYFields();}
public static int getMines() {check(); return data.getMines();}
public static int getWidth() {check(); return data.getWidth();}
public static void setWidth(int width) {check(); data.setWidth(width);}
public static int getHeight() {check(); return data.getHeight();}
public static void setHeight(int height) {check(); data.setHeight(height);}
public static int getMode() {check(); return data.getMode();}
public static void resetFlagsSet(){check(); data.resetFlagsSet();}
public static void resetFirstClick(){check(); data.resetFirstClick();}
public static int getHiddenFields() {check(); return data.getHiddenFields();}
public static void setHiddenFields(int coveredFields) {check(); data.setHiddenFields(coveredFields);}
public static void resetHiddenFields(){check(); data.resetHiddenFields();}
public static HighscoreList getHighscoresEasy() {check(); return data.getHighscoresEasy();}
public static void setHighscoresEasy(HighscoreList easy) {check(); data.setHighscoresEasy(easy);}
public static HighscoreList getHighscoresIntermediate() {check(); return data.getHighscoresIntermediate(); }
public static void setHighscoresIntermediate(HighscoreList intermediate) {check(); data.setHighscoresIntermediate(intermediate);}
public static HighscoreList getHighscoresHard() {check(); return data.getHighscoresHard();}
public static void setHighscoresHard(HighscoreList hard) {check(); data.setHighscoresHard(hard);}
public static HighscoreList getHighscoresCustom() {check(); return data.getHighscoresCustom();}
public static void setHighscoresCustom(HighscoreList custom) {check(); data.setHighscoresCustom(custom);}
public static boolean firstClick(){check(); return data.firstClick();}
public static void firstClicked(){check(); data.firstClicked();}
}
package meta;
import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
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 SimpleLongProperty duration;
private SimpleStringProperty durationString;
private SimpleObjectProperty<LocalDateTime> startTime;
private SimpleStringProperty startTimeString;
private SimpleIntegerProperty moves;
private SimpleIntegerProperty xTiles;
private SimpleIntegerProperty yTiles;
private SimpleIntegerProperty mines;
public HighscoreEntry(){
}
public HighscoreEntry(String name, long startTime, long duration, int moves){
this.name = new SimpleStringProperty(name);
this.setStartTime(startTime);
this.setDuration(duration);
this.moves = new SimpleIntegerProperty(moves);
this.startTime.addListener((ov, oldValue, newValue) -> {
startTimeString.set(formatStartTime());
});
this.duration.addListener((ov, oldValue, newValue) -> {
durationString.set(formatDuration());
});
}
public HighscoreEntry(String name, long startTime, long duration, int moves, int xTiles, int yTiles, int mines){
this(name, startTime, duration, moves);
this.xTiles = new SimpleIntegerProperty(xTiles);
this.yTiles = new SimpleIntegerProperty(yTiles);
this.mines = new SimpleIntegerProperty(mines);
}
public String getName() {
return name.get();
}
public void setName(String name) {
this.name.set(name);
}
public long getDuration() {
return duration.get();
}
public void setDuration(long duration) {
if (this.duration == null) this.duration = new SimpleLongProperty(duration);
else this.duration.set(duration);
if (this.durationString == null) this.durationString = new SimpleStringProperty(formatDuration());
else this.durationString.set(formatDuration());
}
public LocalDateTime getStartTime() {
return startTime.get();
}
public void setStartTime(long startTime) {
this.setStartTime(LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault()));
}
public void setStartTime(LocalDateTime startTime){
if (this.startTime == null) this.startTime = new SimpleObjectProperty<LocalDateTime>(startTime);
else this.startTime.set(startTime);
if (this.startTimeString == null) this.startTimeString = new SimpleStringProperty(formatStartTime());
else this.startTimeString.set(formatStartTime());
}
public boolean isEqual(Object anObject){
try{
HighscoreEntry another = (HighscoreEntry)anObject;
return another.getStartTime() == getStartTime();
} catch (Exception ex){
return false;
}
}
public String formatStartTime(){
return getStartTime().getDayOfMonth() + "." + getStartTime().getMonth() + "." + getStartTime().getYear() + " " + getStartTime().getHour() + ":" + getStartTime().getMinute() + ":" + getStartTime().getSecond();
}
public String formatDuration(){
int seconds = (int)((getDuration() % (1000 * 60))/1000);
int minutes = (int)((getDuration() % (1000 * 60 * 60))/(1000*60));
int millis = (int)(getDuration() % 1000);
return String.valueOf(minutes) + ":" + String.valueOf(seconds) + "." + String.valueOf(millis);
}
public int getxTiles() {
return xTiles.get();
}
public void setxTiles(int xTiles) {
this.xTiles.set(xTiles);
}
public int getyTiles() {
return yTiles.get();
}
public void setyTiles(int yTiles) {
this.yTiles.set(yTiles);
}
public int getMines() {
return mines.get();
}
public void setMines(int mines) {
this.mines.set(mines);
}
public int getMoves() {
return moves.get();
}
public void setMoves(int moves) {
this.moves.set(moves);
}
public String getStartTimeString(){
/* Should produce the same output as formatStartTime(), but
* is needed to autofill the highscore table by passing this
* HighscoreEntry instance. The HighscoreTable will automatically
* call these getters to fill it's values in the table cells.
* Hence: DO NOT REMOVE!
*/
return startTimeString.get();
}
public String getDurationString(){
/* Should produce the same output as formatStartTime(), but
* is needed to autofill the highscore table by passing this
* HighscoreEntry instance. The HighscoreTable will automatically
* call these getters to fill it's values in the table cells.
* Hence: DO NOT REMOVE!
*/
return durationString.get();
}
}
package meta;
import java.io.Serializable;
import java.util.ArrayList;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class HighscoreList implements Serializable {
private ArrayList<HighscoreEntry> entries;
public HighscoreList(){
entries = new ArrayList<HighscoreEntry>();
}
public void add(String name, long startTime, long duration, int moves){
entries.add(new HighscoreEntry(name, startTime, duration, moves));
}
public void add(String name, long startTime, long duration, int moves, int xTiles, int yTiles, int mines){
entries.add(new HighscoreEntry(name, startTime, duration, moves, xTiles, yTiles, mines));
}
public HighscoreEntry get(int i){
return entries.get(i);
}
public int size(){
return entries.size();
}
public HighscoreEntry get(long timestamp){
HighscoreEntry dummy = new HighscoreEntry();
dummy.setDuration(timestamp);
return entries.get(entries.lastIndexOf(dummy));
}
public String getAllNumbers(){
String result = "";
for (int i = 0; i < entries.size(); i++){
result = result + String.valueOf(i) + System.lineSeparator();
}
return result;
}
public String getAllNames(){
String result = "";
for (HighscoreEntry h:entries){
result = result + h.getName() + System.lineSeparator();
}
return result;
}
public String getAllTimes(){
String result = "";
for (HighscoreEntry h:entries){
result = result + h.formatStartTime() + System.lineSeparator();
}
return result;
}
public String getAllDurations(){
String result = "";
for (HighscoreEntry h:entries){
result = result + h.formatDuration() + System.lineSeparator();
}
return result;
}
public ObservableList<HighscoreEntry> getObservableList(){
System.out.println("Number of Entries: " + size());
// ObservableList<HighscoreEntry> oal = FXCollections.emptyObservableList();
// for (HighscoreEntry i : entries) oal.add(i);
// System.out.println("oal size: " + oal.size());
// System.out.println("oal type: " + oal.get(0).getClass().getName());
// return oal;
// return FXCollections.observableArrayList(entries);
return FXCollections.observableArrayList( //TODO: remove
new HighscoreEntry("asdf", System.currentTimeMillis(), 314724, 123),
new HighscoreEntry("foo", System.currentTimeMillis(), 23478123, 123),
new HighscoreEntry("bar", System.currentTimeMillis(), 32194578, 123)
);
}
}
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.Scene;
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.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 BorderPane borderPane;
private Scene s;
private TabPane tabPane;
private Tab easy, intermediate, hard, custom;
private TableView<HighscoreEntry> tableEasy, tableIntermediate, tableHard, tableCustom;
@SuppressWarnings("unchecked")
public HighscoreStage(){
super.setTitle("Highscore");
borderPane = new BorderPane();
borderPane.setPadding(new Insets(25, 25, 25, 25));
s = new Scene(borderPane, 500, 500);
tabPane = new TabPane();
easy = new Tab("Easy");
intermediate = new Tab("Intermediate");
hard = new Tab("Hard");
custom = new Tab("Custom");
tabPane.getTabs().addAll(easy, intermediate, hard, custom);
tableEasy = new TableView<HighscoreEntry>();
tableEasy.setEditable(true);
tableIntermediate = new TableView<HighscoreEntry>();
tableHard = new TableView<HighscoreEntry>();
tableCustom = new TableView<HighscoreEntry>();
TableColumn number = new TableColumn("#");
TableColumn name = new TableColumn("Name");
TableColumn startTime = new TableColumn("Start Time");
TableColumn duration = new TableColumn("Duration");
TableColumn moves = new TableColumn("Moves");
TableColumn fieldWidth = new TableColumn("Width");
TableColumn fieldHeight = new TableColumn("Height");
TableColumn mines = new TableColumn("Mines");
name.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, String>("name"));
startTime.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, String>("startTimeString"));
duration.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, String>("durationString"));
moves.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, Integer>("moves"));
// fieldWidth.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, Integer>("xTiles"));
// fieldHeight.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, Integer>("yTiles"));
// mines.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, Integer>("mines"));
tableEasy.setItems(DataManager.getHighscoresEasy().getObservableList());
name.setCellValueFactory(new PropertyValueFactory<HighscoreEntry, String>("name"));
tableIntermediate.setItems(DataManager.getHighscoresIntermediate().getObservableList());
tableHard.setItems(DataManager.getHighscoresHard().getObservableList());
tableCustom.setItems(DataManager.getHighscoresCustom().getObservableList());
tableEasy.getColumns().addAll(name, startTime, duration, moves);
tableIntermediate.getColumns().addAll(number, name, startTime, duration, moves);
tableHard.getColumns().addAll(number, name, startTime, duration, moves);
tableCustom.getColumns().addAll(number, name, startTime, duration, moves, fieldWidth, fieldHeight, mines);
easy.setContent(tableEasy);
intermediate.setContent(tableIntermediate);
hard.setContent(tableHard);
custom.setContent(tableCustom);
borderPane.setCenter(tabPane);
fillWithData();
this.setScene(s);
this.show();
}
private void fillWithData(){
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment