Selective menu detection within a Composite - java

I want to add some objects into an Composite object. Objects can have different shapes (rectangle, circle, ellipse or even weird shape (represented by a polygon). So I defined classes like this:
public class Circle extends Canvas {
}
public class Rectangle extends Canvas {
}
...
I know how to draw in a Canvas to get the shape I want, but I also expected that the popup menu appears at each canvas only if the users click mouse inside the canvas area, so if I use these code in a composite class:
Menu aSampleMenu = new Menu(this);
Circle circle = new Circle(parent, style);
circle.setMenu(aSampleMenu);
the menu will appear if the user click right mouse button anywhere inside the canvas, even outside the shape area. How can I fix this problem?

Have a look at the code snippet below. The trick is defining the Menu first and then setting it only to those Controls that should allow menu detection:
public class StackOverflow
{
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(2, true));
Composite c1 = new Composite(shell, SWT.BORDER);
c1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Composite c2 = new Composite(shell, SWT.BORDER);
c2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Menu menu = new Menu(shell, SWT.POP_UP);
MenuItem item = new MenuItem(menu, SWT.PUSH);
item.setText("Popup");
new Label(shell, SWT.BORDER).setText("No menu here");
new Label(shell, SWT.BORDER).setText("No menu here");
// Add menu only to c1 and c2, not to the shell and not to the labels
c1.setMenu(menu);
c2.setMenu(menu);
shell.setSize(300, 300);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Here is a screenshot:

One solution is using Region, but I still dont figure out how it can be applied to circle case:
Region region = new Region();
region.add(new int[] {3, 3, 20, 20, 3, 20});
Canvas c = new Canvas(this, SWT.NONE);
c.setBounds(35, 35, 60, 60);
c.setRegion(region);
Menu menu = new Menu(this);
c.setMenu(menu);
MenuItem mntmProperties = new MenuItem(menu, SWT.NONE);
mntmProperties.setText("Properties");
MenuItem mntmDelete = new MenuItem(menu, SWT.NONE);
mntmDelete.setText("Delete");

Related

How can I show Scroll on my UI, with JAVA, SWT?

I'm trying to put a ScrolledComposite on my UI with SWT.
but now it only shows blank area or something wrong.
The area to show ScrolledComposite is blank without the code,
"optionCompositeAtLeft.setSize(optionCompositeAtLeft.computeSize(SWT.DEFAULT, SWT.DEFAULT)); "
This is the pic of screenshot.
The optionTab should have the buttons with Scroll....
and with the code, it shows scroll, but too long height and narrow width.
Somehow, the upper is gone, but come back when I extend the size of window with scroll disappearing.
I set the color of background red to make it easier to see for you.
Can anyone help me make "optionCompositeAtLeft" filled in ScrolledComposite?
I want the left area of Option tab to be filled with a red background from "optionCompositeAtLef".
Below is my code.
private BomCloneDialog dialog = null;
private TabFolder tabFolder = null;
private TabItem tabOption = null;
public CreateOptionTab(TabFolder tabFolder, TabItem tabOption) {
this.tabFolder = tabFolder;
this.tabOption = tabOption;
}
public void createOptionTabUI() {
GridData gdWithFillBoth = new GridData(GridData.FILL_BOTH);
Composite optionComposite = new Composite(tabFolder, SWT.NONE);
optionComposite.setLayout(new GridLayout(2, true));
tabOption.setControl(optionComposite);
ScrolledComposite scrolledCompositeForOption = new ScrolledComposite(optionComposite,SWT.V_SCROLL | SWT.BORDER);
scrolledCompositeForOption.setLayoutData(gdWithFillBoth);
Composite optionCompositeAtLeft = new Composite(scrolledCompositeForOption, SWT.BORDER);
scrolledCompositeForOption.setContent(optionCompositeAtLeft);
optionCompositeAtLeft.setLayout(new GridLayout(1, true));
optionCompositeAtLeft.setLayoutData(gdWithFillBoth);
optionCompositeAtLeft.setBackground(new Color(Display.getCurrent(), 255,0,0));
Button b [] = new Button[30];
for(int a=0; a<30; a++){
b[a] = new Button(optionCompositeAtLeft, SWT.PUSH);
b[a].setText("button"+a);
}
**optionCompositeAtLeft.setSize(optionCompositeAtLeft.computeSize(SWT.DEFAULT, SWT.DEFAULT));**
Composite optionButtonComposite = new Composite(optionComposite, SWT.BORDER);
optionButtonComposite.setLayout(new GridLayout(1, true));
optionButtonComposite.setLayoutData(gdWithFillBoth);
Button optionSaveButton = new Button(optionButtonComposite, SWT.NONE);
optionSaveButton.setLayoutData(gdWithFillBoth);
optionSaveButton.setText("Save");
Button optionAddButton = new Button(optionButtonComposite, SWT.NONE);
optionAddButton.setLayoutData(gdWithFillBoth);
optionAddButton.setText("Add");
}
You need to configure ScrolledComposite to resize the content:
scrolledCompositeForOption.setExpandHorizontal(true);
scrolledCompositeForOption.setExpandVertical(true);

SWT: Resize Table (height) using a MouseListener

I'm new to SWT and want to make an application with two vertical composites (or somethink like that).
The the composite at the buttom caintains a simple Table. Now I need a variable height for the complete table - the user should determine the height by using the Mouse - just like it's possible to resize the eclipse views. The composite on the top should adjust the space.
Is somethinkg like that possible with SWT? If yes, I would be thankful for every suggestion.
You can use a SashForm for this. It's user resizable.
Here is an example.
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
final SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);
Text text1 = new Text(sashForm, SWT.CENTER);
text1.setText("Text in pane #1");
Text text2 = new Text(sashForm, SWT.CENTER);
text2.setText("Text in pane #2");
final SashForm sashForm2 = new SashForm(sashForm, SWT.VERTICAL);
final Label labelA = new Label(sashForm2, SWT.BORDER | SWT.CENTER);
labelA.setText("Label in pane A");
final Label labelB = new Label(sashForm2, SWT.BORDER | SWT.CENTER);
labelB.setText("Label in pane B");
sashForm.setWeights(new int[] { 1, 2, 3 });
new Label(shell, SWT.NONE).setText("Label");
shell.pack();
shell.setSize(400, 300);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

Input form using FormLayout in swt

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:

How can we auto resize the size of components in SWT?

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:

why are widgets placed on the TabItem invisible when using ScrolledComposite SWT?

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...

Categories