0

I have this task:

"We do not want to rely on us Currency their toString() for how a currency is displayed in list our. We will be able to set this up ourselves.

Create a static nested class called "Currency Cell" in ValutaOversikController as extender List Cell <Value>.

Override methods updateItem (Currency and Currency, boolean empty).

Set how a currency should be presented in the list e.g. "Country - Currency Code"

Then put CellFactory for our ListView, which returns an instance of the new Currency Cell class."

I started to make the last method in Controller, but don't know if this is correct. As of now this is what I have:

public class Controller {
@FXML
private ComboBox<Valuta> listeMedValutaerEn, listeMedValutaerTo;
@FXML
private ComboBox<Sorteringen> listeMedSortering;
@FXML
private TextField textFieldValutaerEn, textFieldValutaerTo;
@FXML
private ImageView imageViewValutaerEn, imageViewValutaerTo;

@FXML
public void initialize() {
    listeMedValutaerEn.setItems(DataHandler.hentValutaData());
    listeMedValutaerTo.setItems(DataHandler.hentValutaData());
    listeMedSortering.setItems(DataHandler.hentSorteringsData());

    listeMedValutaerEn.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Valuta>() {
        @Override
        public void changed(ObservableValue<? extends Valuta> observableValue, Valuta gammelValuta, Valuta nyValuta) {
            fyllUtValutaEn(nyValuta);
        }
    });

    listeMedValutaerTo.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Valuta>() {
        @Override
        public void changed(ObservableValue<? extends Valuta> observableValue, Valuta gammelValuta, Valuta nyValuta) {
            fyllUtValutaTo(nyValuta);
        }
    });

    listeMedSortering.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Sorteringen>() {
        @Override
        public void changed(ObservableValue<? extends Sorteringen> observableValue, Sorteringen gammelSortering, Sorteringen nySortering) {
            sortere(nySortering);
        }
    });
}

private void sortere(Sorteringen nySortering) {

    ObservableList<Valuta> valutaSomSkalSorteres = DataHandler.hentValutaData();

    CompareToValuta sortere = new CompareToValuta(nySortering.getSorteringsKode());

    Collections.sort(valutaSomSkalSorteres, sortere);

    listeMedValutaerEn.setItems(valutaSomSkalSorteres);
    listeMedValutaerTo.setItems(valutaSomSkalSorteres);
}

private void fyllUtValutaEn(Valuta enValuta) {
    if (enValuta != null) {
        Image flaggEn = new Image("https://www.countryflags.io/" + enValuta.getLandskode() + "/shiny/64.png");
        imageViewValutaerEn.setImage(flaggEn);
    }
}

private void fyllUtValutaTo(Valuta enValuta) {
    if (enValuta != null) {
        Image flaggTo = new Image("https://www.countryflags.io/" + enValuta.getLandskode() + "/shiny/64.png");
        imageViewValutaerTo.setImage(flaggTo);
    }
}

@FXML
private void buttonBeregn(ActionEvent event) {
    Integer valutaAntall = Integer.valueOf(textFieldValutaerEn.getText());
    double valutaNrEn = listeMedValutaerEn.getSelectionModel().getSelectedItem().getValutakurs();
    double valutaNrTo = listeMedValutaerTo.getSelectionModel().getSelectedItem().getValutakurs();
    double valutaResultat = valutaAntall * (valutaNrEn / valutaNrTo);
    textFieldValutaerTo.setText(String.valueOf(valutaResultat));
}

private static ListCell<Valuta> ValutaCelle() {
    ListCell<Valuta> tja = new ListCell<>();
    return tja;
}
}

Class DataHandler:

public class DataHandler {
private final static ObservableList<Valuta> valutaListe = FXCollections.observableArrayList();
private final static ObservableList<Sorteringen> sorteringsListe = FXCollections.observableArrayList();

public static ObservableList<Sorteringen> hentSorteringsData() {
    if (sorteringsListe.isEmpty()) {
        sorteringsListe.add(new Sorteringen("Sortere alfabetisk på land synkende", 1));
        sorteringsListe.add(new Sorteringen("Sortere alfabetisk på land stigende", 2));
        sorteringsListe.add(new Sorteringen("Sortere på valutakode, stigende", 3));
        sorteringsListe.add(new Sorteringen("Sortere på valutakode, synkende", 4));
    }
    return sorteringsListe;
}

public static ObservableList<Valuta> hentValutaData() {
    if (valutaListe.isEmpty()) {
        valutaListe.addAll(genererValutaData());
    }
    return valutaListe;
}

private static ArrayList<Valuta> genererValutaData() {
    File kilden = new File("src/no/hiof/aleksar/oblig5/valutakurser.csv");

    ArrayList<Valuta> valutaerFraFiler = lesFraCSVFil(kilden);

    return valutaerFraFiler;
}

private static ArrayList<Valuta> lesFraCSVFil(File filSomLesesFra) {
    ArrayList<Valuta> valutaerFraFil = new ArrayList<>();
    try (BufferedReader bufretLeser = new BufferedReader(new FileReader(filSomLesesFra))) {
        String linje;
        while( (linje = bufretLeser.readLine()) != null ){
            String[] deler = linje.split(";");

            Valuta enValuta = new Valuta(deler[0], deler[1], deler[2], Double.parseDouble(deler[3]));

            valutaerFraFil.add(enValuta);
        }

    } catch (IOException e) {
        System.out.println(e);
    }

    return valutaerFraFil;
}
}
double-beep
  • 5,031
  • 17
  • 33
  • 41
  • I don't see a custom ListCell implementation - best to re-read your course tutorial (or any other basic fx tutorial, the info page of the javafx tag here has references) on how-to do it :) – kleopatra Mar 26 '19 at 15:20
  • This does not satisfy the requirements for the exercise. There is no static inner class and `updateItem` only occurs once in your question and it's not part of the code. Needless to say requirements describe the desired results in enough detail to make the only thing you need to worry about the implementation of `updateItem`. Lacking details about the types involved and the desired look/text of the cell, there's little we can do to help... – fabian Mar 26 '19 at 18:05
  • Might help (though you'd want to use a static nested class, rather than an anonymous class): https://stackoverflow.com/questions/36657299/how-can-i-populate-a-listview-in-javafx-using-custom-objects Also see [Nested Classes](https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html); you appear to have created a static _method_ instead. – Slaw Mar 26 '19 at 18:16

0 Answers0