Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Re: SWT control preferred, minimum and maximum sizes
Re: SWT control preferred, minimum and maximum sizes [message #639263] |
Mon, 15 November 2010 18:50 |
Jean Bovet Messages: 34 Registered: September 2009 |
Member |
|
|
Hi,
I am facing an issue related to the minimum size of a control. I'm
probably overlooking an easy solution. The problem is the following: I
have a ScrolledComposite that displays a particular composite A. I
would like to have the scrollbars visible only if the visible area is
smaller than the mininum size of the composite A. How can I do that? I
am using the following method right now:
sc.setContent(compositeA);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinWidth(...);
sc.setMinHeight(...);
The problem is: how to get the minimum size of the composite A? I tried
to use computeSize(0, 0) but the resulted size is also 0. If I use
computeSize(SWT.DEFAULT, SWT.DEFAULT), then it works but fails in
certain particular case: if composite A contains several buttons and a
table, I would like to know its minimum size so the scroll bars of the
Scrolled Composite only appears if the table inside the composite A
cannot shrink anymore.
Any help is welcome, thanks.
Jean
On 2006-04-20 06:34:57 -0700, Veronika Irvine said:
> There is currenlty no way to determine the minimum or maximum size.
>
> Calling computeSize(0, 0) will give the size needed to show a control
> with client area of (0,0) - that is it will add on all neccessary trim.
> For example, in a TabFolder the tabs and the border are considered
> trim.
>
>
> "Alexander Mitin" <Alexander.Mitin@gmail.com> wrote in message
> news:95f7ac89d47840ea3876e46ffb1b5185$1@www.eclipse.org...
>> Hello.
>>
>> Consider the following SWING code:
>>
>> final JTextField textField = new JTextField();
>> textField.setText("button");
>> // skip some code
>> Dimension preferredSize = button.getPreferredSize();
>> Dimension minimumSize = button.getMinimumSize();
>> Dimension maximumSize = button.getMaximumSize();
>> System.out.println("preferredSize = " + preferredSize);
>> System.out.println("minimumSize = " + minimumSize);
>> System.out.println("maximumSize = " + maximumSize);
>>
>> This gives me the result: "preferredSize =
>> java.awt.Dimension[width=41,height=20]
>> minimumSize = java.awt.Dimension[width=11,height=20]
>> maximumSize = java.awt.Dimension[width=2147483647,height=2147483647]"
>>
>> How I do the same thing in SWT: determine the minimum, maximum and
>> preferred sizes of SWT control? Yes, I can call
>> textControl.computeSize(SWT.DEFAULT, SWT.DEFAULT, true) for preferred
>> size but what about minimum and maximum control sizes? If I call
>> textControl.computeSize(0, 0, true) (expecting the control to compute
>> its minimum size) that gives me strange result. Is there any generic
>> methods to determine minimum and maximum size of SWT control which I've
>> missed? Or how can I determine the exact minimum and maximum sizes?
|
|
|
Re: SWT control preferred, minimum and maximum sizes [message #639384 is a reply to message #639263] |
Tue, 16 November 2010 10:47 |
|
Lakshmi P Shanmugam Messages: 279 Registered: July 2009 Location: India |
Senior Member |
|
|
Hi,
composite.computeSize(SWT.DEFAULT, SWT.DEFAULT) gives the preferred size of the composite. And you can use this to set the min size for the control in the ScrolledComposite. I'm not sure if I understood "table inside the composite A cannot shrink anymore" correctly. I assumed it means when the table items are not fully visible.
Here is a simple snippet with Table and Buttons inside a ScrolledComposite that works for me.
public class Snippet {
public static void main (String [] args)
{
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
final ScrolledComposite c1 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
final Composite composite = new Composite(c1, SWT.BORDER);
composite.setLayout(new GridLayout(4, false));
for (int i = 0; i < 20; i++) {
Button button = new Button(composite, SWT.PUSH);
button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
button.setText("button" + i);
}
Table table = new Table(composite, SWT.SINGLE|SWT.NO_SCROLL);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
table.setLinesVisible(true);
table.setHeaderVisible(true);
for (int i = 0; i < 20; i++) {
TableItem tableItem= new TableItem(table, SWT.NONE);
tableItem.setText(0, "item"+i);
}
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumn(i).pack();
}
composite.pack();
c1.setExpandHorizontal(true);
c1.setExpandVertical(true);
c1.setContent(composite);
c1.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
shell.pack();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
If a particular case is not working for you , can you modify the above snippet to show the failing case?
Lakshmi P Shanmugam
|
|
|
Re: SWT control preferred, minimum and maximum sizes [message #639518 is a reply to message #639384] |
Tue, 16 November 2010 18:59 |
Jean Bovet Messages: 34 Registered: September 2009 |
Member |
|
|
This is a multi-part message in MIME format.
----------------19037194461706782197
Content-Type: text/plain; charset=iso-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
Hi,
Thanks for your answer. If I run your snippet, I can see that the table
height will grow as I add more lines and the scrolledcomposite adjusts
itself to be able to scroll the complete height of the table. That's
fine. However, I'm looking at something a bit different: let's say I
want to display only 10 lines of the table at a time and have the table
show its scroll bars. There is maybe a nicer way of doing that but so
far I have to manually set the size on the composite (see code below)
which is something I would rather avoid. This is where I'm looking at
getting the minimum size of the table so I can set the size without
having to rely on constant value. I'm trying to get the answer to the
question "what is the minimum size a component needs to have to display
itself completely?".
Here is the code to illustrate what I want to achieve.
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
final ScrolledComposite c1 = new ScrolledComposite(shell,
SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
final Composite composite = new Composite(c1, SWT.BORDER);
composite.setLayout(new GridLayout(4, false));
for (int i = 0; i < 20; i++) {
Button button = new Button(composite, SWT.PUSH);
button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
button.setText("button" + i);
}
Table table = new Table(composite, SWT.SINGLE|SWT.V_SCROLL|SWT.H_SCROLL);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
table.setLinesVisible(true);
table.setHeaderVisible(true);
for (int i = 0; i < 200; i++) {
TableItem tableItem= new TableItem(table, SWT.NONE);
tableItem.setText(0, "item"+i);
}
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumn(i).pack();
}
//composite.pack();
composite.setSize(200, 300);
c1.setExpandHorizontal(true);
//c1.setExpandVertical(false);
c1.setContent(composite);
c1.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
//shell.pack();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
Regards,
/Jean
On 2010-11-16 03:17:27 -0800, Lakshmi Shanmugam said:
> Hi,
>
> composite.computeSize(SWT.DEFAULT, SWT.DEFAULT) gives the preferred
> size of the composite. And you can use this to set the min size for the
> control in the ScrolledComposite. I'm not sure if I understood "table
> inside the composite A cannot shrink anymore" correctly. I assumed it
> means when the table items are not fully visible. Here is a simple
> snippet with Table and Buttons inside a ScrolledComposite that works
> for me.
> public class Snippet {
> public static void main (String [] args) {
> Display display = new Display ();
> Shell shell = new Shell (display);
> shell.setLayout(new FillLayout());
>
> final ScrolledComposite c1 = new ScrolledComposite(shell,
> SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
> final Composite composite = new Composite(c1, SWT.BORDER);
> composite.setLayout(new GridLayout(4, false));
> for (int i = 0; i < 20; i++) {
> Button button = new Button(composite, SWT.PUSH);
> button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
> button.setText("button" + i);
> }
> Table table = new Table(composite, SWT.SINGLE|SWT.NO_SCROLL);
> table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
> table.setLinesVisible(true);
> table.setHeaderVisible(true);
> for (int i = 0; i < 20; i++) {
> TableItem tableItem= new TableItem(table, SWT.NONE);
> tableItem.setText(0, "item"+i);
> }
> for (int i = 0; i < table.getColumnCount(); i++) {
> table.getColumn(i).pack();
> }
> composite.pack();
> c1.setExpandHorizontal(true);
> c1.setExpandVertical(true);
> c1.setContent(composite);
> c1.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
>
> shell.pack();
> shell.open ();
> while (!shell.isDisposed ()) {
> if (!display.readAndDispatch ()) display.sleep ();
> }
> display.dispose ();
> }
> }
>
> If a particular case is not working for you , can you modify the above
> snippet to show the failing case?
----------------19037194461706782197
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1038.35">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 15.0px; font: 12.0px Helvetica}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 15.0px; font: 12.0px Helvetica; min-height: 14.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #559173}
p.p6 {margin: 0.0px 0.0px 0.0px 12.0px; line-height: 14.0px; font: 12.0px Helvetica; color: #1a0090}
p.p7 {margin: 0.0px 0.0px 0.0px 12.0px; line-height: 14.0px; font: 12.0px Helvetica; color: #1a0090; min-height: 14.0px}
span.s1 {color: #8e0068}
span.s2 {color: #2900c9}
span.s3 {color: #4c00fc}
span.s4 {color: #000000}
span.Apple-tab-span {white-space:pre}
</style>
</head>
<body>
<p class="p1">Hi,</p>
<p class="p2"><br></p>
<p class="p1">Thanks for your answer. If I run your snippet, I can see that the table height will grow as I add more lines and the scrolledcomposite adjusts itself to be able to scroll the complete height of the table. That's fine. However, I'm looking at something a bit different: let's say I want to display only 10 lines of the table at a time and have the table show its scroll bars. There is maybe a nicer way of doing that but so far I have to manually set the size on the composite (see code below) which is something I would rather avoid. This is where I'm looking at getting the minimum size of the table so I can set the size without having to rely on constant value. I'm trying to<span class="Apple-converted-space">
|
|
|
Re: SWT control preferred, minimum and maximum sizes [message #639723 is a reply to message #639518] |
Wed, 17 November 2010 15:28 |
|
Lakshmi P Shanmugam Messages: 279 Registered: July 2009 Location: India |
Senior Member |
|
|
To have the table show a fixed number of items and scrollbars you could try to control the height of the table directly using the GridData.heightHint.
Table table = new Table(composite, SWT.SINGLE|SWT.V_SCROLL|SWT.H_SCROLL);
GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
table.setLayoutData(layoutData);
...
...
int ht = (table.getItemHeight() * 10) + table.getHeaderHeight();
layoutData.heightHint = table.computeSize(SWT.DEFAULT, ht).y;
Lakshmi P Shanmugam
|
|
|
Goto Forum:
Current Time: Wed Sep 25 17:26:46 GMT 2024
Powered by FUDForum. Page generated in 0.04527 seconds
|