Components embedded in the SWT in swing

Source: Internet
Author: User

http://zhanghan3366.blog.163.com/blog/static/6966305220110505351568/

————————————————————————————————————————————————————————————————————————————

First with the environment, go to the Eclipse home page up and down a SWT package, WWW.ECLIPSE.ORT/SWT. There is a swt.jar in the compressed package, and several DLL files.

There is a class called swt_awt, the idea of SWT_AWT is simply to use a AWT canvas to build a shell, and then you can add SWT to the shell of things, as for the canvas where it doesn't matter, perhaps in a separate window, It can also be part of a large window. I found a piece of code written by Japs on the internet to illustrate the approximate usage of this class:
JFrame frame = new JFrame ();
Container CP = Frame.getcontentpane ();
Canvas canvas = new canvas ();
Cp.add (Canvas,borderlayout.center);
Frame.setvisible (TRUE);

Display display = new display ();
Shell shell = Swt_awt.new_shell (Display,canvas);
Shell.setlayout (New Filllayout ());

Button button = New button (SHELL,SWT. PUSH);
Button.settext ("swtのボタン");

Shell.pack ();

while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) {
Display.sleep ();
}
}
The back of the while loop is important, and with this loop, the shell's display is constantly responding to events, and he will sleep when there is no event. But the problem is that while he's consuming your current thread, you can't do any other work, and the original AWT section is no longer responding to events.

The most straightforward solution is to put this code in a separate thread, which is a piece of code found in the Eclipse bug report:

Private class Displaythread extends Thread {
private display display;

public void Run () {
display = Display.getdefault ();
Swteventloop ();
}

private void Swteventloop () {
while (true) {
if (!display.readanddispatch ()) {
Display.sleep ();
}
}
}

Public Display Getdisplay () {
return display;
}
}


It also gives a panel class, which I've slightly modified above, which is the final panel that contains a shell. You can add all kinds of swt to his inner shell, or you can insert the entire class as a panel into any AWT section. It is also worth noting that the panel is used here instead of the JPanel, as explained by the eclipse documentation, "It is strongly recommended to use a heavyweight component as the root control."

public class Swtpane extends Panel {
Displaythread Displaythread;
Private Canvas canvas;

Public Swtpane () {
Displaythread=new Displaythread ();
Displaythread.start ();
Canvas = new canvas ();
SetLayout (New BorderLayout ());
Add (canvas, borderlayout.center);
}

public void Addnotify () {
Super.addnotify ();
Display Dis=displaythread.getdisplay ();
Dis.syncexec (New Runnable () {
public void Run () {
Shell shell = Swt_awt.new_shell (Displaythread.getdisplay (), canvas);
Shell.setlayout (New Filllayout ());
Final Browser Browser = new Browser (Shell, SWT. NONE);
Browser.setlayoutdata (Borderlayout.center);
Browser.seturl ("Http://blog.csdn.net/fafey");
}
} );
}
}

One of the functions of dis.syncexec is to allow display to find a suitable time to execute the code provided later, based on the circumstances of the thread.
But this code also has a problem, run-time in this place throws NullPointerException

I think it may be because the thread has not been established before, the first call Getdisplay (), returned a null, no way, add synchronization mechanism in the thread!

public class Displaythread extends Thread {
 private display display;
 object sem=new Object ();
 
 public void Run () {
  synchronized (SEM) {
   display = Display.getdefault ();
   sem.notifyall ();
  }
  swteventloop ();
 }
 
 private void Swteventloop () {
  while (true) {
   if (! Display.readanddispatch ()) {
    display.sleep ();
   }
  }
 }
 
 public Display Getdisplay () {
   try{
  synchronized (SEM) {
   while (display==null)
     Sem.wait ();
   return display;
  }
  }
  catch (Exception e) {
   return null;
  }
 }

}

This class can be used together with the upper one.

Components embedded in the SWT in swing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.