1

I'm working on a program that parses a text and prints the output like this

LS  1 -> 3
LS  2 -> 3
LS  3 -> 2
PRP itself -> 2
PRP it -> 5
DT  all -> 7
DT  All -> 11
DT  no -> 9
DT  a -> 77
NNP Milwaukee -> 2
NNP D65 -> 1
NNP STD -> 1
NNP Gimp -> 3
NNP Constitution -> 1

Instead i want something like

enter image description here

I Tried using JTable for 3 hours and couldn't still figure out how do i sort the text to table. Please Help.

Sample Code

public String getTagsList() throws IOException {
    String output = "";
    for (Map.Entry<String, Map<String, Integer>> entry : tagMap.entrySet()) {
        String oa = entry.getKey();
        Map<String, Integer> words = entry.getValue();
        for (Map.Entry<String, Integer> entryWords : words.entrySet()) {
            String ob = entryWords.getKey() + " -> " + entryWords.getValue();
            output += oa + "\t" + ob + "\n";
        }
    }
    return output;
}

And

try {
    if (cmd == cmdOpen) {
        int code = chooser.showOpenDialog(myPane);
        if (code == JFileChooser.APPROVE_OPTION) {
            File selectedFile = chooser.getSelectedFile();
            fileName = selectedFile.getName();
            TagText tagText = new TagText(selectedFile.getAbsolutePath(), 4);
            myPane.setText(tagText.getTagsList());
        }
    }
}
Ramesh-X
  • 4,853
  • 6
  • 46
  • 67
  • What is the relationship between the rows? If you sort by the `LS` column how will effect the other columns? – MadProgrammer Dec 15 '15 at 04:22
  • 3 hours is not a long time..! – Ramesh-X Dec 15 '15 at 04:48
  • Yes it is...to a mayfly... – Michael Queue Dec 15 '15 at 05:09
  • 1
    There is no relationship between the rows if it is an LS it should fall under a row. @Ramesh-X: Why there are more people who try to be so negative ? Every one had been a noob in their life time and i'm trying to learn something. I didn't ask for an entire code. I just wanted to know how it is best done so i can implement it. Peace :) – Raghav Chandra Dec 15 '15 at 05:14
  • I think i got it wrong. LS stands for List item marker, it is printing out all the LSes in a file and and number of occurrences. and there are 34 different terms like LS. So i wanted to list them in a table with the tittle on the top and the contents below it – Raghav Chandra Dec 15 '15 at 05:23
  • Let your `TableModel` contain the `Map`, for [example](http://stackoverflow.com/a/9134371/230513). – trashgod Dec 15 '15 at 11:28

1 Answers1

0

There can be thousands of ways to do this. This is one I thought of..

First you can add the your items into separate Lists as columns. And keep a variable to track down the maximum size of a column.

    List<List<String>> columns = new ArrayList<>();
    int maxColumnSize = 0;
    for (Map.Entry<String, Map<String, Integer>> entry : tagMap.entrySet()) {
        List<String> column = new ArrayList<>();
        column.add(entry.getKey());
        Map<String, Integer> words = entry.getValue();
        for (Map.Entry<String, Integer> entryWords : words.entrySet()) {
            String ob = entryWords.getKey() + " -> " + entryWords.getValue();
            column.add(ob);
        }
        columns.add(column);
        if (column.size() > maxColumnSize) {
            maxColumnSize = column.size();
        }
    }

Then you can print each column, row by row as follows..

    String output = "";
    for (int i = 0; i < maxColumnSize; i++) {
        for (List<String> column : columns) {
            String s = "  ";
            if(i<column.size()){
                s = column.get(i);
            }
            output += s + "\t";
        }
        output += "\n";
    }

This is a sample code. You can edit the printing mechanism to make you table nicer. (eg: you can use java.util.Formatter to format your String)

Ramesh-X
  • 4,853
  • 6
  • 46
  • 67