views:

62

answers:

4

Good Mourning everybody

I have a main screen, with follows keybindings:

shell.getDisplay().addFilter(SWT.KeyUp, new Listener() {
    @Override
    public void handleEvent(Event event) {
        switch (event.keyCode) {
        case SWT.ESC:
            shell.close();
            break;
        case SWT.F12:
            display.syncExec(new Runnable() {
                @Override
                public void run() {
                new Venda(shell);
                }
            });                 
            break;                  
        default:
            break;
        }               
    }
});

When push F12, search's screen is opened. Constructor of sale screen:

public Venda(Shell parent) {
        super(parent);
        this.shell = new Shell(getParent(),SWT.APPLICATION_MODAL);
        this.display = getParent().getDisplay();
        this.shell.setMaximized(true);
        this.shell.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN));
        this.fontText = new Font(shell.getDisplay(), new FontData("Arial", 28, SWT.NORMAL));
        this.fontLabel = new Font(shell.getDisplay(), new FontData("Arial", 13, SWT.NORMAL));
        this.shell.setText("Cupom Fiscal - Venda Produto");
        this.criaCampos();
        this.configuraTeclaAtalho();
        this.shell.open();
        while (!display.isDisposed()) {
            if (!display.isDisposed() && !display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

When push F3 on sale screen, the search screen is opened. My problem is: When sale screen is opened the first time, the search screen works normally, but if sale screen is closed and opened again, the search screen didn´t works, casting the error: Widget is disposed. The error happen on line 02, in the source code follows. The variable "abreFechaCaixa" verify if sale screen should be open.

    if(!abreFechaCaixa){
        MessageBox msg = new MessageBox(shell, SWT.ICON_WARNING | SWT.YES | SWT.NO);
        msg.setMessage("Caixa Fechado, deseja abrir?");
    msg.setText(shell.getText());
    if(msg.open() == SWT.YES){
        abreCaixa();
    }
    }
if(abreFechaCaixa){
    display.syncExec(new Runnable() {
        @Override
    public void run() {
                new Consulta(shell,"Cupom Fiscal - Consulta Produto");
    }
    });
}

Constructor's search screen:

public Consulta(Shell parent) {
                super(parent);
        this.shell = new Shell(parent, SWT.APPLICATION_MODAL);
        this.display = getParent().getDisplay();
        this.shell.setText(tituloTela);
        this.shell.setLayout(new GridLayout(1, false));
        this.fontText = new Font(shell.getDisplay(), new FontData("Arial", 28, SWT.NORMAL));
        this.fontLabel = new Font(shell.getDisplay(), new FontData("Arial", 13, SWT.NORMAL));
        this.criaCampos();
        this.shell.pack();
        this.centralizaTela();
        this.shell.open();
        while (!shell.isDisposed()) {
            if (!display.isDisposed() && !display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

Can you help me solve this problem? Or show the best way to close windows in SWT? Thanks!

A: 

I think you need to add "display.dispose();" after you while loop for !shell.isDisposed(). Like following:

 while (!shell.isDisposed()) {
    if (!display.readAndDispatch()){
       display.sleep();
    }
  }
 display.dispose ();
YoK
A: 

I tested your example and didn´t work, same problem.

w1l14n
To resolve my problem, I separated the process in threads.
w1l14n
A: 

To resolve my problem, I separated the process in threads.

w1l14n
A: 

You do not need the while loop in the Consulta class, as the Shell is a child of Venda. This means that the display object of the child shell is the same as its parent; in your construction, running readAndDispatch() over this display is therefore handled twice.

Paul Lammertsma