Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » AWT_SWT Adding Hidden Components
AWT_SWT Adding Hidden Components [message #447852] Sun, 19 December 2004 18:12 Go to next message
Eclipse UserFriend
Originally posted by: still_aimless.wp.pl

I just stumbled upon a road block in my venture into SWT AWT land. The
application I'm working on requires that some of the Swing/AWT controls
(JButtons, etc) in the AWT panel have to be initially hidden and made
visible later at runtime. The problem is simple: I cannot unhide any of the
components, which were created and made invisible before the main window
was realized. I have tried everything from forcing repaint on individual
controls, invalidating areas, calling paint(..) directly, repainting the
AWT frame, repainting the SWT composite holding the frame ... nothing
worked. The controls are still not showing, even though they do have the
visible bit set to high. The only way to get them to show up is to manually
resize the window, in which they reside - obviously that's not a solution
I'm looking for.
What's interesting that after they are shown for the first time ( via the
window resize trick ) I can hide/show them at will, and from that point on
they behave as expected. It's only the first setVisible(..) call that
fails. Honestly it looks like SWT/AWT is not honoring the existance of
these components until a full flush of the window contents (like manual
resize).

I'm using JDK 5.0 and SWT 3.0 on Windows XP SP2. The AWT Frame has an AWT
Panel a child, which houses the controls that are initially hidden.

Example code.

The button at the bottom cycles the visibility of the center button,
however the first click, which should make the center button appear does
nothing. At this point if you resize the window, the center button will
appear as it should have instantly after the click event. Now, clicking the
bottom button cycles correctly the visibility state of the center button.


/******************************************************/
import org.eclipse.swt.layout.*;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.*;
import javax.swing.JButton;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.*;


class Snippet {

static boolean state = false;

public static void main(String[] args) {

final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Composite composite = new Composite(shell, SWT.EMBEDDED);

Frame frame = SWT_AWT.new_Frame(composite);
Panel panel = new Panel(new BorderLayout());
frame.add(panel);

final JButton invisibleButton = new JButton("I am invisible");
invisibleButton.setVisible(state);
panel.add(invisibleButton, BorderLayout.CENTER);

final JButton button = new JButton("Make my friend visible");
button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

state = !state;
invisibleButton.setVisible(state);
button.setText("Make my friend " + (state ?
"invisible" : "visible"));
}
});
panel.add(button, BorderLayout.SOUTH);

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


-----

- bartek
Re: AWT_SWT Adding Hidden Components [message #447877 is a reply to message #447852] Mon, 20 December 2004 18:33 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
I believe that this has nothing to do with SWT. Here is your code hacked to
use AWT/Swing only. It seems to have the same problem.

/******************************************************/
import org.eclipse.swt.layout.*;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.*;
import javax.swing.JButton;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.*;

class Snippet {
static boolean state = false;
public static void main(String[] args) {

final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Composite composite = new Composite(shell, SWT.EMBEDDED);

//Frame frame = SWT_AWT.new_Frame(composite);
Frame frame = new Frame();
frame.setVisible(true);
Panel panel = new Panel(new BorderLayout());
frame.add(panel);

final JButton invisibleButton = new JButton("I am invisible");
invisibleButton.setVisible(state);
panel.add(invisibleButton, BorderLayout.CENTER);

final JButton button = new JButton("Make my friend visible");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
state = !state;
invisibleButton.setVisible(state);
button.setText("Make my friend "
+ (state ? "invisible" : "visible"));
}
});

panel.add(button, BorderLayout.SOUTH);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
/** *************************************************** */

"Bartosz Frak" <still_aimless@wp.pl> wrote in message
news:Xns95C4C38116D4Bstillaimlesswppl@206.191.52.34...
> I just stumbled upon a road block in my venture into SWT AWT land. The
> application I'm working on requires that some of the Swing/AWT controls
> (JButtons, etc) in the AWT panel have to be initially hidden and made
> visible later at runtime. The problem is simple: I cannot unhide any of
the
> components, which were created and made invisible before the main window
> was realized. I have tried everything from forcing repaint on individual
> controls, invalidating areas, calling paint(..) directly, repainting the
> AWT frame, repainting the SWT composite holding the frame ... nothing
> worked. The controls are still not showing, even though they do have the
> visible bit set to high. The only way to get them to show up is to
manually
> resize the window, in which they reside - obviously that's not a solution
> I'm looking for.
> What's interesting that after they are shown for the first time ( via the
> window resize trick ) I can hide/show them at will, and from that point on
> they behave as expected. It's only the first setVisible(..) call that
> fails. Honestly it looks like SWT/AWT is not honoring the existance of
> these components until a full flush of the window contents (like manual
> resize).
>
> I'm using JDK 5.0 and SWT 3.0 on Windows XP SP2. The AWT Frame has an AWT
> Panel a child, which houses the controls that are initially hidden.
>
> Example code.
>
> The button at the bottom cycles the visibility of the center button,
> however the first click, which should make the center button appear does
> nothing. At this point if you resize the window, the center button will
> appear as it should have instantly after the click event. Now, clicking
the
> bottom button cycles correctly the visibility state of the center button.
>
>
> /******************************************************/
> import org.eclipse.swt.layout.*;
> import java.awt.BorderLayout;
> import java.awt.Frame;
> import java.awt.Panel;
> import java.awt.event.*;
> import javax.swing.JButton;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.awt.SWT_AWT;
> import org.eclipse.swt.widgets.*;
>
>
> class Snippet {
>
> static boolean state = false;
>
> public static void main(String[] args) {
>
> final Display display = new Display();
> final Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
> Composite composite = new Composite(shell, SWT.EMBEDDED);
>
> Frame frame = SWT_AWT.new_Frame(composite);
> Panel panel = new Panel(new BorderLayout());
> frame.add(panel);
>
> final JButton invisibleButton = new JButton("I am invisible");
> invisibleButton.setVisible(state);
> panel.add(invisibleButton, BorderLayout.CENTER);
>
> final JButton button = new JButton("Make my friend visible");
> button.addActionListener(new ActionListener() {
>
> public void actionPerformed(ActionEvent e) {
>
> state = !state;
> invisibleButton.setVisible(state);
> button.setText("Make my friend " + (state ?
> "invisible" : "visible"));
> }
> });
> panel.add(button, BorderLayout.SOUTH);
>
> shell.open();
> while(!shell.isDisposed()) {
> if (!display.readAndDispatch()) display.sleep();
> }
> display.dispose();
> }
> }
> /******************************************************/
>
>
> -----
>
> - bartek
Re: AWT_SWT Adding Hidden Components [message #447879 is a reply to message #447877] Mon, 20 December 2004 20:07 Go to previous messageGo to next message
Jim Adams is currently offline Jim AdamsFriend
Messages: 160
Registered: July 2009
Senior Member
It should be noted that you can't really use Swing components without them
being in a JRootPane. The SWT_AWT frame returned is not a JRootPane. This
will cause you all sorts of refresh errors. Note that Javasoft did not
provide an implementation of JRootPane that you can derive from so you will
have to make your own. We have found that, once you parent from a
JRootPaneContainer that life is MUCH better. Maybe this is something that we
can get the SWT guys to provide? It really is not nice to have to re-invent
this wheel over and over...


"Steve Northover" <steve_northover@ca.ibm.com> wrote in message
news:cq75tg$p7c$1@www.eclipse.org...
> I believe that this has nothing to do with SWT. Here is your code hacked
to
> use AWT/Swing only. It seems to have the same problem.
>
> /******************************************************/
> import org.eclipse.swt.layout.*;
> import java.awt.BorderLayout;
> import java.awt.Frame;
> import java.awt.Panel;
> import java.awt.event.*;
> import javax.swing.JButton;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.awt.SWT_AWT;
> import org.eclipse.swt.widgets.*;
>
> class Snippet {
> static boolean state = false;
> public static void main(String[] args) {
>
> final Display display = new Display();
> final Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
> Composite composite = new Composite(shell, SWT.EMBEDDED);
>
> //Frame frame = SWT_AWT.new_Frame(composite);
> Frame frame = new Frame();
> frame.setVisible(true);
> Panel panel = new Panel(new BorderLayout());
> frame.add(panel);
>
> final JButton invisibleButton = new JButton("I am invisible");
> invisibleButton.setVisible(state);
> panel.add(invisibleButton, BorderLayout.CENTER);
>
> final JButton button = new JButton("Make my friend visible");
> button.addActionListener(new ActionListener() {
> public void actionPerformed(ActionEvent e) {
> state = !state;
> invisibleButton.setVisible(state);
> button.setText("Make my friend "
> + (state ? "invisible" : "visible"));
> }
> });
>
> panel.add(button, BorderLayout.SOUTH);
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
> }
> /** *************************************************** */
>
> "Bartosz Frak" <still_aimless@wp.pl> wrote in message
> news:Xns95C4C38116D4Bstillaimlesswppl@206.191.52.34...
> > I just stumbled upon a road block in my venture into SWT AWT land. The
> > application I'm working on requires that some of the Swing/AWT controls
> > (JButtons, etc) in the AWT panel have to be initially hidden and made
> > visible later at runtime. The problem is simple: I cannot unhide any of
> the
> > components, which were created and made invisible before the main window
> > was realized. I have tried everything from forcing repaint on individual
> > controls, invalidating areas, calling paint(..) directly, repainting the
> > AWT frame, repainting the SWT composite holding the frame ... nothing
> > worked. The controls are still not showing, even though they do have the
> > visible bit set to high. The only way to get them to show up is to
> manually
> > resize the window, in which they reside - obviously that's not a
solution
> > I'm looking for.
> > What's interesting that after they are shown for the first time ( via
the
> > window resize trick ) I can hide/show them at will, and from that point
on
> > they behave as expected. It's only the first setVisible(..) call that
> > fails. Honestly it looks like SWT/AWT is not honoring the existance of
> > these components until a full flush of the window contents (like manual
> > resize).
> >
> > I'm using JDK 5.0 and SWT 3.0 on Windows XP SP2. The AWT Frame has an
AWT
> > Panel a child, which houses the controls that are initially hidden.
> >
> > Example code.
> >
> > The button at the bottom cycles the visibility of the center button,
> > however the first click, which should make the center button appear does
> > nothing. At this point if you resize the window, the center button will
> > appear as it should have instantly after the click event. Now, clicking
> the
> > bottom button cycles correctly the visibility state of the center
button.
> >
> >
> > /******************************************************/
> > import org.eclipse.swt.layout.*;
> > import java.awt.BorderLayout;
> > import java.awt.Frame;
> > import java.awt.Panel;
> > import java.awt.event.*;
> > import javax.swing.JButton;
> > import org.eclipse.swt.SWT;
> > import org.eclipse.swt.awt.SWT_AWT;
> > import org.eclipse.swt.widgets.*;
> >
> >
> > class Snippet {
> >
> > static boolean state = false;
> >
> > public static void main(String[] args) {
> >
> > final Display display = new Display();
> > final Shell shell = new Shell(display);
> > shell.setLayout(new FillLayout());
> > Composite composite = new Composite(shell, SWT.EMBEDDED);
> >
> > Frame frame = SWT_AWT.new_Frame(composite);
> > Panel panel = new Panel(new BorderLayout());
> > frame.add(panel);
> >
> > final JButton invisibleButton = new JButton("I am invisible");
> > invisibleButton.setVisible(state);
> > panel.add(invisibleButton, BorderLayout.CENTER);
> >
> > final JButton button = new JButton("Make my friend visible");
> > button.addActionListener(new ActionListener() {
> >
> > public void actionPerformed(ActionEvent e) {
> >
> > state = !state;
> > invisibleButton.setVisible(state);
> > button.setText("Make my friend " + (state ?
> > "invisible" : "visible"));
> > }
> > });
> > panel.add(button, BorderLayout.SOUTH);
> >
> > shell.open();
> > while(!shell.isDisposed()) {
> > if (!display.readAndDispatch()) display.sleep();
> > }
> > display.dispose();
> > }
> > }
> > /******************************************************/
> >
> >
> > -----
> >
> > - bartek
>
>
Re: AWT_SWT Adding Hidden Components [message #447896 is a reply to message #447879] Tue, 21 December 2004 16:00 Go to previous message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Jim, is there a bug report that captures this?

"Jim Adams" <Jim.Adams@sas.com> wrote in message
news:cq7beh$bjs$1@www.eclipse.org...
> It should be noted that you can't really use Swing components without them
> being in a JRootPane. The SWT_AWT frame returned is not a JRootPane. This
> will cause you all sorts of refresh errors. Note that Javasoft did not
> provide an implementation of JRootPane that you can derive from so you
will
> have to make your own. We have found that, once you parent from a
> JRootPaneContainer that life is MUCH better. Maybe this is something that
we
> can get the SWT guys to provide? It really is not nice to have to
re-invent
> this wheel over and over...
>
>
> "Steve Northover" <steve_northover@ca.ibm.com> wrote in message
> news:cq75tg$p7c$1@www.eclipse.org...
> > I believe that this has nothing to do with SWT. Here is your code
hacked
> to
> > use AWT/Swing only. It seems to have the same problem.
> >
> > /******************************************************/
> > import org.eclipse.swt.layout.*;
> > import java.awt.BorderLayout;
> > import java.awt.Frame;
> > import java.awt.Panel;
> > import java.awt.event.*;
> > import javax.swing.JButton;
> > import org.eclipse.swt.SWT;
> > import org.eclipse.swt.awt.SWT_AWT;
> > import org.eclipse.swt.widgets.*;
> >
> > class Snippet {
> > static boolean state = false;
> > public static void main(String[] args) {
> >
> > final Display display = new Display();
> > final Shell shell = new Shell(display);
> > shell.setLayout(new FillLayout());
> > Composite composite = new Composite(shell, SWT.EMBEDDED);
> >
> > //Frame frame = SWT_AWT.new_Frame(composite);
> > Frame frame = new Frame();
> > frame.setVisible(true);
> > Panel panel = new Panel(new BorderLayout());
> > frame.add(panel);
> >
> > final JButton invisibleButton = new JButton("I am invisible");
> > invisibleButton.setVisible(state);
> > panel.add(invisibleButton, BorderLayout.CENTER);
> >
> > final JButton button = new JButton("Make my friend visible");
> > button.addActionListener(new ActionListener() {
> > public void actionPerformed(ActionEvent e) {
> > state = !state;
> > invisibleButton.setVisible(state);
> > button.setText("Make my friend "
> > + (state ? "invisible" : "visible"));
> > }
> > });
> >
> > panel.add(button, BorderLayout.SOUTH);
> > shell.open();
> > while (!shell.isDisposed()) {
> > if (!display.readAndDispatch())
> > display.sleep();
> > }
> > display.dispose();
> > }
> > }
> > /** *************************************************** */
> >
> > "Bartosz Frak" <still_aimless@wp.pl> wrote in message
> > news:Xns95C4C38116D4Bstillaimlesswppl@206.191.52.34...
> > > I just stumbled upon a road block in my venture into SWT AWT land. The
> > > application I'm working on requires that some of the Swing/AWT
controls
> > > (JButtons, etc) in the AWT panel have to be initially hidden and made
> > > visible later at runtime. The problem is simple: I cannot unhide any
of
> > the
> > > components, which were created and made invisible before the main
window
> > > was realized. I have tried everything from forcing repaint on
individual
> > > controls, invalidating areas, calling paint(..) directly, repainting
the
> > > AWT frame, repainting the SWT composite holding the frame ... nothing
> > > worked. The controls are still not showing, even though they do have
the
> > > visible bit set to high. The only way to get them to show up is to
> > manually
> > > resize the window, in which they reside - obviously that's not a
> solution
> > > I'm looking for.
> > > What's interesting that after they are shown for the first time ( via
> the
> > > window resize trick ) I can hide/show them at will, and from that
point
> on
> > > they behave as expected. It's only the first setVisible(..) call that
> > > fails. Honestly it looks like SWT/AWT is not honoring the existance of
> > > these components until a full flush of the window contents (like
manual
> > > resize).
> > >
> > > I'm using JDK 5.0 and SWT 3.0 on Windows XP SP2. The AWT Frame has an
> AWT
> > > Panel a child, which houses the controls that are initially hidden.
> > >
> > > Example code.
> > >
> > > The button at the bottom cycles the visibility of the center button,
> > > however the first click, which should make the center button appear
does
> > > nothing. At this point if you resize the window, the center button
will
> > > appear as it should have instantly after the click event. Now,
clicking
> > the
> > > bottom button cycles correctly the visibility state of the center
> button.
> > >
> > >
> > > /******************************************************/
> > > import org.eclipse.swt.layout.*;
> > > import java.awt.BorderLayout;
> > > import java.awt.Frame;
> > > import java.awt.Panel;
> > > import java.awt.event.*;
> > > import javax.swing.JButton;
> > > import org.eclipse.swt.SWT;
> > > import org.eclipse.swt.awt.SWT_AWT;
> > > import org.eclipse.swt.widgets.*;
> > >
> > >
> > > class Snippet {
> > >
> > > static boolean state = false;
> > >
> > > public static void main(String[] args) {
> > >
> > > final Display display = new Display();
> > > final Shell shell = new Shell(display);
> > > shell.setLayout(new FillLayout());
> > > Composite composite = new Composite(shell, SWT.EMBEDDED);
> > >
> > > Frame frame = SWT_AWT.new_Frame(composite);
> > > Panel panel = new Panel(new BorderLayout());
> > > frame.add(panel);
> > >
> > > final JButton invisibleButton = new JButton("I am invisible");
> > > invisibleButton.setVisible(state);
> > > panel.add(invisibleButton, BorderLayout.CENTER);
> > >
> > > final JButton button = new JButton("Make my friend visible");
> > > button.addActionListener(new ActionListener() {
> > >
> > > public void actionPerformed(ActionEvent e) {
> > >
> > > state = !state;
> > > invisibleButton.setVisible(state);
> > > button.setText("Make my friend " + (state ?
> > > "invisible" : "visible"));
> > > }
> > > });
> > > panel.add(button, BorderLayout.SOUTH);
> > >
> > > shell.open();
> > > while(!shell.isDisposed()) {
> > > if (!display.readAndDispatch()) display.sleep();
> > > }
> > > display.dispose();
> > > }
> > > }
> > > /******************************************************/
> > >
> > >
> > > -----
> > >
> > > - bartek
> >
> >
>
>
Previous Topic:embed a composite control in a table or list?
Next Topic:why does my focus go back to the first Button in my InputDialog Box
Goto Forum:
  


Current Time: Thu Mar 28 21:25:10 GMT 2024

Powered by FUDForum. Page generated in 0.02668 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top