0

I am working on a program that outputs the shortest path between two artists (known by their integer IDs). Start and end are the used inputs for the two artists. I am creating a GUI with a run button and a label. When the button is pressed, the program should calculate a shortest path, which is then returned as a string and set as the label of the GUI. The shortest path would look something like this 5 73 19 100, where each of the ints are IDs of artists and 5 is the start artist and 100 is the end artist.

My code for shortest path (the code in function()) works fine when I put it in main. However, when I try to combine get my GUI to run it, the final string only contains the first artist. It seems my da.run is no longer functional? Does this have something to do with the fact it is in another class?

Any pointers about what might be happening would be appreciated. Here is all the relevant GUI code since I think that is where the problem is

public class ShortestPath extends JFrame {

private static Map<Integer, Artist> artists = new HashMap<Integer, Artist>();
private static Artist start, end;
private static ArtistGraph map = new ArtistGraph(artists);
private static DijkstrasAlgorithm da = new DijkstrasAlgorithm(map);
private static Route r = new Route();

private static Connection connection;
private static Statement statement;

private Container contentPane;
private JPanel panel1, panel2;
private JButton button1;
private JLabel label1, label2, label3;
private JTextField field1, field2;

public ShortestPath() {
    //CODE FOR SETTING LABELS, PANELS, ETC REMOVED BECAUSE NOT RELEVANT FOR QUESTION
    button1.addActionListener(new Listener());
    panel1.add(button1);

    pack();
    setVisible(true);

}

private class Listener implements ActionListener
{
    public void actionPerformed(ActionEvent e)  
    {

        start = generateArtist(field1.getText());
        end = generateArtist(field2.getText());
        String done = function();


        label3.setText(done);


    }
}

 public static String function(){               
        ArtistGraph map = new ArtistGraph(artists);
        DijkstrasAlgorithm da = new DijkstrasAlgorithm(map);
        da.run(start, end);

        Route r = new Route();
        r.calculateRoute(end, da);

        for (int i = 0; i < r.getRoute().size(); i++) {
            int artist_id = r.getRoute().get(i).getID();
            System.out.println(artist_id);
        }

        String s = r.toString();

        return s;
    }

Let me know if any other pieces of code would be useful.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I wonder if we don't have enough information to make an educated guess as to what the problem is. – Hovercraft Full Of Eels Mar 10 '12 at 21:29
  • I think it might have to do with where your fields in Listener reference. As @HovercraftFullOfEels said, having more information would be of great benefit. – Ryan Amos Mar 10 '12 at 21:29
  • I don't think that the problem is with the DijkstrasAlgorithm class since your code works without the GUI. No, I think that the problem is with the GUI and how you get information from it (perhaps). – Hovercraft Full Of Eels Mar 10 '12 at 21:58
  • 1
    There is a possible problem with your doing your `function()` on the Swing event thread, the EDT. Perhaps you want to do this on a background thread, but having said that, I doubt that this will solve your main problem. For that we'll need to see more code, I think, ... just not too much code please. :) – Hovercraft Full Of Eels Mar 10 '12 at 22:02
  • I added all the relevant GUI code. It's pretty basic so I don't know why it isn't working. Any suggestions? – user1261492 Mar 10 '12 at 22:08
  • 1
    Sorry not from me :( . You probably need to do some debugging with a debugger or println statements to find out more about the problem. – Hovercraft Full Of Eels Mar 10 '12 at 22:14
  • What does `Route.toString()` return - does it uses spaces or something else to separate the artist IDs? – Joni Mar 10 '12 at 22:31
  • The code looks ok. Please create a SSCCE: http://sscce.org/ – Esko Luontola Mar 10 '12 at 22:32
  • Route.toString() returns a string with artists separated by a space: s = s + artist_id + " "; – user1261492 Mar 10 '12 at 22:44
  • 2
    I will try to have an SSCCE going soon. Working on it – user1261492 Mar 10 '12 at 22:54
  • 1
    My guess would be it is some kind of data visibility issue. GUI events are handled by a separate thread, and the structures you use here are neither thread safe nor do they provide any ordering or visibility guarantees. If, for example you modify `artists` from the main thread (either the reference or the contents), these changes may not be seen by the Swing event dispatch thread from which your `function()` is called. By the way, using static variables to pass data around between functions is not good design, even in a single-threaded application. – Michał Kosmulski Mar 10 '12 at 23:18

1 Answers1

1

There's not enough information to identify a specific flaw in the code you've posted. Going forward, you may want to look at the Model–View–Controller pattern to isolate your model (a graph searched using Dijkstra's algorithm) from the view (the GUI display). An example is discussed here. As the search may take some time to complete, consider using SwingWorker, illustrated here. It will allow the search to proceed without blocking the GUI's event dispatch thread.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045