Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
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 Go to next message
Liam Morley is currently offline Liam MorleyFriend
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 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
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
Re: custom control objects- their children, and FocusListener [message #446553 is a reply to message #446540] Fri, 26 November 2004 11:04 Go to previous messageGo to next message
Liam Morley is currently offline Liam MorleyFriend
Messages: 47
Registered: July 2009
Member
Thanks for the response. Let me make sure I understand this-
"FocusListener" is only associated with keyboard focus? I want to
respond when my control is clicked on, so I imagine I should be using
"SelectionListener". As composite objects don't have an
"addSelectionListener" method, I tried using
"Widget.addListener(SWT.Selection...)", but this never fired at all. How
do I take care of control selection, outside of a list or tree?


Steve Northover wrote:
>
> Controls won't take focus unless you add a key listener.
>

--
Liam Morley
Computer Science Undergraduate
Worcester Polytechnic Institute
Re: custom control objects- their children, and FocusListener [message #446564 is a reply to message #446553] Fri, 26 November 2004 15:23 Go to previous message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
You are right, "focus" means keyboard focus. If you add a key listener,
your control will take focus when you click on it. After that, it's up to
you do decide what selection means for your custom control and use
Control.notifyListeners() to send SWT.Selection when your code has decided
it occurs.

"Liam Morley" <lmorley@wpi.edu> wrote in message
news:co72ki$qvo$1@www.eclipse.org...
> Thanks for the response. Let me make sure I understand this-
> "FocusListener" is only associated with keyboard focus? I want to
> respond when my control is clicked on, so I imagine I should be using
> "SelectionListener". As composite objects don't have an
> "addSelectionListener" method, I tried using
> "Widget.addListener(SWT.Selection...)", but this never fired at all. How
> do I take care of control selection, outside of a list or tree?
>
>
> Steve Northover wrote:
> >
> > Controls won't take focus unless you add a key listener.
> >
>
> --
> Liam Morley
> Computer Science Undergraduate
> Worcester Polytechnic Institute
Previous Topic:Mama mia
Next Topic:SWT Browser Widget: how to detect selection of text?
Goto Forum:
  


Current Time: Thu Nov 26 04:55:21 GMT 2020

Powered by FUDForum. Page generated in 0.02220 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top