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:
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();
}
}
How do I right align the check box in my Java SWT application ? Here is my code snippet:
Button checkFullECR = new Button(scrolledForm.getBody(), SWT.CHECK);
checkFullECR.setAlignment(SWT.RIGHT);
checkFullECR.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
checkFullECR.setText("I need the complete ECR/ECN Process:");
It looks like the following:
[ ] I need the complete ECR/ECN Process:
I want it to look like:
I need the complete ECR/ECN Process: [ ]
You can't do this using just a Button control, you will need to use a Label for the text and a checkbox Button with no text arranged in two columns.
Something like:
Composite body = new Composite(parent, SWT.NONE);
body.setLayout(new GridLayout(2, false));
Label label = new Label(body, SWT.LEFT);
label.setText("I need the complete ECR/ECN Process:");
Button checkFullECR = new Button(body, SWT.CHECK);
... more label / button pairs ....
For once I kind of have to disagree with Greg.
You can use the setOrientation(int) method of the Button to force right-to-left layout:
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.CHECK);
button.setText("TEST");
button = new Button(shell, SWT.CHECK);
button.setOrientation(SWT.RIGHT_TO_LEFT);
button.setText("TEST");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
Looks like this:
(Tested on Linux Mint and Windows. Does not work on the Mac.)
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.
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: