Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [platform-swt-dev] BusyIndicator.showWhile() doesn't show wai t cursor

You are doing all the work in the UI thread and not allowing the UI to 
run.

Here is an example (loosely based on yours) that shows how to do this.

public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new GridLayout());
        final Text text = new Text(shell, SWT.BORDER);
        text.setLayoutData(new GridData(GridData.FILL_BOTH));
        final Table table = new Table(shell, SWT.BORDER);
        table.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
        TableColumn c1 = new TableColumn(table, SWT.NONE);
        c1.setWidth(100);
        TableColumn c2 = new TableColumn(table, SWT.NONE);
        c2.setWidth(100);
        TableColumn c3 = new TableColumn(table, SWT.NONE);
        c3.setWidth(100);
        table.setLayoutData(new GridData(GridData.FILL_BOTH));
        Button b = new Button(shell, SWT.PUSH);
        b.setText("load");
        b.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                        FileDialog dialog = new FileDialog (shell, 
SWT.OPEN);
                        dialog.setFilterNames (new String [] {"Comma 
Seperated Files", "All Files (*.*)"});
                        dialog.setFilterExtensions (new String [] 
{"*.csv", "*.*"});
            dialog.open();
            String fileName = dialog.getFileName();
            String fullFileName = dialog.getFilterPath() +
                        File.separator + dialog.getFileName(); 
                        if (null == fileName) {
                            System.out.println("No File selected");
                            return;
                        }
                        text.setText(fullFileName);
                        table.removeAll();
                        Runnable loadCSV = new Runnable() {
                                boolean done = false;
                                public void run() {
                                        Thread thread = new Thread(new 
Runnable() {
                                                public void run() {
                                                        for (int i = 0; i 
< 1000; i++) {
 System.out.println("parse file to get strings");
                                                                if 
(display.isDisposed()) return;
 display.syncExec(new Runnable() {
 public void run() {
 if (table.isDisposed()) return;
 Color whiteSmoke = display.getSystemColor(SWT.COLOR_WHITE);
 TableItem item = new TableItem(table, SWT.NONE);
 item.setText(new String[] {"asadas", "wqeqweqwe", "ertetretete" });
 int index = table.indexOf(item);
 if (1 == index % 2) { 
        item.setBackground(whiteSmoke);
 }
                                                                        }
                                                                });
                                                        }
                                                        done = true;
                                                        display.wake();
                                                }
                                        });
                                        thread.start();
                                        while (!done && 
!shell.isDisposed()) {
                                                if 
(!display.readAndDispatch())
                                                        display.sleep();
                                        }
                                }
                        };
                        BusyIndicator.showWhile(null, loadCSV);
                        System.out.println("File selected: " + 
fullFileName);
                }
        });

        shell.open();
        while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                        display.sleep();
        }
        display.dispose();
}


Back to the top