SWT application with a GLCanvas not resizing properly - java

I'm making an application that is some sort of drawing program, and to that end I am attempting to use SWT with a GLCanvas widget in the shell. This is a snippet of it that shows the problem I'm having:
shell = new Shell();
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
shell.setLayout(gridLayout);
shell.setSize(1000, 800);
shell.setText(APPLICATION_NAME);
Label label = new Label(shell, SWT.NONE);
label.setText("Test");
Composite composite = new Composite(shell, SWT.NONE);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
composite.setLayoutData(gridData);
composite.setLayout(new FillLayout(SWT.HORIZONTAL));
GLData data = new GLData();
data.doubleBuffer = true;
canvas = new GLCanvas(composite, SWT.NO_BACKGROUND, data);
When I resize the window from this code, it does not display properly. The GLCanvas flickers, but it's also as if there's a step in the drawing where it draws the GLCanvas to the screen before it's been resized to properly fill the remaining white space. This has the effect where, for example, while the window is being resized to the left, the GLCanvas is smaller than it should be. Inversely, while the window is being resized to the right, the GLCanvas is larger than it should be and extends off into the right of the window.
I've tried setting the style of the canvas from SWT.NONE to SWT.NO_BACKGROUND to SWT.DOUBLE_BUFFERED to SWT.NO_REDRAW_RESIZE and it had no effect on this problem.
How can fix this behavior?

Related

What does the moveBelow method of Control class do?

I am using a Gridlayout with 2 column. I have Labels and corresponding Text control with it. I wanted the Text control of first label to slip down the label instead of right next to it (since its a gridlayout). For this I thought the moveBelow method would work but doesn't seem to be. Am i interpreting the use of the method wrongly?
Label label = Components.createLabel(myContainer, SWT.LEFT
| SWT.WRAP);
abel.setText("WC Plan Name");
textName = createTextControl(myContainer, SWT.LEFT);
textName.moveBelow(label);
private Text createTextControl(Composite parent, int horizontalAlignment)
{
final Text textControl = Components.createText(parent, SWT.SINGLE | SWT.BORDER);
final GridData layoutData = new GridData(horizontalAlignment, SWT.FILL, false, false);
layoutData.widthHint = 200;
textControl.setLayoutData(layoutData);
return textControl;
}
moveBelow() does exactly what it says in the documentation:
Moves the receiver below the specified control in the drawing order. If the argument is null, then the receiver is moved to the bottom of the drawing order. The control at the bottom of the drawing order will be covered by all other controls which occupy intersecting areas.
This means that it can be used to reorder children (if the layout of the parent allows it). For example, if you have a RowLayout and call moveBelow(null) on the last child, it will be moved to the top.
Now to solve your problem: You have a GridLayout with 2 columns. A GridLayout is filled from top left to bottom right. If you want two elements to appear below each other rather than next to each other, there are two options:
Add an empty Label in between, so that it can occupy the space to the right of your first element
Add a GridData to your first element and set GridData#horizontalSpan to 2. This way it will span two columns.
UPDATE
Here is an example of solution 2:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(4, false));
Text text = new Text(shell, SWT.BORDER);
text.setLayoutData(new GridData(SWT.BEGINNING, SWT.TOP, false, true, 4, 1));
text = new Text(shell, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 4, 1));
for (int i = 0; i < 8; i++)
{
new Button(shell, SWT.PUSH).setText("Button " + i);
}
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
Looks like this:

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 RowLayout: what is the minimum size?

I must be missing something stupid. The docs clearly state that the RowData object for the RowLayout layout lets you specify a minimum size (width and height), which makes sense. However, when the underlying widget exceeds this size, the size does not increases and the widget is cropped. Is it really a minimum?
Example
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Example");
shell.setBounds(100, 100, 325, 200);
shell.setLayout(new FillLayout());
Composite comp = new Composite(shell, SWT.BORDER);
comp.setLayout(new RowLayout(SWT.VERTICAL));
Label label1 = new Label(comp, SWT.CENTER);
label1.setLayoutData(new RowData(20,20));
label1.setText("Trying with bounded rowdata...");
Label label2 = new Label(comp, SWT.CENTER);
label2.setText("Trying with no rowdata...");
comp.layout(true,true); // no difference
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Result:
The javadoc of the constructor RowData(int, int) must be wrong! If you read carrefully the header of the RowData javadoc, it is said that RowData are used to set an initial size for Controls, not the minimum. That's why your Labelis croped by a square of 20x20 px!
Try turning packing off on the layout:
Composite comp = new Composite(shell, SWT.BORDER);
RowLayout layout = new RowLayout(SWT.VERTICAL);
layout.pack = false;
comp.setLayout(layout);
On the other hand you can also consider using a GridLayout if you want the controls to resize with respect to their parents but not less than a minimum width. The minimum width works with GridData.

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