In Netbeans, I am trying to create a Desktop Application whose UI looks like below:
I am executing "adb logcat command" through Java code which loads 1000s of lines of logs in few seconds & I intend to display all of this information through jTable in NetBeans.
Using parameter: adb logcat -t 100 -> I am restricting the logs to 100 lines only right now. However the applet becomes unresponsive (or gets stuck in process() method) for 1000 lines or when removing such restriction on number of lines.
I am not sure whether I have properly implemented the SwingWorker thread in my code. I am looking for suggestions on how to improve the code for loading large amount of data dynamically without applet becoming unresponsive.
Following is the implemented code for the applet... with 2 functions:
- viewLogs() called from init() method of applet.
SwingWorker implementation.
public void viewLogs() throws IOException { String[] command = {"CMD","/C", "adb logcat -t 100"}; ProcessBuilder probuilder = new ProcessBuilder( command ); probuilder.directory(new File("c:\\Users\\k.garg\\Desktop\\")); Process process = probuilder.start(); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); br = new BufferedReader(isr); DefaultTableModel model = (DefaultTableModel)jTable1.getModel(); worker.execute(); try { int exitVal = process.waitFor(); System.out.println("exitVal = " + exitVal); } catch (InterruptedException e) { e.printStackTrace(); } } public class TableSwingWorker extends SwingWorker<DefaultTableModel, Object[]>{ private DefaultTableModel tableModel; public TableSwingWorker(DefaultTableModel tableModel){ this.tableModel = tableModel; } @Override protected DefaultTableModel doInBackground() throws Exception { Thread.sleep(2000); //added for initial UI to load System.out.println("Start populating"); String line, date, time, loglevel, PID, TID, tag, message=""; String log; int count = 0; while ((log = br.readLine()) != null) { count++; String[] splitLog = log.trim().split("\\s+"); line = Integer.toString(count); date = splitLog[0]; time = splitLog[1]; PID = splitLog[2]; TID = splitLog[3]; loglevel = splitLog[4]; tag = splitLog[5]; for(int i=6; i<splitLog.length;i++){ message += splitLog[i]; } publish(new Object[]{line, date, time, PID, TID, loglevel, tag, message}); } return tableModel; } @Override protected void process(List<Object[]> chunks) { System.out.println("Adding " + chunks.size() + " rows"); for(Object[] row: chunks) tableModel.insertRow(0,row); }
}