Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » Albireo » Size-Calculation of Embedded Swing Controls in Dialogs
Size-Calculation of Embedded Swing Controls in Dialogs [message #10897] Mon, 06 April 2009 11:29 Go to next message
Dominik Kaspar is currently offline Dominik Kaspar
Messages: 12
Registered: July 2009
Junior Member
Hi,

If I'm using a JFace dialog like the TitleAreaDialog and having a
SwingControl inside the dialog, the size of the main panel is not
correctly calculated after the shell of the dialog is created.

I tried all available Composites for the layoutDeferredAncestor, but no
one was working correctly. My workaround was to call pack() on the shell
in the afterComponentCreatedSWTThread() method but this works only
randomly.

Any ideas to solve this problem?

Thanks,
Dominik

TitleAreaDialog d = new TitleAreaDialog(topLevelShell) {
protected Control createDialogArea(Composite parent) {
final Composite comp = (Composite) super.createDialogArea(parent);
SwingControl control = new SwingControl(comp, SWT.NONE) {
protected JComponent createSwingComponent() {
JPanel p = new JPanel();
p.setLayout(new java.awt.GridLayout(10, 1));
for (int i = 0; i < 10; i++) {
p.add(new JTextField(String.valueOf(i)));
}
JScrollPane scroll = new JScrollPane(p);
scroll.setBorder(new EmptyBorder(0, 0, 0, 0));
return scroll;
}
public Composite getLayoutAncestor() {
return getShell();
}
};
control.setLayoutData(new GridData(GridData.FILL_BOTH));
return parent;
}
};
d.open();
Re: Size-Calculation of Embedded Swing Controls in Dialogs [message #10910 is a reply to message #10897] Tue, 07 April 2009 14:30 Go to previous messageGo to next message
Gordon Hirsch is currently offline Gordon Hirsch
Messages: 100
Registered: July 2009
Senior Member
The code you included below works fine for me. What version of Java are
you using? Could you be more specific about the way in which the size is
not correctly calculated?

Dominik Kaspar wrote:
> Hi,
>
> If I'm using a JFace dialog like the TitleAreaDialog and having a
> SwingControl inside the dialog, the size of the main panel is not
> correctly calculated after the shell of the dialog is created.
>
> I tried all available Composites for the layoutDeferredAncestor, but no
> one was working correctly. My workaround was to call pack() on the shell
> in the afterComponentCreatedSWTThread() method but this works only
> randomly.
>
> Any ideas to solve this problem?
>
> Thanks,
> Dominik
>
> TitleAreaDialog d = new TitleAreaDialog(topLevelShell) {
> protected Control createDialogArea(Composite parent) {
> final Composite comp = (Composite) super.createDialogArea(parent);
> SwingControl control = new SwingControl(comp, SWT.NONE) {
> protected JComponent createSwingComponent() {
> JPanel p = new JPanel();
> p.setLayout(new java.awt.GridLayout(10, 1));
> for (int i = 0; i < 10; i++) {
> p.add(new JTextField(String.valueOf(i)));
> }
> JScrollPane scroll = new JScrollPane(p);
> scroll.setBorder(new EmptyBorder(0, 0, 0, 0));
> return scroll;
> }
> public Composite getLayoutAncestor() {
> return getShell();
> }
> };
> control.setLayoutData(new GridData(GridData.FILL_BOTH));
> return parent;
> }
> };
> d.open();
>
Re: Size-Calculation of Embedded Swing Controls in Dialogs [message #10923 is a reply to message #10910] Wed, 08 April 2009 15:02 Go to previous messageGo to next message
Dominik Kaspar is currently offline Dominik Kaspar
Messages: 12
Registered: July 2009
Junior Member
I'm using the following environment:
-albireo standalone 0.0.3 (v20081031)
-java 1.6.0_12 64-Bit (also tested with 32-Bit)
-standalone swt win32 x86 64-Bit 3.4.1 (also tested with 32-Bit)
-standalone jface 3.4.1

I'm expecting:
-the dialog is big enough to display all JTextFields*
-there should be no scroll bar visible*

*as long the dialog is not resized and the screen resolution is not too
low;)
Re: Size-Calculation of Embedded Swing Controls in Dialogs [message #10934 is a reply to message #10923] Wed, 08 April 2009 19:47 Go to previous messageGo to next message
Gordon Hirsch is currently offline Gordon Hirsch
Messages: 100
Registered: July 2009
Senior Member
I think the base problem here has to do with the fact that the preferred
size of the Swing component is not yet known when the dialog decides on
its own initial size. (It's not yet known because size calculations
involve waiting for the AWT event dispatch thread to run. We defer
actual layout of the SWT composite until this has happened, but this is
a case where just a size computation is done, and it probably can't be
deferred.)

One solution for you would be to override your TitleAreaDialog's
getInitialSize() method and return a reasonable value that does not rely
on waiting for the Swing component's preferred size to be known.

If that won't work, you could try to find a way to resize the dialog
once the Swing control's preferred size has been calculated. I'm not
sure how easy that would be.

Dominik Kaspar wrote:
> I'm using the following environment:
> -albireo standalone 0.0.3 (v20081031)
> -java 1.6.0_12 64-Bit (also tested with 32-Bit)
> -standalone swt win32 x86 64-Bit 3.4.1 (also tested with 32-Bit)
> -standalone jface 3.4.1
>
> I'm expecting:
> -the dialog is big enough to display all JTextFields*
> -there should be no scroll bar visible*
>
> *as long the dialog is not resized and the screen resolution is not too
> low;)
>
Re: Size-Calculation of Embedded Swing Controls in Dialogs [message #10944 is a reply to message #10934] Tue, 14 April 2009 09:15 Go to previous messageGo to next message
Dominik Kaspar is currently offline Dominik Kaspar
Messages: 12
Registered: July 2009
Junior Member
The first solution is not a option because we use a xml to generate the
swing components. To pre calculate the size of the components will be a
lot of effort. Therefore I tried to put my swing components into a frame
(without displaying it), pack the components, get the size and remove the
components from the frame. Unfortunatly I had to do this in the SWT thread
before the Swing thread is used for really creating the components. You
can imagine that this leads into problems;) Is it possible to put the
'swing pre creation and packing' into a sync swing thread?

The second solution could be a work around. The problem there is that you
can see the dialog, when it pupups, in the wrong size for a short moment
before it resizes to the correct size. The problem here is really to find
the correct moment to set the size and I don't know how to figure out when
the size calculation of the swing componetns is successfully finished.

Thanks, Dominik
Re: Size-Calculation of Embedded Swing Controls in Dialogs [message #10976 is a reply to message #10944] Thu, 14 May 2009 09:33 Go to previous message
Dominik Kaspar is currently offline Dominik Kaspar
Messages: 12
Registered: July 2009
Junior Member
I solved the problem by adding a SizeListener to the SwingControl which is
called after the Swing/AWT size calculation is finished. There I just set
the size on the shell again. It's a little bit ugly here that you can see
the shell resizing on the screen. I tried to set an initial size of
Point(0,0) but the shell has some min size for the title and close, min,
max buttons. Any ideas?

Thanks and regards
Dominik

Example code:

TitleAreaDialog d = new TitleAreaDialog(topLevelShell) {
protected Control createDialogArea(final Composite parent) {
Composite comp = (Composite) super.createDialogArea(parent);
SwingControl control = new SwingControl(comp, SWT.NONE) {
protected JComponent createSwingComponent() {
JPanel p = new JPanel();
p.setLayout(new java.awt.GridLayout(10, 1));
for (int i = 0; i < 10; i++) {
p.add(new JTextField(String.valueOf(i)));
}
JScrollPane scroll = new JScrollPane(p);
scroll.setBorder(new EmptyBorder(0, 0, 0, 0));
return scroll;
}
public Composite getLayoutAncestor() {
return parent;
}
};
control.setLayoutData(new GridData(GridData.FILL_BOTH));
control.addSizeListener(new SizeListener() {
private boolean packed = false;
public void preferredSizeChanged(SizeEvent event) {
if (!packed) {
Point size = getInitialSize();
getShell().setSize(size);
packed = true;
}
}
});
return parent;
}
};
d.open();
Re: Size-Calculation of Embedded Swing Controls in Dialogs [message #574958 is a reply to message #10897] Tue, 07 April 2009 14:30 Go to previous message
Gordon Hirsch is currently offline Gordon Hirsch
Messages: 100
Registered: July 2009
Senior Member
The code you included below works fine for me. What version of Java are
you using? Could you be more specific about the way in which the size is
not correctly calculated?

Dominik Kaspar wrote:
> Hi,
>
> If I'm using a JFace dialog like the TitleAreaDialog and having a
> SwingControl inside the dialog, the size of the main panel is not
> correctly calculated after the shell of the dialog is created.
>
> I tried all available Composites for the layoutDeferredAncestor, but no
> one was working correctly. My workaround was to call pack() on the shell
> in the afterComponentCreatedSWTThread() method but this works only
> randomly.
>
> Any ideas to solve this problem?
>
> Thanks,
> Dominik
>
> TitleAreaDialog d = new TitleAreaDialog(topLevelShell) {
> protected Control createDialogArea(Composite parent) {
> final Composite comp = (Composite) super.createDialogArea(parent);
> SwingControl control = new SwingControl(comp, SWT.NONE) {
> protected JComponent createSwingComponent() {
> JPanel p = new JPanel();
> p.setLayout(new java.awt.GridLayout(10, 1));
> for (int i = 0; i < 10; i++) {
> p.add(new JTextField(String.valueOf(i)));
> }
> JScrollPane scroll = new JScrollPane(p);
> scroll.setBorder(new EmptyBorder(0, 0, 0, 0));
> return scroll;
> }
> public Composite getLayoutAncestor() {
> return getShell();
> }
> };
> control.setLayoutData(new GridData(GridData.FILL_BOTH));
> return parent;
> }
> };
> d.open();
>
Re: Size-Calculation of Embedded Swing Controls in Dialogs [message #574984 is a reply to message #10910] Wed, 08 April 2009 15:02 Go to previous message
Dominik Kaspar is currently offline Dominik Kaspar
Messages: 12
Registered: July 2009
Junior Member
I'm using the following environment:
-albireo standalone 0.0.3 (v20081031)
-java 1.6.0_12 64-Bit (also tested with 32-Bit)
-standalone swt win32 x86 64-Bit 3.4.1 (also tested with 32-Bit)
-standalone jface 3.4.1

I'm expecting:
-the dialog is big enough to display all JTextFields*
-there should be no scroll bar visible*

*as long the dialog is not resized and the screen resolution is not too
low;)
Re: Size-Calculation of Embedded Swing Controls in Dialogs [message #575004 is a reply to message #10923] Wed, 08 April 2009 19:47 Go to previous message
Gordon Hirsch is currently offline Gordon Hirsch
Messages: 100
Registered: July 2009
Senior Member
I think the base problem here has to do with the fact that the preferred
size of the Swing component is not yet known when the dialog decides on
its own initial size. (It's not yet known because size calculations
involve waiting for the AWT event dispatch thread to run. We defer
actual layout of the SWT composite until this has happened, but this is
a case where just a size computation is done, and it probably can't be
deferred.)

One solution for you would be to override your TitleAreaDialog's
getInitialSize() method and return a reasonable value that does not rely
on waiting for the Swing component's preferred size to be known.

If that won't work, you could try to find a way to resize the dialog
once the Swing control's preferred size has been calculated. I'm not
sure how easy that would be.

Dominik Kaspar wrote:
> I'm using the following environment:
> -albireo standalone 0.0.3 (v20081031)
> -java 1.6.0_12 64-Bit (also tested with 32-Bit)
> -standalone swt win32 x86 64-Bit 3.4.1 (also tested with 32-Bit)
> -standalone jface 3.4.1
>
> I'm expecting:
> -the dialog is big enough to display all JTextFields*
> -there should be no scroll bar visible*
>
> *as long the dialog is not resized and the screen resolution is not too
> low;)
>
Re: Size-Calculation of Embedded Swing Controls in Dialogs [message #575024 is a reply to message #10934] Tue, 14 April 2009 09:15 Go to previous message
Dominik Kaspar is currently offline Dominik Kaspar
Messages: 12
Registered: July 2009
Junior Member
The first solution is not a option because we use a xml to generate the
swing components. To pre calculate the size of the components will be a
lot of effort. Therefore I tried to put my swing components into a frame
(without displaying it), pack the components, get the size and remove the
components from the frame. Unfortunatly I had to do this in the SWT thread
before the Swing thread is used for really creating the components. You
can imagine that this leads into problems;) Is it possible to put the
'swing pre creation and packing' into a sync swing thread?

The second solution could be a work around. The problem there is that you
can see the dialog, when it pupups, in the wrong size for a short moment
before it resizes to the correct size. The problem here is really to find
the correct moment to set the size and I don't know how to figure out when
the size calculation of the swing componetns is successfully finished.

Thanks, Dominik
Re: Size-Calculation of Embedded Swing Controls in Dialogs [message #575103 is a reply to message #10944] Thu, 14 May 2009 09:33 Go to previous message
Dominik Kaspar is currently offline Dominik Kaspar
Messages: 12
Registered: July 2009
Junior Member
I solved the problem by adding a SizeListener to the SwingControl which is
called after the Swing/AWT size calculation is finished. There I just set
the size on the shell again. It's a little bit ugly here that you can see
the shell resizing on the screen. I tried to set an initial size of
Point(0,0) but the shell has some min size for the title and close, min,
max buttons. Any ideas?

Thanks and regards
Dominik

Example code:

TitleAreaDialog d = new TitleAreaDialog(topLevelShell) {
protected Control createDialogArea(final Composite parent) {
Composite comp = (Composite) super.createDialogArea(parent);
SwingControl control = new SwingControl(comp, SWT.NONE) {
protected JComponent createSwingComponent() {
JPanel p = new JPanel();
p.setLayout(new java.awt.GridLayout(10, 1));
for (int i = 0; i < 10; i++) {
p.add(new JTextField(String.valueOf(i)));
}
JScrollPane scroll = new JScrollPane(p);
scroll.setBorder(new EmptyBorder(0, 0, 0, 0));
return scroll;
}
public Composite getLayoutAncestor() {
return parent;
}
};
control.setLayoutData(new GridData(GridData.FILL_BOTH));
control.addSizeListener(new SizeListener() {
private boolean packed = false;
public void preferredSizeChanged(SizeEvent event) {
if (!packed) {
Point size = getInitialSize();
getShell().setSize(size);
packed = true;
}
}
});
return parent;
}
};
d.open();
Previous Topic:Re: Drag and drop and cut, copy, paste between awt / swing and swt.
Next Topic:Memory leak
Goto Forum:
  


Current Time: Thu Oct 23 16:50:01 GMT 2014

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

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