I\'ve created a simple Java application that each second for for 10 seconds consecutive seconds adds a new row to a JTable
. It consists of three classes.
After doing it, I'm not fully convinced a SwingWorker is a simpler (aka: better) solution - still requires additional thread synching (between the worker thread and the "outer" thread which passes in the file/names). Anyway (taking the opportunity to learn, and be it by errors :), below is a crude proof of concept example for the basic idea:
open issues
Feedback welcome :-)
public class GUI {
private JFrame frame = new JFrame();
private DefaultTableModel model = new DefaultTableModel();
private JTable table = new JTable(model);
private JScrollPane pane = new JScrollPane(table);
public GUI() {
model.addColumn("Name");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
public void addRow(String name) {
model.addRow(new Object[] { name });
}
/**
* Controller is a SwingWorker.
*/
public static class Controller extends SwingWorker<Void, String> {
private GUI gui;
private List<String> pending;
public Controller() {
gui = new GUI();
}
public void doWork(String newLine) {
if (pending == null) {
pending = new ArrayList<String>();
pending.add(newLine);
execute();
} else {
pending.add(newLine);
}
}
@Override
protected Void doInBackground() throws Exception {
while (pending.size() > 0) {
publish(pending.remove(0));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
/**
* @inherited <p>
*/
@Override
protected void process(List<String> chunks) {
for (String object : chunks) {
gui.addRow(object);
}
}
}
/**
* Simulating the adapter.
*
* Obviously, the real-thingy wouldn't have a reference
* to the controller, but message the doWork refectively
*/
public static class Adapter implements Runnable {
Controller controller;
public Adapter(Controller controller) {
this.controller = controller;
}
@Override
public void run() {
for (int i=0; i<10; i++)
{
controller.doWork("Line "+(i+1));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args)
{
System.err.println("Initializing controller");
new Adapter(new Controller()).run();
}
@SuppressWarnings("unused")
private static final Logger LOG = Logger.getLogger(GUI.class.getName());
}
It looks like you're blocking the event dispatch thread(EDT). SwingWorker would be a better choice, but this example implements Runnable
.
Addendum: You might look at this project for an example of MVC architecture. It also shows how to construct a Mac OS application bundle without using JAR Bundler. More on MVC may be found here.
As an aside, this example shows one approach to auto-scrolling a JTable
. Click on the thumb to suspend scrolling; release to resume.
Addendum: Your application lags for 10 seconds on startup. As this is the exact time for which the Controller
sleeps, it's surely sleeping on the EDT. An sscce would be dispositive. Instead, do the work on another thread and update the model on the EDT. SwingWorker
has a process()
method that does so automatically, or you can use invokeLater()
as shown below. Until your application is correctly synchronized, there's little hope of getting Apple events to work.
Addendum: You can invoke isDispatchThread() in the Controller
to check. The project cited includes a .dmg
with a Mac application and an ant
file that builds the bundle in situ via target dist2
.
Addendum: See also the alternate approaches shown here.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/** @seehttps://stackoverflow.com/questions/7519244 */
public class TableAddTest extends JPanel implements Runnable {
private static final int N_ROWS = 8;
private static String[] header = {"ID", "String", "Number", "Boolean"};
private DefaultTableModel dtm = new DefaultTableModel(null, header) {
@Override
public Class<?> getColumnClass(int col) {
return getValueAt(0, col).getClass();
}
};
private JTable table = new JTable(dtm);
private JScrollPane scrollPane = new JScrollPane(table);
private JScrollBar vScroll = scrollPane.getVerticalScrollBar();
private JProgressBar jpb = new JProgressBar();
private int row;
private boolean isAutoScroll;
public TableAddTest() {
this.setLayout(new BorderLayout());
jpb.setIndeterminate(true);
this.add(jpb, BorderLayout.NORTH);
Dimension d = new Dimension(320, N_ROWS * table.getRowHeight());
table.setPreferredScrollableViewportSize(d);
for (int i = 0; i < N_ROWS; i++) {
addRow();
}
scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
vScroll.addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
isAutoScroll = !e.getValueIsAdjusting();
}
});
this.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.add(new JButton(new AbstractAction("Add Row") {
@Override
public void actionPerformed(ActionEvent e) {
addRow();
}
}));
this.add(panel, BorderLayout.SOUTH);
}
private void addRow() {
char c = (char) ('A' + row++ % 26);
dtm.addRow(new Object[]{
Character.valueOf(c),
String.valueOf(c) + String.valueOf(row),
Integer.valueOf(row),
Boolean.valueOf(row % 2 == 0)
});
}
private void scrollToLast() {
if (isAutoScroll) {
int last = table.getModel().getRowCount() - 1;
Rectangle r = table.getCellRect(last, 0, true);
table.scrollRectToVisible(r);
}
}
@Override
public void run() {
while (true) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
addRow();
}
});
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
scrollToLast();
}
});
try {
Thread.sleep(1000); // simulate latency
} catch (InterruptedException ex) {
System.err.println(ex);
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableAddTest nlt = new TableAddTest();
f.add(nlt);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
new Thread(nlt).start();
}
});
}
}
Here's a variation of @kleopatra's example in which a continuously running Controller
accepts new entries in doWork()
, while a SwingWorker
processes the pending
entries asynchronously in its background thread. ArrayBlockingQueue handles the synchronization.
import java.awt.EventQueue;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;
public class GUI {
private static final Random rnd = new Random();
private JFrame frame = new JFrame();
private DefaultTableModel model = new DefaultTableModel();
private JTable table = new JTable(model);
private JScrollPane pane = new JScrollPane(table);
public GUI() {
model.addColumn("Name");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
public void addRow(String name) {
model.addRow(new Object[]{name});
}
/**
* Controller is a SwingWorker.
*/
private static class Controller extends SwingWorker<Void, String> {
private static final int MAX = 5;
private GUI gui;
private BlockingQueue<String> pending =
new ArrayBlockingQueue<String>(MAX);
public Controller() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
gui = new GUI();
}
});
}
private void doWork(String newLine) {
try {
pending.put(newLine);
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
}
@Override
protected Void doInBackground() throws Exception {
while (true) {
// may block if nothing pending
publish(pending.take());
try {
Thread.sleep(rnd.nextInt(500)); // simulate latency
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
}
}
@Override
protected void process(List<String> chunks) {
for (String object : chunks) {
gui.addRow(object);
}
}
}
/**
* Exercise the Controller.
*/
private static class Adapter implements Runnable {
private Controller controller;
private Adapter(Controller controller) {
this.controller = controller;
}
@Override
public void run() {
controller.execute();
int i = 0;
while (true) {
// may block if Controller busy
controller.doWork("Line " + (++i));
try {
Thread.sleep(rnd.nextInt(500)); // simulate latency
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
}
}
}
public static void main(String[] args) {
System.out.println("Initializing controller");
// Could run on inital thread via
// new Adapter(new Controller()).run();
// but we'll start a new one
new Thread(new Adapter(new Controller())).start();
}
}