Question

I have a HashMap<String, String> representing a date (the key), and an amount (the value).

I would like to display this information on my interface. I began by using an ArrayList and adding a new JLabel to it for each new key & value that was added to the HashMap. However this led to display problems, and I also feel like it's not the right way to do this:

Iterator iterator = labels.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry pairs = (Map.Entry)iterator.next();
        entries.add(new JLabel(pairs.getKey() + ": £" + pairs.getValue()));
        for(JLabel entry : entries) {
            mainPanel.add(entry);
            for(int i = 0; i < entries.size() - 1; i+=2) {
                JLabel labelOne = entries.get(i);
                JLabel labelTwo = entries.get(i+1);
                mainPanelLayout.putConstraint(SpringLayout.NORTH, labelOne,
                    400,
                    SpringLayout.NORTH, contentPane);
                mainPanelLayout.putConstraint(SpringLayout.NORTH, labelTwo,
                    10,
                    SpringLayout.SOUTH, labelOne);

            }
            /*mainPanelLayout.putConstraint(SpringLayout.NORTH, entry,
                400,
                SpringLayout.NORTH, contentPane);*/
        }
    }

In terms of what I'm trying to achieve, I want to display the information like so:

21/04/13 : 300

22/04/13 : 400

Is there a way of displaying information from the HashMap in GUI?

Was it helpful?

Solution

I would use a JTable and display a String representation of the key in one column and String representations of important fields of the respective value in the other columns. The most important step I think will be in creating a decent TableModel to hold the data. It can use a Map as its data nucleus, but the Map will need to be orderable in some fashion I think for this to work well, perhaps a LinkedHashMap or a TreeMap.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top