Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2006

Tip: Looking for answers? Try searching our database.

Embedding SWING/JPanel in SWT

Thread view: 
qetoom@gmail.com - 31 Oct 2006 17:14 GMT
Hi all,
I'm trying to embed a a data plotting class in a simple SWT
application, the calss is driven from JPanel. The code generates
nothing btut an empty window, I tried the same thing with a simple
JPanel holding a couple of controls and the result is always an empty
window.
here is the code of the secound attempt:

public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setText("Plot2d in SWT app");
        Composite compos = new Composite(shell, SWT.EMBEDDED);
        java.awt.Frame frame = SWT_AWT.new_Frame(compos);
        javax.swing.JPanel panel = new javax.swing.JPanel( );
        javax.swing.JLabel label = new javax.swing.JLabel("hi");
        final javax.swing.JButton button = new javax.swing.JButton("Post
Message");
        frame.add(panel);
        panel.add(label);
        panel.add(button);

        shell.open();
        while(!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}
Ian Wilson - 31 Oct 2006 17:39 GMT
> Hi all,
> I'm trying to embed a a data plotting class in a simple SWT
> application, the calss is driven from JPanel. The code generates
> nothing btut an empty window, I tried the same thing with a simple
> JPanel holding a couple of controls and the result is always an empty
> window.

Can you usefully mix SWT and Swing? I thought this was not possible?

>  here is the code of the secound attempt:
>
> public static void main(String[] args) {

>         final Display display = new Display();
>         final Shell shell = new Shell(display);
>         shell.setText("Plot2d in SWT app");
>         Composite compos = new Composite(shell, SWT.EMBEDDED);

I don't see why you use SWT above, why not use the Swing equivalents?

>         java.awt.Frame frame = SWT_AWT.new_Frame(compos);

Aren't you now mixing in AWT? Why not use JFrame?

>         javax.swing.JPanel panel = new javax.swing.JPanel( );
>         javax.swing.JLabel label = new javax.swing.JLabel("hi");
[quoted text clipped - 3 lines]
>         panel.add(label);
>         panel.add(button);

>         shell.open();
>         while(!shell.isDisposed()) {
[quoted text clipped - 3 lines]
>     }
> }

If it was a JFrame wouldn't you need to
  frame.pack();
  frame.setVisible(true);
instead of all this SWTish code.

Swing components normally need to be instatiated in the event
dispatching thread (e.g. using SwingUtilities.invokeLater()).

Is there some reason you don't write this as a Swing app if you want to
doodle on a JPanel?

Doubtless there is a SWT component that can be drawn on if you want a
SWT app - isn't there?

Maybe you are doing something clever, too clever for my level of knowledge?
qetoom@gmail.com - 31 Oct 2006 18:03 GMT
Here is how others have been doing it.
http://eclipsezone.com/eclipse/forums/t45697.html
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclips
e/swt/snippets/Snippet154.java?rev=HEAD&content-type=text/vnd.viewcvs-markup


I need to use a small number of data visualization calsses that are
written using swing in applications written in swt. Rewriting either is
not an option :).

> > Hi all,
> > I'm trying to embed a a data plotting class in a simple SWT
[quoted text clipped - 8 lines]
> >
>..........


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.