Preventing the main user interface from crashing from the child thread

I ran into this problem, I have

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 a separate window whenever the user clicks a button.

Problem: I want to display graphs when the user clicks a button in the FinalUI1 class, but when I close any of the generated graphs, the entire user interface crashes, everything disappears. I want to save the main interface and close the specific chart that the user decided to close. It occurred to me that since the user interface is in the main thread, and I can create new charts in the new thread, if I do this and close one of the child threads, the main user interface should work. Could you help me with this.

Thank you very much.


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)", 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); } 

}

+4
source share
1 answer

I assume that this behavior is due to the use of JFrames or something similar to displaying child windows and that the JFrame properties of setDefaultCloseOperation setDefaultCloseOperation set to JFrame.EXIT_ON_CLOSE , which will cause the JVM to shut down if any of the windows close.

I think you should show them in dialog boxes like JDialog, and not in JFrame or ApplicationFrame. In addition, I have to worry about how you use streams. All Swing code should be called by a single thread, EDT, and not by separate threads, as you can do above. Of course, do lengthy calculations in the background thread, but the actual display of the diagram and any other Swing calls should be on the EDT (unless you know for sure that the calls are thread safe). Another option is to set the JFrame setDefaultCloseOperation to JFrame.DISPOSE_ON_CLOSE , but still these guys behave like dialogs and, in my opinion, should display like dialogs, JDialogs.

If this does not help, consider hosting a minimal compiled and executable example that is very small, has no extraneous code that is not related to the problem, and this demonstrates your problem, SSCCE .

+2
source

All Articles