Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Parent's size(When I can know the size of a prent of a Composite?)
| | |
Re: Parent's size [message #499042 is a reply to message #498818] |
Thu, 19 November 2009 15:58 |
Grant Gayed Messages: 2150 Registered: July 2009 |
Senior Member |
|
|
The easiest way to do this is to listen to Resize events on the parent and
adjust your size accordingly. I don't know about MigLayout, but it can
probably be used in a similar manner to the snippet below. (In some ways
FormLayout lends itself to this case better than GridLayout, but GridLayout
seems adequate and is much more straightforward).
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setBounds(200,200,200,200);
shell.setLayout(new GridLayout());
final Label label1 = new Label(shell, SWT.BORDER);
label1.setText("I fill 50%");
label1.setLayoutData(new GridData());
final Label label2 = new Label(shell, SWT.BORDER);
label2.setText("I fill 25%");
label2.setLayoutData(new GridData());
shell.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
Rectangle rect = shell.getClientArea();
((GridData)label1.getLayoutData()).widthHint = rect.width / 2;
((GridData)label2.getLayoutData()).widthHint = rect.width / 4;
shell.layout();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
shell.dispose();
display.dispose();
}
Grant
"David Crecente" <david.crecente@gmail.com> wrote in message
news:he1c10$f7r$1@build.eclipse.org...
> Hi and thank you for your answer.
>
> I've seen serveral of the Snippets in the url you gave me but i don't
find the answer for my simple question.
>
> I need to paint several events in a bar. I am using labels for it. This
information has a size depending on the time of starting, ending and the
available space in its container.
>
> So, if the container has a size of 800 pixels and an event had a
duration of 1 hour then, after several calculations, we know that the label
for this event should have 150 pixels.
>
> But if the container has a size of 1200 pixels the same event needs to
be painted with a size 66% bigger.
>
> Similar calculations must be done for the position of the labels in
that bar. These depends on the time.
>
> But before painting anything I need to know how big is the parent in
which bars and events are going to be painted.
>
> is It possible?
>
> Currently I have several bars working, and the events are being
painting right but I don't know exactly why it's working.
> Even when I resize the window all bars are painted again and
calculations are working.
>
> About the layout I use always MigLayout in each Composite.
>
>
|
|
|
Re: Parent's size [message #499201 is a reply to message #499042] |
Fri, 20 November 2009 11:52 |
David Crecente Messages: 18 Registered: July 2009 |
Junior Member |
|
|
Hi,
thank you for your example. But I think GridLayout is not enoght flexible for this case.
I've written an small Snippet using MigLayout, and caught several images of this code.
In this figure you can see two events in the wrong position and wrong sizes.
If you move the window the position and sizes for the events are corrected.
You can make smaller the window and every thing work right. You can see that the events are smaller because they have less space to be represented. And their relative position has also changed.
In my real application I have serveral of these bars and I had to set over my composite an absolute size (and I don't like it).
What I really need is something like this one:
Where I can calcule each event depending of the time.
Any feedback will be appreciated.
Any other point of view to do this task will also be appreciated.
The Snippet:
package misejemplos;
import net.miginfocom.swt.MigLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class AvailableSpaceTest {
// I need this references.
private static Display display;
private static Shell shell;
private static Composite evolutionBar;
public static void main(String[] args) {
display = new Display();
shell = new Shell(display);
shell.setLayout(new MigLayout("wrap 1, fillx"));
shell.setLayoutData("pushx, growx");
// A parent composite which never will be disposed.
Composite parentContainer = new Composite(shell, SWT.BORDER);
parentContainer.setLayout(new MigLayout("wrap 1, fillx"));
parentContainer.setLayoutData("pushx, growx");
parentContainer.pack(true);
// A container which will be dispose when it's required to calculate
// position,
// widths, heights, positions.
// We can understand this container like the evolution bar.
// It never will be disposed.
evolutionBar = new Composite(parentContainer, SWT.BORDER);
evolutionBar.setLayout(new MigLayout("wrap 1, fillx"));
evolutionBar.setLayoutData("pushx, growx, h 20");
evolutionBar.setBackground(new Color(display, 45, 32, 133));
evolutionBar.pack(true); // requered in order to get the size later.
// If the window is resized we need to repeat all calculates.
shell.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
Composite parent = evolutionBar.getParent();
evolutionBar.dispose();
evolutionBar = new Composite(parent, SWT.BORDER);
evolutionBar.setBackground(new Color(display, 45, 32, 133));
evolutionBar.setLayout(new MigLayout("fillx"));
evolutionBar.setLayoutData("pushx, growx");
evolutionBar.pack(true); // requered in order to get the size
// later.
paint();
}
});
// The method which paints the progress bar
paint();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
shell.dispose();
display.dispose();
}
/**
* In my real scenario I get new events from a queue and I have a refresh
* each 4 seconds. Here I will only have resize.
*/
public static void paint() {
// The first event is painted in its evolution bar.
final Label label1 = new Label(evolutionBar, SWT.BORDER);
label1.setText("event 1");
int childWidth = findOutParentWidth(evolutionBar);
// We translate the time this event took to the available space.
// This is only an example.
childWidth /= 4;
label1.setSize(childWidth, 20);
label1.setLayoutData("pos 0 0, w " + childWidth + ", h 20");
label1.setBackground(new Color(display, 123, 32, 56));
// The second event is painted in its evolution bar.
final Label label2 = new Label(evolutionBar, SWT.BORDER);
label2.setText("event 2");
childWidth = findOutParentWidth(evolutionBar);
// We translate the time this event took to the available space.
// This event took less time than the previous. This is only an example.
childWidth /= 6;
label2.setSize(childWidth, 20);
// We need to find out it position in the evolution bar. So,
// we translate the time in the available space.
int position = findOutParentWidth(evolutionBar) / 2;
label2.setLayoutData("pos " + position + " 0, w " + childWidth
+ ", h 20");
label2.setBackground(new Color(display, 32, 123, 56));
evolutionBar.pack(true);
evolutionBar.getParent().pack(true);
}
/**
* finds out the width.
*/
private static int findOutParentWidth(Composite child) {
return child.getParent().getSize().x;
}
}
|
|
| |
Goto Forum:
Current Time: Thu Mar 28 12:14:59 GMT 2024
Powered by FUDForum. Page generated in 0.02997 seconds
|