user1261492
user1261492

Reputation: 1

Java GUI functionality - subclass issues?

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.

Upvotes: 0

Views: 495

Answers (1)

trashgod
trashgod

Reputation: 205785

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.

Upvotes: 1

Related Questions