Home » Eclipse Projects » Standard Widget Toolkit (SWT) » custom control objects- their children, and FocusListener
custom control objects- their children, and FocusListener [message #446427] |
Thu, 25 November 2004 06:43 |
Liam Morley Messages: 47 Registered: July 2009 |
Member |
|
|
I have a class that subclasses Composite. It has a bit of drawing, and
it also contains a Label object. I'd like to add a focus listener to the
class, as well as a context menu by way of MouseListener. It seems
however that I can't simply add these listeners to the Composite itself-
when I do this, I can't right-click on the Label. The only way I know of
to get around this is to add the same mouse listener to the label
itself, but it seems that since the label is part of the Composite and a
child of the Composite, it should invoke the mouse listener for the
parent composite.
I'm having another problem with FocusListener- it seems that, with the
following implementation, only right-clicking on the composite will fire
focusGained or focusLost. Is there any reason for this? (I've also tried
adding a ControlListener to this, and that doesn't seem to work at all-
no amount of clicking and dragging at the edges of my component seems to
have any effect.)
This is being run from within a view, using the following code from
within createPartControl:
sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL
| SWT.BORDER);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
panel = new Composite(sc, SWT.NONE);
sc.setContent(panel);
Any and all advice would be greatly appreciated, it feels like I'm just
missing one or two key details.
public class MyComposite extends Composite {
private class ContextMenuConstructor extends MouseAdapter {
public void mouseUp(MouseEvent e) {
MyComposite.this.constructContextMenu(e);
}
}
private class OBFocusListener implements FocusListener {
public void focusGained(FocusEvent fe) {
getComp(fe.getSource()).focusGained();
}
public void focusLost(FocusEvent fe) {
getComp(fe.getSource()).focusLost();
}
private MyComposite getComp(Object source) {
MyComposite comp = null;
if (source instanceof Instance)
instance = (Instance) source;
else if (source instanceof Label)
instance = (Instance) ((Label) source).getParent();
return comp;
}
}
public MyComposite(Composite parent, Object obj, String name) {
super(parent, SWT.NONE);
nameLabel = new Label(this, SWT.NONE);
nameLabel.setText("name");
nameLabel.setFont(font);
addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
MyComposite.this.paintControl(e);
}
});
OBFocusListener fl = new OBFocusListener();
addFocusListener(fl);
//nameLabel.addFocusListener(fl);
ContextMenuConstructor cmc = new ContextMenuConstructor();
addMouseListener(cmc);
//nameLabel.addMouseListener(cmc);
}
protected void focusLost() {
System.out.println("fLost");
}
protected void focusGained() {
System.out.println("fGained");
}
protected void constructContextMenu(MouseEvent e) {
Menu menu = new Menu(this);
new MenuItem(menu, SWT.SEPARATOR); // do other stuff too
menu.setLocation(e.display.getCursorLocation());
menu.setVisible(true);
}
protected void paintControl(PaintEvent e) {
// draw bg rectangle
GC gc = e.gc;
gc.setBackground(bgColorActive);
gc..fillRoundRectangle(1, 1, DEFAULT_WIDTH - 1,
DEFAULT_HEIGHT - 1, 4, 4);
}
}
--
Liam Morley
Computer Science Undergraduate
Worcester Polytechnic Institute
|
|
|
Re: custom control objects- their children, and FocusListener [message #446540 is a reply to message #446427] |
Thu, 25 November 2004 15:40 |
Steve Northover Messages: 1636 Registered: July 2009 |
Senior Member |
|
|
"Liam Morley" <lmorley@wpi.edu> wrote in message
news:co3uum$tm6$1@www.eclipse.org...
> I have a class that subclasses Composite. It has a bit of drawing, and
> it also contains a Label object. I'd like to add a focus listener to the
> class, as well as a context menu by way of MouseListener. It seems
> however that I can't simply add these listeners to the Composite itself-
> when I do this, I can't right-click on the Label. The only way I know of
> to get around this is to add the same mouse listener to the label
> itself, but it seems that since the label is part of the Composite and a
> child of the Composite, it should invoke the mouse listener for the
> parent composite.
>
Event are not inherited in SWT by design. You'll need to add the listeners
to the child widgets.
> I'm having another problem with FocusListener- it seems that, with the
> following implementation, only right-clicking on the composite will fire
> focusGained or focusLost. Is there any reason for this? (I've also tried
> adding a ControlListener to this, and that doesn't seem to work at all-
> no amount of clicking and dragging at the edges of my component seems to
> have any effect.)
>
Controls won't take focus unless you add a key listener.
> This is being run from within a view, using the following code from
> within createPartControl:
>
> sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL
> | SWT.BORDER);
> sc.setExpandHorizontal(true);
> sc.setExpandVertical(true);
> panel = new Composite(sc, SWT.NONE);
> sc.setContent(panel);
>
> Any and all advice would be greatly appreciated, it feels like I'm just
> missing one or two key details.
>
>
> public class MyComposite extends Composite {
> private class ContextMenuConstructor extends MouseAdapter {
> public void mouseUp(MouseEvent e) {
> MyComposite.this.constructContextMenu(e);
> }
> }
>
> private class OBFocusListener implements FocusListener {
> public void focusGained(FocusEvent fe) {
> getComp(fe.getSource()).focusGained();
> }
>
> public void focusLost(FocusEvent fe) {
> getComp(fe.getSource()).focusLost();
> }
> private MyComposite getComp(Object source) {
> MyComposite comp = null;
> if (source instanceof Instance)
> instance = (Instance) source;
> else if (source instanceof Label)
> instance = (Instance) ((Label) source).getParent();
> return comp;
> }
> }
>
> public MyComposite(Composite parent, Object obj, String name) {
> super(parent, SWT.NONE);
> nameLabel = new Label(this, SWT.NONE);
> nameLabel.setText("name");
> nameLabel.setFont(font);
>
> addPaintListener(new PaintListener() {
> public void paintControl(PaintEvent e) {
> MyComposite.this.paintControl(e);
> }
> });
>
> OBFocusListener fl = new OBFocusListener();
> addFocusListener(fl);
> //nameLabel.addFocusListener(fl);
>
> ContextMenuConstructor cmc = new ContextMenuConstructor();
> addMouseListener(cmc);
> //nameLabel.addMouseListener(cmc);
> }
>
> protected void focusLost() {
> System.out.println("fLost");
> }
>
> protected void focusGained() {
> System.out.println("fGained");
> }
>
> protected void constructContextMenu(MouseEvent e) {
> Menu menu = new Menu(this);
> new MenuItem(menu, SWT.SEPARATOR); // do other stuff too
> menu.setLocation(e.display.getCursorLocation());
> menu.setVisible(true);
> }
>
> protected void paintControl(PaintEvent e) {
> // draw bg rectangle
> GC gc = e.gc;
> gc.setBackground(bgColorActive);
> gc..fillRoundRectangle(1, 1, DEFAULT_WIDTH - 1,
> DEFAULT_HEIGHT - 1, 4, 4);
> }
> }
>
> --
> Liam Morley
> Computer Science Undergraduate
> Worcester Polytechnic Institute
|
|
| | |
Goto Forum:
Current Time: Thu Mar 28 18:12:12 GMT 2024
Powered by FUDForum. Page generated in 0.04958 seconds
|