Multiple Threads Swing

82 Views Asked by At

I have some issue with my code. I need to monitor CUPS server wich each printer is a thread:

public static void startCupsMonitor() throws Exception {

    for (CupsPrinter cupsPrinter : PrintJobsUtils.getAllPrinters()) {
        new Thread( new PrinterTask( cupsPrinter.getName() ), cupsPrinter.getName() ).start();
    }
}

As you can see, I start a Thread for each element in my List called at PrintJobsUtils.getAllPrinters (). So far, so good.

But I have a Modal (JDialog with setModal seted to true) with a JTable where it will be updated when a new Print Service arrives on CUPS server. So... I created a class who implemented the Runnable's interface and this stuff:

@Override
public void run() {

    CupsPrinter cupsPrinter;
    PrintQueue printQueue = new PrintQueue();

    try {
        cupsPrinter = PrintJobsUtils.getPrinter( "http://"
                                                     + ApplicationUtils.getApplicationProperties().getProperty( "cups.hostname" )
                                                     + ":" + ApplicationUtils.getApplicationProperties().getProperty( "cups.port" )
                                                     + "/printers/"
                                                     + pName.toUpperCase() );


        while (true) {

            SwingUtilities.invokeLater( () -> {
                List<PrintJobAttributes> printJobAttributesList;
                try {
                    printJobAttributesList = PrintJobsUtils.getAllJobDetails( cupsPrinter );
                    if (!printJobAttributesList.isEmpty()) {
                        for (PrintJobAttributes printJobAttributes : printJobAttributesList) {
                            if (printJobAttributes.getJobState() == JobStateEnum.PENDING_HELD) {
                                printQueue.setVisible( true );
                                if (!ApplicationUtils.cotainsId( printJobAttributes, printQueueArrayList )) {
                                    printQueueArrayList.add( printJobAttributes );

                                    for (PrintJobAttributes jobAttributes : printQueueArrayList) {
                                        printQueue.getDefaultTableModel().addRow( new Object[]{jobAttributes.getJobID(),
                                            jobAttributes.getJobName(), jobAttributes.getSize()} );
                                    }

                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } );
            Thread.sleep( 1000 );
        }
    } catch (Exception e) {

    }

But this does not work properly. I expected the jtable filled but I got the error above and duplicated rows. So how can I do this? I tried SwingWorker but I had read this class can not be reused.

EDIT

I solved this issue using SwingWorker. Thanks all

1

There are 1 best solutions below

0
On

Swing cannot able you to bind objects. Then you can create a Thread that contains a Jtable and you manipulate this object, and associate this object of the Jtable of the modal, then it will reflect on modal.

public class ThRefreshTable extends Thread{
    private JProgressBar barraProgresso = null;

// setJtable //run etc }

modal:
 ThRefreshTable th = new ThRefreshTable();
                   th.setJTable(this.table);
th.start();