N
N
nadom2015-12-09 22:14:26
Java
nadom, 2015-12-09 22:14:26

How to implement thread inside JFrame?

By clicking on the button, a new JFrame is created in which the graph is drawn.
Since there are no built-in charts in Java.swing, I use the jCharts library. The library is good, but for correct display in the created frame, I start a thread that periodically redraws the chart.
At the same time, the minimize button works, but the cross does not work.
Due to the fact that the thread is constantly falling asleep, the main frame, on which the button for creating a frame with a graph, is not even pressed.
How to bypass it?

Perhaps there is a window move event, then I know how to organize a frame with a graph without a thread. Now it is impossible to remove the stream, because redrawing is necessary.

UPD In other words, I need to force the thread that draws the main frame to sleep until the graph frame is closed.

frame = new JFrame();
    frame.setSize( 600, 600 );
    frame.setTitle(title);
    frame.setResizable(false);
    frame.setVisible( true );
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    Thread T = new Thread (this);
    init_components(type, xAxisLabels, xAxisTitle, 
        yAxisTitle, title, legendLabels, data);
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                T.run();
            }
        });

@Override
  public void run() {
    while (true) {
      try {
        repaint();
        T.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace(System.out);
      }
    }
  }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question