SWT/Display.sleep()

K

Kevin Pors

Hi

Consider the following code:

// Main.java

public class Main {
public static void main(String[] args) {
System.out.println("Number 1");
SWTView view = new SWTView();
System.out.println("Number 2");
}
}

// SWTView.java

public class SWTView {
public SWTView() {
 
S

Shripathi Kamath

Kevin Pors said:
Hi

Consider the following code:

// Main.java

public class Main {
public static void main(String[] args) {
System.out.println("Number 1");
SWTView view = new SWTView();
System.out.println("Number 2");
}
}

// SWTView.java

public class SWTView {
public SWTView() {
.
.
// add display, shell etc to make a simple
// hello world application
.
.
while (!shell.isDisposed())
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

After executing the main() method, it creates a SWTView object after
printing out the text "Number 1". The app will not print out "Number
2" until the window has been closed. How can I keep the window open,
with still being able to execute statements in the main() method?!

I am asking this because my application has to exec a
setConnected(true) method through the SWTView instance. But this can't
be done until I closed the window.

Help is very, very appreciated.

Any reason why you do not use a separate thread?
 
K

Kevin Pors

Shripathi Kamath said:
Any reason why you do not use a separate thread?

I tried the following using threads:

// Main.java

public class Main implements Runnable {

Thread t;
SWTView view;

public static void main(String[] args) {
t = new Thread(this);
t.start();
view.getConnected(); // Exception here (nullpointer) which I
understand
}

public void run() {
view = new SWTView();
// view.getConnected(); // this should generate no errors
// but i do not want to manage things in here
}
}

Second thing I thought of was putting the while() { } loop from
SWTView.java
into a thread in SWTView.java, but it generated an thread exception
from within SWT.

The main thing I try to achieve, is AWT like behaviour - the program
is not closed until the window is closed, and I can access the SWTView
instance like a JFrame or Frame.
 
S

Shripathi Kamath

Kevin Pors said:
"Shripathi Kamath" <[email protected]> wrote in message
Any reason why you do not use a separate thread?

I tried the following using threads:

// Main.java

public class Main implements Runnable {

Thread t;
SWTView view;

public static void main(String[] args) {
t = new Thread(this);

In a static method, you are using the 'this' reference. What do you believe
it means/

t.start();
view.getConnected(); // Exception here (nullpointer) which I
understand
}

public void run() {
view = new SWTView();
// view.getConnected(); // this should generate no errors
// but i do not want to manage things in here

Why not? Why not do the SWT function as part of the main thread, and all
your other processing in another thread?

}
}

Second thing I thought of was putting the while() { } loop from
SWTView.java
into a thread in SWTView.java, but it generated an thread exception
from within SWT.

The main thing I try to achieve, is AWT like behaviour - the program
is not closed until the window is closed, and I can access the SWTView
instance like a JFrame or Frame.

That is unclear, your original issue seemed to be that you until the window
was closed, you could not get to the line of code past the SWT loop, and
here you are asking for something different.

Please clarify.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top