开发者

Prevent Main UI to collapse from child thread

开发者 https://www.devze.com 2023-04-07 14:15 出处:网络
I have this code: class FinalUI1 extends javax.swing.JFrame { //do something Thread t; try { t = new Thread(new PcapTool(null));

I have this code:

   class FinalUI1 extends javax.swing.JFrame 
     {
       //do something 
          Thread t;
        try {
            t = new Thread(new PcapTool(null));
            t.start();
           } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
       // do something

     }

     class PcapTool extends ApplicationFrame implements Runnable
  {
     //do something
     public void run() {
        Display Graph based on above classes input
         }
   }

There is a Main UI window and new graphs are generated in separate window whenever user clicks a button.

I want to display graphs, when user clicks button in FinalUI1 class, but when I close any of generated graphs, the whole UI collapses, everything is gone. I want to retain the main UI and shut down the particular graph which user chose to close. It came to my mind that, since UI is on main thread and I can spawn new graphs in new thread, if I do this and close one of child threads, the main UI should still be running.


Additional code:

public class PcapTool extends ApplicationFrame { 
public static String domainChoice, domainConcatenate="";;
public static XYSeries series1;
public static XYSeriesCollection dataset=null;
public static XYSeries series2;
public static PacketInfo resPacketObject;
public static Hashtable<String, Object> DomainNameTable=new Hashtable<String, Object>();
public static String[] hasArray=new String[100];
public static JFreeChart chart;
public static String customTitle = " ";
public  ArrayList<Double> dataNumberList=new ArrayList<Double>(); 
public static String[]dataUsage;
public static String[]timeArrival,txRxTag;
private static final long serialVersionUID = 1L;
public PcapTool(final String title) throws InterruptedException {
    super(title);
    IntervalXYDataset dataset = createDataset();
    JFreeChart chart = createChart(dataset);
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(2000,1000));//(width,height) of display
    setContentPane(chartPanel);
}
public IntervalXYDataset createDataset() throws InterruptedException {
      // add Series 1 and Series 2
    }
    dataset= new XYSeriesCollection(series1);
    dataset.addSeries(series2);
    dataset.setIntervalWidth(0.05);//set width here
    return dataset;
}
private JFreeChart createChart(IntervalXYDataset dataset) {
    final JFreeChart chart = ChartFactory.createXYBarChart(
            "Pcap Analysis Tool\n Domain: "+domainConcatenate,
            "Time (Seconds)", 
            false,
            "Data Usage (bytes)"开发者_如何学C, 
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
            );
    return chart;    
}
public static void main(final String[] args) throws InterruptedException {

    final PcapTool demo = new PcapTool("PCAP Analysis");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);
    System.out.println("domain: "+dropBoxUserValue);
}
}


I'm guessing that this behavior is due to your using JFrames or something similar to display your child windows, and that the JFrame's setDefaultCloseOperation properties have been set to JFrame.EXIT_ON_CLOSE which will cause the JVM to exit when any of the windows close.

I think that you should show them in dialog windows such as a JDialog, not in a JFrame or ApplicationFrame. Also, I have to worry about your use of threading. All Swing code needs to be called on one single thread, the EDT, not separate threads as you may be doing above. Sure, do long-running calculations in a background thread, but the actual display of the chart and any other Swing calls must be on the EDT (unless you know for certain that the calls are thread-safe). The other option is to set the JFrame setDefaultCloseOperation to JFrame.DISPOSE_ON_CLOSE, but still these guys are behaving as dialogs and in my mind, should be shown as dialogs, JDialogs.

If this doesn't help, consider posting a minimal compilable and runnable example that is very small, has no extraneous code unrelated to the problem at hand, and that demonstrates your problem, an SSCCE.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号