3

I have following java code in my thread class:

@Override    
public void run() {

    JFrame window = new JFrame("Visualization POC");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    while (true) {
        window.setVisible(false);

        Layout<Node,Edge> layout = new CircleLayout<Node, Edge>(graph);
        layout.setSize(new Dimension(300, 300));

        BasicVisualizationServer<Node, Edge> vv = new BasicVisualizationServer<Node, Edge>(layout);
        vv.setPreferredSize(new Dimension(350, 350));
        vv.getRenderContext().setVertexFillPaintTransformer(new NodeColorTransformer());
        vv.getRenderContext().setEdgeDrawPaintTransformer(new EdgeColorTransformer());

        window.getContentPane().add(vv);
        window.pack();
        window.setVisible(true);

        try {
            Thread.sleep(ONE_SECOND);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

I want to use it to refresh state of graph visualization, but I got stuck on massive problem. When the block of code creating layout and setting content of JFrame is inside while loop it is not displayed in output window. When I place it before while, it works fine but it isn't that what I want. I run this thread via SpringUtilities.invokeLater in my main class.

I can see that the window is refresh, because it is blinking for a while. I'm looking forward for any tips.

tomi891
  • 85
  • 1
  • 5
  • 1
    _Don't_ sleep on the EDT; _do_ see [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/). – trashgod Jan 21 '12 at 21:46
  • But one of the task of background thread is to update data for every second. Should it be then launch every second in main thread in a loop or something? – tomi891 Jan 22 '12 at 10:34
  • Ok, after research I found answer for looping thread execution. Two key classes: Timer and TimerTask from java.util package – tomi891 Jan 22 '12 at 11:17
  • beware: while you might use those classes in the util package, they don't help you in _accessing all_ Swing component properties on the EDT – kleopatra Jan 22 '12 at 11:50
  • @kleopatra raises a good point; also consider `javax.swing.Timer`, compared [here](http://stackoverflow.com/a/8281574/230513). – trashgod Jan 22 '12 at 12:03
  • What is your intention to keep creating the same JUNG layout instance and calling the same Transformers in a loop of a Runnable/Thread? You don't have to. The right JUNG's Transformers will help you updating every vertice/edge's features and events; you only need to call these Transformers only once during main class construction and JUNG will register them in its "engine". – ee. Jan 25 '12 at 01:01

1 Answers1

0

You invoke your code from EDT thread. Any update to the component will be painted at the end of the EDT thread. Your thread will not exit

I have modified your code little, it works fine. please replace JLabel with your jung .

final JFrame window = new JFrame("Visualization POC");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final Random ran = new Random();

while (true) {


  SwingUtilities.invokeLater(new Runnable(){
public void run() {
    window.setVisible(false);

     JPanel panel = new JPanel();
     JLabel label = new JLabel();
      int nextInt = ran.nextInt(100);
     label.setText("Test Label"+String.valueOf(nextInt));
    System.out.println(nextInt);
     panel.add(label);

    window.getContentPane().removeAll();
    window.getContentPane().add(panel);
    window.pack();
    window.setVisible(true);


}

});

try {

     Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

}

defender
  • 353
  • 2
  • 11