1

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?

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305

1 Answers1

3

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • You are going to need to do at least some data copying in order to get random access into the `Map`. – Tom Hawtin - tackline Apr 21 '13 at 16:13
  • Aha, I did play around with a JTable, my main issue however was that the rows and columns were editable (although I think I know how to fix that). So thanks, now I know that's the direction to head in! – Rhys Harris Apr 21 '13 at 16:16
  • @TomHawtin-tackline: I not sure about that. If the Map is ordered, and if any change to the Map's data fires one of the AbstractTableModel fire data changed methods, then you can iterate through the map whenever you need to obtain random access. – Hovercraft Full Of Eels Apr 21 '13 at 16:18
  • @RhysHarris: yep, that's an easy fix: you can have your AbstractTableModel override `isCellEditable(...)` and have it return false. – Hovercraft Full Of Eels Apr 21 '13 at 16:18
  • 1
    @HovercraftFullOfEels You are in danger of some O(n^2) algorithms in there. Though of course iterating through a linked list is relatively cheap in the scheme of things, and you'd need a quite large n to before you hit the massive performance hit. – Tom Hawtin - tackline Apr 21 '13 at 16:22
  • @TomHawtin-tackline: I agree strongly with you there. If the data is very large though, I'd probably have it held in a database and then have the JTable display snippets of it obtained via queries. I am fearful whenever copying data that one copy will get out of sync with the other at some point. – Hovercraft Full Of Eels Apr 21 '13 at 16:23
  • 1
    Yes! Success using a JTable! Thanks for all the help :) – Rhys Harris Apr 21 '13 at 17:08