I have a String containing a Date in the form "dd-MM-yyyy" I want the dates to be sorted when displayed in the table view.
Controller Class
private final ObservableList<Stock>stockList = FXCollections.observableArrayList();
public class StocksController implements Initializable {
@FXML
private TableColumn<Stock, String> date;
public void initialize(URL url, ResourceBundle resourceBundle){
addToCSV.setOnAction(event -> {
try {
writeCSV();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
dollarButton.setOnAction(event -> {changeDollar();});
percentButton.setOnAction(event -> {changePercent();});
price.setCellValueFactory(new PropertyValueFactory<>("price"));
date.setCellValueFactory(new PropertyValueFactory<>("dateRN"));
checker.setCellValueFactory(new PropertyValueFactory<>("checker"));
stockView.setItems(stockList);
search();
readCSV();
}
public void writeCSV() throws IOException {
String stockNames = ("$" + stockName.getText()).trim().toUpperCase();
Double stockPrices = Double.parseDouble((stockPrice.getText()).trim());
String stockDates = stockDate.getValue().format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
FileWriter fw = new FileWriter("src/stocks.csv",true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println(stockNames + ',' + stockDates + ',' + stockPrices);
pw.flush();
pw.close();
stockList.clear();
readCSV();
}
public void readCSV(){
String csv;
csv = "src/stocks.csv";
String delimiter = ",";
try{
System.out.println(new FileReader(csv));
BufferedReader br = new BufferedReader(new FileReader(csv));
String line;
while((line = br.readLine()) != null) {
String[] values = line.split(delimiter);
String stockNames = values[0];
String stockDates = values[1];
Double stockPrices = Double.parseDouble(values[2]);
Stock stock = new Stock(stockNames, stockPrices,stockDates);
stockList.add(stock);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(StocksController.class.getName())
.log(Level.FINE, null, ex);
} catch (IOException ex) {
Logger.getLogger(StocksController.class.getName())
.log(Level.FINE, null, ex);
}
}
}
Stock Class
public class Stock {
private String dateRN;
public Stock(){
this.dateRN = "";
}
public Stock(String ticker, Double price, String dateRN){
this.ticker = ticker;
this.price = price;
this.dateRN = dateRN;
this.checker = new CheckBox();
}
public String getDateRN() {
return dateRN;
}
public void setDateRN(String dateRN) {
this.dateRN = dateRN;
}
}
I've tried creating a comparator for Stock but it's not working so I gave up on that method is there anyway I could just create a function inside my controller sorting it directly from the observableList itself? or even when I read in the CSV?