0

I want to know if you can use TableColoum like Map?

I want to take a file > read the file > scan all characters > map the characters to the FIRST column > map the number of characters to the SECOND column.

Is it possible to do with TableColoum?

Best regards

Alex

EDIT!: Codes here:

        package sample;

        import javafx.application.Application;
        import javafx.beans.property.SimpleStringProperty;
        import javafx.beans.value.ObservableValue;
        import javafx.collections.FXCollections;
        import javafx.collections.ObservableList;
        import javafx.scene.Scene;
        import javafx.scene.control.TableColumn;
        import javafx.scene.control.TableView;
        import javafx.stage.Stage;
        import javafx.util.Callback;

        import java.io.File;
        import java.io.FileNotFoundException;
        import java.util.Map;
        import java.util.Scanner;
        import java.util.TreeMap;

        public class Main extends Application {

            Map<Character, Integer> countCharacters = new TreeMap<Character, Integer>();
            Scanner scanner = null;

            @Override
            public void start(Stage primaryStage) throws Exception {

                // sample data


                int total = 0;

                try {


               scanner = new Scanner(new File("MY_FILE_PATH_HERE"), "utf-8");

                    while (scanner.hasNext()) {
                        char[] chars = scanner.nextLine().toLowerCase().toCharArray();

                        for (char character : chars) {
                            if (Character.isLetter(character)) {
                                if (countCharacters.containsKey(character)) {
                                    countCharacters.put(character, countCharacters.get(character) + 1);
                                } else {
                                    countCharacters.put(character, 1);
                                }

                            }

                        }
                    }


                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    if (scanner != null) {
                        scanner.close();
                    }
                }

                if (!countCharacters.isEmpty()) {
                    for (Map.Entry<Character, Integer> entry : countCharacters.entrySet()) {
                        total += entry.getValue();
                    }

                    for (Map.Entry<Character, Integer> entry : countCharacters.entrySet()) {
                        System.out.println(entry.getKey() + ":\t" + entry.getValue() + "  \t: \t " + percentCount(entry.getValue(), total) + "%");
                    }

 if (!countCharacters.isEmpty()) {
            for (Map.Entry<Character, Integer> entry : countCharacters.entrySet()) {
                total += entry.getValue();
            }

            for (Map.Entry<Character, Integer> entry : countCharacters.entrySet()) {
                System.out.println(entry.getKey() + ":\t" + entry.getValue() + "  \t: \t " + percentCount(entry.getValue(), total) + "%");
            }

            TableColumn<Map.Entry<Character, Integer>, Character> column1 = new TableColumn<>("Key");

            for (Map.Entry<Character, Integer> entry : countCharacters.entrySet())
            {
                column1.setCellValueFactory(new PropertyValueFactory<Map.Entry<Character, Integer>, Character>("Test"));
            }

            TableColumn<Map.Entry<Character, Integer>, Character> column2 = new TableColumn<>("Value");

            for (Map.Entry<Character, Integer> entry : countCharacters.entrySet())
            {
                column2.setCellValueFactory(new PropertyValueFactory<Map.Entry<Character, Integer>, Character>(""));
            }


                    ObservableList<Map.Entry<Character, Integer>> items = FXCollections.observableArrayList(map.entrySet());
                    final TableView<Map.Entry<Character, Integer>> table = new TableView<>(items);

                    table.getColumns().setAll(column1, column2);

                    primaryStage.setTitle("Alice");
                    primaryStage.setScene(new Scene(table, 300, 275));
                    primaryStage.show();
                }
            }
            public static float percentCount(float number, int total)
            {
                return (number / total) * 100;
            }


            public static void main(String[] args) {
                launch(args);
            }
        }
Alexander Falk
  • 499
  • 8
  • 21

1 Answers1

1

The following solution must work for you. The solution is to convert the Character / Integer of your map to a String to display it on the TableView.

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.util.Map;
import java.util.TreeMap;

public class Main extends Application {

    Map<Character, Integer> countCharacters = new TreeMap<Character, Integer>();

    @Override
    public void start(Stage primaryStage) throws Exception {

        // sample data


        int total = 0;
        countCharacters.put('A', 2);
        countCharacters.put('B', 5);
        countCharacters.put('C', 10);

        if (!countCharacters.isEmpty()) {
            for (Map.Entry<Character, Integer> entry : countCharacters.entrySet()) {
                total += entry.getValue();
            }

            for (Map.Entry<Character, Integer> entry : countCharacters.entrySet()) {
                System.out.println(entry.getKey() + ":\t" + entry.getValue() + "  \t: \t " + percentCount(entry.getValue(), total) + "%");
            }

        }

        TableColumn<Map.Entry<Character, Integer>, String> column1 = new TableColumn<>("Key");
        column1.setCellValueFactory(
                new Callback<CellDataFeatures<Map.Entry<Character, Integer>, String>, ObservableValue<String>>() {
                    @Override
                    public ObservableValue<String> call(CellDataFeatures<Map.Entry<Character, Integer>, String> param) {
                        return new SimpleStringProperty(String.valueOf(param.getValue().getKey()));
                    }
                });

        TableColumn<Map.Entry<Character, Integer>, String> column2 = new TableColumn<>("Value");
        column2.setCellValueFactory(
                new Callback<CellDataFeatures<Map.Entry<Character, Integer>, String>, ObservableValue<String>>() {
                    @Override
                    public ObservableValue<String> call(CellDataFeatures<Map.Entry<Character, Integer>, String> param) {
                        return new SimpleStringProperty(String.valueOf(param.getValue().getValue()));
                    }
                });

        ObservableList<Map.Entry<Character, Integer>> items = FXCollections.observableArrayList(countCharacters.entrySet());
        final TableView<Map.Entry<Character, Integer>> table = new TableView<>(items);
        table.getColumns().addAll(column1, column2);

        primaryStage.setTitle("Alice");
        primaryStage.setScene(new Scene(table, 300, 275));
        primaryStage.show();
    }

    public static float percentCount(float number, int total) {
        return (number / total) * 100;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • How would you make a forLoop in this one? I want each character to get their own key and each Integer to get their own Value :) – Alexander Falk Sep 14 '15 at 09:45
  • Don't you think that is what we have here? ;) – ItachiUchiha Sep 14 '15 at 09:59
  • But if I would use the .put method in a for loop? How would I be able to do that with a text file? :-O – Alexander Falk Sep 14 '15 at 10:26
  • I FIGURED IT OUT! But I was wondering: Could you explain me HOW the TableColoumn and the ObservableList are working? – Alexander Falk Sep 14 '15 at 10:37
  • 1
    The tableview needs an ObservableList as its items, from which it picks the values. It delegates the responsibility of displaying the correct values to each of the [TableColumn](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TableColumn.html). We use the [setCellValueFactory()](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TableColumn.html#cellValueFactoryProperty) of TableColumn to allow it to display the correct value from the map entry. – ItachiUchiha Sep 14 '15 at 11:09
  • That was a great explanation! Thanks! How are you then able to make a loop with this method? – Alexander Falk Sep 14 '15 at 11:16
  • 1
    If you are asking about the how tableview iterates through the item and shows them in the table, it is internally handled by the API. The implementation must be in the TableViewSkin source. – ItachiUchiha Sep 14 '15 at 12:04
  • Thank you for your time to answer my question! I may need to read some more about API :) – Alexander Falk Sep 14 '15 at 17:09
  • I know you are gonna hate me for this question: But if I want to add an ekstra column, which gives me the percentage of each characters, can I then just add another cellValueFactory? – Alexander Falk Sep 14 '15 at 17:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89622/discussion-between-alexander-falk-and-itachiuchiha). – Alexander Falk Sep 14 '15 at 20:09