how to split the screen to 3 parts in SWT? - java

I am new in SWT and java
I really need an help here.
I need to build Eclipse plug-in that should open a dialog when you press on button.
The dialog should look like
label 1 textBox1 label 2 textBox 2
label 3 textBox13 label 4 textBox 4
could be alot of them -> should be with scroller
---------------------------------------------------
output ( should be textbox)
-----------------------------------------------------
messages ( should be textbox)
It could be alot of labels and textbox, How I can add them to control that could hold alot of them ? ( it should be with scroller )
How I can split the screen to 3 parts in SWT or fjace ? and how I can control on the size for example that the first part ( label textbox) will be 60% and the output will be 30% and the messages 10% ?
Maybe you could help me with an example for this ?

This is asking for far too much code - you are supposed to show us what you have tried!
Some hints:
Use org.eclipse.jface.dialog.Dialog for the dialog, you could also use org.eclipse.jface.dialog.TitleAreaDialog which has an area for error messages.
To split an area by percentages use org.eclipse.swt.custom.SashForm.
To get multiple items on a line use org.eclipse.swt.layout.GridLayout specifying the number of columns.
To get a scrolled area use org.eclipse.swt.custom.ScrolledComposite
So something like:
#Override
protected Control createDialogArea(final Composite parent)
{
Composite body = (Composite)super.createDialogArea(parent);
// Vertical sash
SashForm sashForm = new SashForm(body, SWT.VERTICAL);
sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
// First part, scrollable
ScrolledComposite scrolledComp = new ScrolledComposite(sashForm, SWT.V_SCROLL);
Composite comp1 = new Composite(scrolledComp, SWT.NONE);
comp1.setLayout(new GridLayout());
// TODO: add controls to comp1
// Set scroll size - may need to adjust this
Point size = comp1.computeSize(SWT.DEFAULT, SWT.DEFAULT);
scrolledComp.setMinHeight(size.y);
scrolledComp.setMinWidth(size.x);
scrolledComp.setExpandVertical(true);
scrolledComp.setExpandHorizontal(true);
scrolledComp.setContent(comp1);
// Second part
Composite comp2 = new Composite(sashForm, SWT.NONE);
comp2.setLayout(new GridLayout());
// TODO: add controls to comp2
// Third part
Composite comp3 = new Composite(sashForm, SWT.NONE);
comp3.setLayout(new GridLayout());
// TODO: add controls to comp3
// Set the sash weighting (must be after controls are created)
sashForm.setWeights(new int [] {60, 30, 10});
return body;
}

Related

SWT - Not using first and second Column with GridLayout

I'm using SWT, on a desktop application, where I'm using a GridLayout splited by 5 columns and one line, there is a tabble using the top of the space and at the bottom I have 3 buttons, I want to put these 3 botton to the right. Basically jump the two first spaces. I tried using:
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment= GridData.END;
CompanyGroup.setLayoutData(gridData);
But I realized that this method only align inside the column.
Composite rightComp = DialogCompositeFactory.create(parent);
rightComp.setLayout(new GridLayout(1, false));
Group CompanyGroup = DialogGroupFactory.create(rightComp,Message));
CompanyGroup.setLayout(new GridLayout(5, true));
Like the example below
Just put empty label controls in the first two columns.
// Empty first column
new Label(CompanyGroup, SWT.LEAD);
// Empty second column
new Label(CompanyGroup, SWT.LEAD);
... your buttons in the last three columns
I assume you have a composite for buttons, if not create a composite for buttons and set a GridData with HORIZONTAL_ALIGN_END
Composite buttonsComposite = new Composite( groupComp, SWT.NONE );
GridData gd = new GridData( GridData.HORIZONTAL_ALIGN_END );
GridLayout lo = new GridLayout(3,true);
buttonsComp.setLayoutData( gd );
buttonsComp.setLayout(lo);

Keeping SWT Text box size after setting text

When I use setText() on one or both Text fields, it resizes the field to the length of the text. How do I prevent that from happening?
inner = new Composite(middle, SWT.NONE);
inner.setLayout(new GridLayout(5, false));
chkbxBtn = new Button(inner, SWT.CHECK);
chkbxBtn.setText("Check box button: ");
chkbxBtn.setSelection(false);
new Label(inner, SWT.NONE).setText("Text field 1: ");
startCol = new Text(inner, SWT.BORDER | SWT.NONE);
new Label(inner, SWT.NONE).setText("Text field 2: ");
endCol = new Text(inner, SWT.BORDER | SWT.NONE);
To clarify, SWT does not re-layout after changing the text (or any other property) of a Text control (or controls in general). It is your code or a resize event that causes the re-layout.
If you want a control to have a pre-set size, and its parent uses a GridLayout, you can set GridData with a widthHint like this:
GridData gridData = new GridData();
gridData.widthHint = ...
text.setLayoutData( gridData );
However, it is usually a bad idea trying to control the size of widgets. Thus make sure that your layout strategy aligns with best practices of UI design.
There are situations when changes to the text trigger a component re-layout. For example, show an error message as a text validation result (I found it in SWT forms).
Setting the width hint in the GridData for the text component fixes this. Width hint can be set to 0 or the minimum required size.
GridDataFactory.swtDefaults()//
.grab(true, false)//
.hint(0, SWT.DEFAULT)// width hint prevents text from expanding to full line
.align(SWT.FILL, SWT.CENTER)//
.applyTo(text);

SWT Button image limit

When an image is set on a SWT Button and the image is bigger (in this case the width), the limits of the button are not honored, and the image from one button can "touch" the image from another.
Strangely, if SWT.LEFT or SWT.RIGHT is used, the problem does no occur on the respective side. Tried with SWT.CENTER, without success.
Is there any "workaround" to bypass this problem? Or is just something that any SWT user must get used to?
Composite Code:
GridLayout gridLayout = new GridLayout(1, true);
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.verticalSpacing = 0;
setLayout(gridLayout);
setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button Code:
Button button=new Button(this, SWT.PUSH | SWT.WRAP);
GridData gridData=new GridData(SWT.FILL, SWT.FILL, true, false);
gridData.widthHint=size.width;
gridData.heightHint=size.height;
button.setLayoutData(gridData);
I also have a listener associated with the Composite Layout, to adjust the Number of Columns, but i assume that's irrelevant.
Using a Button for this task doesn't seem to be the best choice (because of the borders).
You might be better off using a Label to host the Image. Your code will only change very little, since you only need to replace all occurrences of Button with Label.

SWT - Creating a grid of numbers that I'd like to be able to tab through with keyboard

This problem has been driving me CRAZY!!! Using SWT, I'd like to create a grid of numbers that I can tab traverse using the keyboard. I'd like to be able to select and click into each cell (not just on the numbers) to perform some action. In other words, I'm looking for a way to make a true FLAT button. The goal here is to make the grid accessible with screen readers such that when a cell has focus, the screen reader will read the number value in the middle of each cell.
Originally, I created a GridLayout with a set of button controls to display the number values. This actually worked but I didn't want the buttons to look like buttons. The SWT.FLAT style for button controls doesn't work on Windows operating systems. This is caused by an OS limitation.
Next I then tried converting all buttons to labels but since label controls cant take focus, I couldn't implement any type of tab traversal. Next I tried replacing all labels with read-only text controls. I was able to re-introduce the tab traversal/focusing but I can't get the text to display in the dead-center of each cell. This is actually caused by a SWT limitation. The SWT.CENTER style for text controls only affects the horizontal alignment. Text controls can't be vertically aligned.
So finally, someone told me to wrap each read-only text control in a Composite. This allowed me to center the read-only text controls but I don't know how to make the composite itself to be tab traversed.
Any ideas on how I can get this done or how I can make controls that typically dont take focus (like composite, canvas, label) actually take focus so I can tab traverse each control with my keyboard?
I'm fairly new to java and SWT so I apologize if some of this is confusing. Many thanks. This is how I've constructed each of my cells thus far (I've replaced the read-only text with CLabel controls):
Display display = new Display();
Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout(5, false);
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.horizontalSpacing = 1;
gridLayout.verticalSpacing = 1;
shell.setLayout(gridLayout);
Composite resetComp = new Composite(shell, SWT.BORDER);
GridData compgridData = new GridData(SWT.CENTER, SWT.CENTER, true, true);
GridData resetGD = new GridData(SWT.FILL, SWT.FILL, false, false);
resetGD.verticalSpan = 2;
resetComp.setLayoutData(resetGD);
resetComp.setLayout(new GridLayout(1, false));
CLabel resetCLabel = new CLabel(resetComp, SWT.SHADOW_OUT | SWT.CENTER);
resetCLabel.setText("Reset"); //$NON-NLS-1$
resetCLabel.setLayoutData(compgridData);
resetCLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED)); //$NON-NLS-1$
resetComp.setToolTipText(resetCLabel.getText());
Here is a trick, maybe it will suits you: create each button inside a Composite, and set the size of the Button bigger than its parent Composite; This way, the border of button is not visible, because outside of its parent composite.
private static final int GRID_SIZE = 5;
private static final int CELL_SIZE = 40;
private static final int PADDING = 3;
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new GridLayout(GRID_SIZE, true));
for (int i = 0; i < GRID_SIZE * GRID_SIZE; i++) {
createButton(shell, i+1);
}
shell.pack ();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
private static void createButton(Shell shell, final int number) {
Composite c = new Composite(shell, SWT.NONE);
c.setLayoutData(new GridData(CELL_SIZE, CELL_SIZE));
Button button = new Button(c, SWT.NONE);
button.setText(Integer.toString(number));
button.setBounds(-PADDING, -PADDING, CELL_SIZE + 2 * PADDING, CELL_SIZE + 2 * PADDING);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.err.println("click on " + number);
}
});
}
It fulfills your requirement: focus, centered. The result may be more or less nice depending on the Windows version (try to change the padding, or add a border to the Composite, it may look nicer).

How to have text fields fill 100% horizontally

If I have a text field with SWT, how can I get the field to fill to 100% or some specified width.
For example, this text field will only reach for so much horizontally.
public class Tmp {
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
GridLayout gridLayout = new GridLayout ();
shell.setLayout (gridLayout);
Button button0 = new Button(shell, SWT.PUSH);
button0.setText ("button0");
Text text = new Text(shell, SWT.BORDER | SWT.FILL);
text.setText ("Text Field");
shell.setSize(500, 400);
//shell.pack();
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ())
display.sleep ();
}
display.dispose ();
}
}
Make something like this:
Text text = new Text(shell, SWT.BORDER);
text.setText ("Text Field");
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER));
/: Since this is the accepted answer I remove the errors. Thx for correcting me.
Positioning of elements in a Component depends on the Layout object that you are using. In the sample provided, you are using a GridLayout. That means, that you need to provide a specific LayoutData object to indicate how you want your component displayed. In the case of GridLayout, the object is GridData.
To achieve what you want, you must create a GridData object that grabs all horizontal space and fills it:
// Fills available horizontal and vertical space, grabs horizontal space,grab
// does not grab vertical space
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
text.setLayoutData(gd);
Alternative ways include using a different LayoutManager, such as FormLayout. This layout uses a FormData object that also allows you to specify how the component will be placed on the screen.
You can also read this article on Layouts to understand how Layouts work.
As a side note, the constructor new GridData(int style) is marked as "not recommended" in the documentation. The explicit constructor shown in this example is preferred instead.

Categories

Resources