I'm making a program which creates and displays graphs.
I have a class which handles a graph and implements a mouse listener (Graphstream ViewerListener
).
In order to grab the events, you need to use a loop and call a pump() method until you're done.
To display the graph, the Graphstream
library has a View object which extends and can be used as a regular Jpanel
, just set the View object as a content pane in the JFrame
.
My class works fine when used with a JFrame
with no menu bar.
But when i set a JMenuBar
at the JFrame
, the problems start.
I call the method inside my graph class, it gets inside the loop, but the ViewerListener
doesn't work, like it's not listening for events. Why?
This is my class code, unnecessary stuff omitted
public class GeneratedGraph implements ViewerListener{
public String[] getNodes(){
boolean flag=true;
startEndNodes[0]=null; startEndNodes[1]=null;
arrayposition=0;
while(flag){
System.out.println("Inside the loop");
fromViewer.pump();
try {
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(startEndNodes[0]!=null && startEndNodes[1]!=null){
flag=false;
}
}
System.out.println("Outside the loop");
return startEndNodes;
}
public void buttonPushed(String id) { //This method fires up when you click on a node
System.out.println("Button pushed on node "+id);
startEndNodes[arrayposition]=id;
arrayposition++;
if(arrayposition>1){ arrayposition=0;}
}
}
Now, to put things into perspective, this WORKS, gets in, grabs the nodes, and exits the loop fine. (unnecessary code again omitted)
public class main extends implements ActionListener {
public static void main(String args[]){
JFrame mainFrame = new JFrame("Graph");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GeneratedGraph myGraph = new GeneratedGraph(mainFrame);
myGraph.option1(5,6); //a method which creates a graph
myGraph.show();
mainFrame.pack();
mainFrame.setVisible(true);
String s[] = myGraph.getNodes();
}
}
But when i add this, it doesn't. It just gets inside the loop and events from the ViewerListener
never fire so it gets stuck.
public class main extends implements ActionListener {
public static void main(String args[]){
JFrame mainFrame = new JFrame("Graph");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar= new JMenuBar();
mainFrame.setJMenuBar(menuBar);
JMenu algorithmMenu = new JMenu("Algorithms");
JMenuItem dijkstra = new JMenuItem("Dijkstra");
dijkstra.addActionListener(this);
dijkstra.setActionCommand("Dijkstra");
algorithmMenu.add(dijkstra);
menuBar.add(algorithmMenu);
GeneratedGraph myGraph = new GeneratedGraph(mainFrame);
myGraph.option1(5,6); //a method which creates a graph
myGraph.show();
mainFrame.pack();
mainFrame.setVisible(true);
String s[] = myGraph.getNodes();
}
}
The part that seems to break everything is this
menuBar.add(algorithmMenu);
When i comment out this particular line, it works fine.
Any ideas what might be causing the ViewerListener
not to respond? It just seems so random that a JMenuBar
would break it.