hi,
I am using swing worker to update my GUI in edt. I have the code below. when I run the code without the while loop it works,
whereas when I run with the while loop it falis , why is it so ?
import java.awt.BorderLayout; import java.awt.EventQueue; import java.util.List; import java.util.Scanner; import java.util.Vector; import java.util.concurrent.ExecutionException; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.SwingWorker; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; public class App { static JFrame frame = new JFrame("Random Data Demo"); static JTable table = new JTable(); static DefaultTableModel tableModel = new DefaultTableModel(); static Scanner sc=new Scanner(System.in); public static void main(String[] args) throws Exception { EventQueue.invokeLater(new Runnable() { @Override public void run() { while(true) ////// while loop --- { System.out.println("Write message here: "); String message = sc.nextLine(); System.out.println("Message is: "+message); Swingwor mainWorker = new Swingwor(table,message); mainWorker.execute(); init(); } } }); } private static void init() { //execTask(); // JFrame frame = new JFrame("Random Data Demo"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setSize(600,600); // DefaultTableModel model = new DefaultTableModel(); //JTable table = new JTable(model); table.setModel(tableModel); table.repaint(); JScrollPane scrollPane = new JScrollPane(table); JPanel panel = new JPanel(new BorderLayout()); //Swingworker mainWorker = new Swingworker(table); // mainWorker.execute(); panel.add(table.getTableHeader(), BorderLayout.PAGE_START); panel.add(scrollPane, BorderLayout.CENTER); //getContentPane().add(panel); frame.add(panel); frame.setVisible(true); } // private static void execTask() // { // Swingwor mainWorker = new Swingwor(table); // mainWorker.execute(); // } } class Swingwor extends SwingWorker<TableModel,TableModel> { Object data[][]=null; private final JTable table; int i,r; String p; String message=null; Object col[] = {"Sensor","Values","Arrival_Time","Status",""}; public Swingwor(JTable table, String message) { this.table = table; this.message = message; } @Override protected TableModel doInBackground() throws Exception { if(!message.equals("ITS EMPTY")) { String[] parts = message.split("\t"); data = new Object[parts.length / 4][5]; for (i = 0, r = 0; r < data.length; r++) { data[r][0] = parts[i++]; //data[r][1] = new Double(parts[i++]) ; data[r][1] = parts[i++] ; data[r][2] = parts[i++]; data[r][3] = parts[i++]; p=(String) data[r][3]; // w = (String) abc(p); data[r][4]=Boolean.parseBoolean(p) ; System.out.println(data[r][4]); } //System.out.println(java.util.Arrays.deepToString(data)); //Display Table } DefaultTableModel tableModel = new DefaultTableModel(data, col); tableModel.setDataVector(data, col); return tableModel; } @Override protected void done() { try { TableModel model = get(); table.setModel(model); } catch (InterruptedException | ExecutionException ex) { ex.printStackTrace(); } } }