I have the following code:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Composite parent = new Composite(shell, SWT.NONE);
parent.setBounds(20, 20, 400, 300);//since shell doesn't have a layout
parent.setLayout(new FillLayout(SWT.HORIZONTAL|SWT.VERTICAL));
Composite child = new Composite(parent, SWT.BORDER);
child.setLayout(new GridLayout(2, true));
Label l = new Label(child, SWT.NONE);
l.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
l.setText("BLOB-BLOB-BLOB!");
Button button = new Button(child, SWT.PUSH);
button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
button.setText("TEEEEXTTTT");
shell.setSize(500, 500);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
So, I thought I would have a parent composite bounded as (20, 20, 400, 300) since shell doesn't have a layout and a child composite filling a parent because it has a FillLayout.
But I get only a parent placed right. And I have to either set layout for shell or set every bounds for every Control inspite of having layouts being set. Why layouts doesn't work in this case?
If you force a layout on the shell before it is opened, everything appears as expected:
shell.setSize( 500, 500 );
shell.layout( true, true ); // force layout
shell.open();
I am not sure as to why the layout() call is necessary. But I don't usually use absolute positions either...
As a side note: the FillLayout takes either SWT.HORIZONTAL or SWT.VERTICAL. Specifying both does not make sense.
Related
I am using Java's SWT toolkit to create a GUI with text field inputs. These input fields require numerical input and have units assigned to them. I'm trying to create a fancy way to integrate units within the field as a fixed suffix to the text, such that the user can only edit the numerical part. I'd also like for the suffix to be greyed out so the user knows it is disabled - something like the following:
While searching, I saw some solutions with a mask formatter from Swing that might do the trick, but I'm sort of hoping there might be something default with SWT. Any suggestions on how to make this work?
The field is part of a a matrix, so I can't simply add the units to a header label. I suppose I could create another column after the text field that could provide units as a label, but I'm going for something more intuitive and aesthetic.
Any suggestions?
One option would be to group Text and Label widgets in the same composite, and set the text on the Label to the desired suffix:
The area to the left of the suffix is the single-line text field, which can be edited, and the suffix is a disabled Label.
public class TextWithSuffixExample {
public class TextWithSuffix {
public TextWithSuffix(final Composite parent) {
// The border gives the appearance of a single component
final Composite baseComposite = new Composite(parent, SWT.BORDER);
baseComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
final GridLayout baseCompositeGridLayout = new GridLayout(2, false);
baseCompositeGridLayout.marginHeight = 0;
baseCompositeGridLayout.marginWidth = 0;
baseComposite.setLayout(baseCompositeGridLayout);
// You can set the background color and force it on
// the children (the Text and Label objects) to add
// to the illusion of a single component
baseComposite.setBackground(new Color(parent.getDisplay(), new RGB(255, 255, 255)));
baseComposite.setBackgroundMode(SWT.INHERIT_FORCE);
final Text text = new Text(baseComposite, SWT.SINGLE | SWT.RIGHT);
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Label label = new Label(baseComposite, SWT.NONE);
label.setEnabled(false);
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true));
label.setText("kg/m^3");
}
}
final Display display;
final Shell shell;
public TextWithSuffixExample() {
display = new Display();
shell = new Shell(display);
shell.setLayout(new GridLayout());
shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
new TextWithSuffix(shell);
}
public void run() {
shell.setSize(200, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(final String[] args) {
new TextWithSuffixExample().run();
}
}
I am having an issue trying to do what seemed to be an easy task.
Coding an UI in java/swt, I'm trying to get the text of a button display on two lines (wrap the string passed to the button), but I can't manage to do so with the carriage return in the string, nor with the SWT.WRAP style of the button.
Here is a sample of my code :
Button myButton = new Button(compoCentre, SWT.WRAP);
myButton.setBounds(40, 200, 240, 40);
myButton.setText("A long text, but not so long, just enough);
However, this results in the text displaying on one single line, hiding the part not fitting the size of the button.
Any ideas / workaround ?
Thank you for your time.
Check out below code :
public class Sample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
Composite comp = new Composite(shell, SWT.NONE);
comp.setLayout(new GridLayout(1, false));
comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button testButton = new Button(comp, SWT.PUSH | SWT.WRAP);
testButton.setText("A long text, but not so long, just enough");
final GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
layoutData.widthHint = 100;
testButton.setLayoutData(layoutData);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Output on Windows 10:
i am new to swt and im trying to create a layout in a window. Each texfield will have a label to the left of the textfield- however in some cases there may be two textfields per label, and maybe at a later date there will be radio buttons added. Is a formlayout the best way of doing this? It seems to me unnecessarily over-complicated. I dont have windowbuilder or a visual designer utility and am finding the FormAttachment method difficult to handle. Any advice appreciated. I have attached a screenshot of the basic gui design Im trying to create.
Unfortunately im not allowed upload images for the moment, as i a new user. Essentially the structure i am aiming for is like so:
LABEL TEXTBOX
LABEL TEXTBOX TEXTBOX
LABEL TEXTBOX
LABEL TEXTBOX TEXTBOX
LABEL CALENDAR CONTROL
OK | NOK
Here is a simple example that should explain how to span a widget across multiple columns of a GridLayout:
public class StackOverflow2
{
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(3, false));
Label firstLabel = new Label(shell, SWT.NONE);
firstLabel.setText("Label");
Text firstText = new Text(shell, SWT.BORDER);
addSpanData(firstText, 2);
Label secondLabel = new Label(shell, SWT.NONE);
secondLabel.setText("Label");
Text secondText = new Text(shell, SWT.BORDER);
addSpanData(secondText, 1);
Text thirdText = new Text(shell, SWT.BORDER);
addSpanData(thirdText, 1);
Label thirdLabel = new Label(shell, SWT.NONE);
thirdLabel.setText("Label");
Text fourthText = new Text(shell, SWT.BORDER);
addSpanData(fourthText, 2);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void addSpanData(Control comp, int span)
{
GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
data.horizontalSpan = span;
comp.setLayoutData(data);
}
}
This is how it looks:
In my SWT application i have certain components inside the SWT shell.
Now how can i auto re-size this components according to the size of display window.
Display display = new Display();
Shell shell = new Shell(display);
Group outerGroup,lowerGroup;
Text text;
public test1() {
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns=1;
shell.setLayout(gridLayout);
outerGroup = new Group(shell, SWT.NONE);
GridData data = new GridData(1000,400);
data.verticalSpan = 2;
outerGroup.setLayoutData(data);
gridLayout = new GridLayout();
gridLayout.numColumns=2;
gridLayout.makeColumnsEqualWidth=true;
outerGroup.setLayout(gridLayout);
...
}
i.e when I decrease the size of window the components inside it should appear according to that.
That sounds suspiciously like you aren't using layouts.
The whole concept of layouts makes worrying about resizing needless. The layout will take care of the size of all of its components.
I would recommend to read the Eclipse article about layouts
Your code can easily be corrected. Don't set the size of individual components, the layout will determine their size. If you want the window to have a predefined size, set the shell's size:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
Group outerGroup = new Group(shell, SWT.NONE);
// Tell the group to stretch in all directions
outerGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
outerGroup.setLayout(new GridLayout(2, true));
outerGroup.setText("Group");
Button left = new Button(outerGroup, SWT.PUSH);
left.setText("Left");
// Tell the button to stretch in all directions
left.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button right = new Button(outerGroup, SWT.PUSH);
right.setText("Right");
// Tell the button to stretch in all directions
right.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
shell.setSize(1000,400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Before resizing:
After resizing:
Here is my code which actually does all the GUI work for Domain(right side contained in the picture attached).In this function What i am doing is I am creating a composite "test" on the "newTabFolder".Then i am creating ScrolledComposite "sc" on it and then Creating a composite "compositeInTab" on it and after placing all the widgets on "compositeInTab" I am creating a TabItem for placing the composite "test" on it.
public DomainUI(Composite composite, TabFolder newTabFolder, boolean comingFromSelf)
{
Composite test = new Composite(newTabFolder,SWT.NONE);
test.setLayout(new FillLayout());
ScrolledComposite sc = new ScrolledComposite(test, SWT.V_SCROLL|SWT.H_SCROLL);
final Composite compositeInTab = new Composite(sc, SWT.NONE);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinHeight(compositeInTab.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
sc.setMinWidth(compositeInTab.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
sc.setContent(compositeInTab);
compositeInTab.setLayout(null);
sc.setAlwaysShowScrollBars(true);
/*HERE I AM CREATING LABELS AND TEXT FIELDS AND SETTING THEIR BOUNDS*/
systemCodeLabel = new Label(compositeInTab, 0);
systemCodeText = new Text(compositeInTab, SWT.BORDER);
systemCodeLabel.setText("System Code");
systemCodeLabel.setBounds(350, 60, 100, 15);
systemCodeText.setBounds(480, 60, 150, 17);
// CREATION OF LABELS AND TEXTFIELDS ENDED
// CREATION OF TABLE STARTS
myTable = new CreateTable(compositeInTab, 1);
myTable.setBounds(50, 230, 0, 0);
myTable.table.setSize(myTable.table.computeSize(570, 250));
//here i filled data in table
for(int i=0; i<myTable.table.getColumnCount(); i++)
{
myTable.table.getColumn(i).pack();
myTable.table.getColumn(i).setWidth(myTable.table.getColumn(i).getWidth()+10);
}
TabItem tabItem1 = new TabItem(newTabFolder, SWT.NONE);
tabItem1.setText("Domain");
tabItem1.setControl(test);
newTabFolder.setBounds(0, 0, 480, 300);
}
It seems to be a matter of layouts. I fiddled around a little bit and managed to get this working:
public static void main(String[] args)
{
Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
ScrolledComposite scrollComp = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL);
Composite innerComp = new Composite(scrollComp, SWT.NONE);
innerComp.setLayout(new GridLayout(4, true));
for(int i = 0; i < 32; i++)
new Button(innerComp, SWT.PUSH).setText("Button");
scrollComp.setMinHeight(innerComp.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
scrollComp.setMinWidth(innerComp.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
scrollComp.setContent(innerComp);
scrollComp.setExpandHorizontal(true);
scrollComp.setExpandVertical(true);
scrollComp.setAlwaysShowScrollBars(true);
shell.pack();
shell.open();
shell.setSize(200, 200);
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
display.sleep();
}
}
It will show the scrollbars. However, if I change the layout of the shell to a GridLayout, it will not work. Maybe your "combination" of layouts seems to be the problem...