Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Create Control Dynamically
Create Control Dynamically [message #720484] Tue, 30 August 2011 17:09 Go to next message
Balaji  is currently offline Balaji Friend
Messages: 17
Registered: August 2011
Junior Member
Hi
I want to dynamically create 'n' number of control(say TextBox or Label) and want to have an action to all Controls.

Example
--------
1. Dynamically Create 10 Textbox
2. On CARRIAGE return of it perform some operation (addKeyListener)
(Edited)

My Question
-----------
How to I add action to those created controls?
Is there any Control Array(same as VB)

[Updated on: Wed, 31 August 2011 02:59]

Report message to a moderator

Re: Create Control Dynamically [message #720728 is a reply to message #720484] Wed, 31 August 2011 06:52 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by:

On 2011-08-30 19:09, Balaji wrote:
> Hi I want to dynamically create 'n' number of control(say TextBox or
> Label) and want to have an action to all Controls.

1) There is no 'TextBox' in SWT, do you mean the
org.eclipse.swt.widgets.Text widget?
2) You don say what kind of "actions" you are referring to. Do you want
to register a SelectionListener or a generic Listener?

> Example
> --------
> 1. Dynamically Create 10 Textbox
>
> 2. Specific 5 TextBox has to perform some action on CARRIAGE RETURN and
> other 5 perform some action on ESCAPE key
>
> My Question
> -----------
> How to I add action to those created controls?

I don't understand the question. If you are writing a loop to generate
the control, why is there there a problem to add the listeners within
the loop?

> Is there any Control Array(same as VB)

I don't know, what a Control Array is, but you can simply create a
java.util.List<org.eclipse.swt.widgets.Control> if you really need to.

HTH & Greetings from Bremen,

- Daniel Krügler
Re: Create Control Dynamically [message #721889 is a reply to message #720728] Sat, 03 September 2011 11:56 Go to previous messageGo to next message
Balaji  is currently offline Balaji Friend
Messages: 17
Registered: August 2011
Junior Member
Hi
Thanks.

Here is the sample code, where I want to create a 'n' Label and want to perform a move operation on selected Label.

Example

  Label label;

  Listener listener = new Listener () {
    public void handleEvent (Event event) {
       ...
       // MY HANDLER HERE
       ...
       ...
    }
  }

  
  for(...) 
  { 
     label = new Label(composite, SWT.NONE);
     label.addListener (SWT.MouseDown, listener);
  }



Please correct me if anything is wrong.

My Doubt
--------
In the above case, in the handler how do I know on which Label its operating?
Re: Create Control Dynamically [message #722333 is a reply to message #721889] Mon, 05 September 2011 12:06 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by:

On 2011-09-03 13:56, Balaji wrote:
> Hi
> Thanks.
> Here is the sample code, where I want to create a 'n' Label and want to
> perform a move operation on selected Label.
>
> Example
>
>
> Label label;
>
> Listener listener = new Listener () {
> public void handleEvent (Event event) {
> ...
> // MY HANDLER HERE
> ...
> ...
> }
> }
>
>
> for(...) { label = new Label(composite, SWT.NONE);
> label.addListener (SWT.MouseDown, listener);
> }
>
>
>
> Please correct me if anything is wrong.
>
> My Doubt
> --------
> In the above case, in the handler how do I know on which Label its
> operating?

The event handler function provides an Event object. Look at the member
"widget" to get access to the widget that had caused the event to fire.

HTH & Greetings from Bremen,

- Daniel Krügler
Re: Create Control Dynamically [message #723168 is a reply to message #722333] Wed, 07 September 2011 22:01 Go to previous message
John Steele is currently offline John SteeleFriend
Messages: 33
Registered: June 2010
Location: Seattle, WA
Member
You're probably better off using a Typed Listener as apposed to an Untyped Listener.

You could use a general MouseListener like so:
MouseListener listener = new MouseListener() {
				
	@Override
	public void mouseUp(MouseEvent e) {}
				
	@Override
	public void mouseDown(MouseEvent e) {}
				
	@Override
	public void mouseDoubleClick(MouseEvent e) {
		Object o = e.getSource();
		if (o instanceof Label) {
			Label label = (Label) o;						
			if (label.getText().equals("...")) {							
			}
		}
	}
};
		
label.addMouseListener(listener);


Or you can use the MouseAdapter:
MouseAdapter adapter = new MouseAdapter() {
	@Override
	public void mouseUp(MouseEvent e) {
		Object o = e.getSource();
		if (o instanceof Label) {
			Label source = (Label) o;
			if (source.getText().equals("...")) {
					//...
			}
		}
	}
};
label.addMouseListener(adapter);


All I'm doing here is using the name of the label to identify which label was clicked on.
Hope this helps.
(no subject) [message #723170 is a reply to message #722333] Wed, 07 September 2011 22:01 Go to previous message
John Steele is currently offline John SteeleFriend
Messages: 33
Registered: June 2010
Location: Seattle, WA
Member
You're probably better off using a Typed Listener as apposed to an Untyped Listener.

You could use a general MouseListener like so:

MouseListener listener = new MouseListener() {

@Override
public void mouseUp(MouseEvent e) {}

@Override
public void mouseDown(MouseEvent e) {}

@Override
public void mouseDoubleClick(MouseEvent e) {
Object o = e.getSource();
if (o instanceof Label) {
Label label = (Label) o;
if (label.getText().equals("...")) {
}
}
}
};

label.addMouseListener(listener);


Or you can use the MouseAdapter:

MouseAdapter adapter = new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
Object o = e.getSource();
if (o instanceof Label) {
Label source = (Label) o;
if (source.getText().equals("...")) {
//...
}
}
}
};
label.addMouseListener(adapter);


All I'm doing here is using the name of the label to identify which label was clicked on.
Hope this helps.
Previous Topic:Flat button inside toolbar
Next Topic:Background image on Composite
Goto Forum:
  


Current Time: Fri Mar 29 06:41:22 GMT 2024

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

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

Back to the top