Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » dynamic combo generation(swt multiple combo box generation)
dynamic combo generation [message #713282] Mon, 08 August 2011 09:53 Go to next message
Sankar Raman V is currently offline Sankar Raman VFriend
Messages: 15
Registered: July 2011
Junior Member
hi,


i have string values list or array where each value indexes to few more sub values... This goes on until 5 or 6 levels may be.


my req. is to store first set of strings in combo box and when user selects each of the list element it should generate new combo with list of second elements set and when a value is selected fro second set, third set of combo should be created and so on.


where if i use selection listener to handle event of combo box, appreciate the logic for dynamic creation of the same where the number of values and levels is variable and unknown and form should also correspondingly expand for the same.


Thanks a lot in advance, pls help.
Re: dynamic combo generation [message #713899 is a reply to message #713282] Tue, 09 August 2011 10:37 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Widget;

public class SWTComboExample implements SelectionListener {
	private static final String CHILD = "CHILD";

	Display d;

	Shell s;

	private Composite top;

	SWTComboExample() {
		d = new Display();
		s = new Shell(d);
		s.setSize(250, 250);
		s.setText("A Combo Example");
		ScrolledComposite sc= new ScrolledComposite(s, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);		
		top = new Composite(sc, SWT.None);
		sc.setContent(top);
		
		top.setBackground(d.getSystemColor(SWT.COLOR_WHITE));
		s.setLayout(new FillLayout());
		top.setLayout(new RowLayout());
		final Combo c = new Combo(top, SWT.READ_ONLY);

		String items[] = { "Item One", "Item Two", "Item Three", "Item Four",
				"Item Five" };
		c.setItems(items);
		c.addSelectionListener(this);
		top.setSize(top.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		s.open();
		while (!s.isDisposed()) {
			if (!d.readAndDispatch())
				d.sleep();
		}
		d.dispose();
	}

	public static void main(String[] argv) {
		new SWTComboExample();
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void widgetSelected(SelectionEvent arg0) {
		Combo parent = (Combo) arg0.widget;
		Combo data = (Combo) parent.getData(CHILD);

		if (data == null) {
			Combo c = new Combo(top, SWT.READ_ONLY);

			String items[] = { "Item One", "Item Two", "Item Three",
					"Item Four", "Item Five" };
			c.setItems(items);
			c.addSelectionListener(this);
			arg0.widget.setData(CHILD, c);

		} else {
			data.deselectAll();
			removeChildren(data);
		}
		top.setSize(top.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		s.layout();
	}

	private void removeChildren(Combo parent) {
		Combo data = (Combo) parent.getData(CHILD);
		if (data != null) {
			removeChildren(data);
			data.dispose();
			parent.setData(CHILD, null);
		} 
	}
}


Its just a hint for you...
use what ever layout you want and accordingly modify.


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: dynamic combo generation [message #714010 is a reply to message #713899] Tue, 09 August 2011 15:27 Go to previous messageGo to next message
Sankar Raman V is currently offline Sankar Raman VFriend
Messages: 15
Registered: July 2011
Junior Member
thats great, helps a lot...

thanks a many


one more doubt is if the combo items are variable,

say like this:


item1 -> item2, item3
item2 -> item4, item5
item3 -> item6, item7
...


in this case input is something like a tree or index to be passed on, how that work out inputs to widgetSelected varies i think?

thank you agian

[Updated on: Tue, 09 August 2011 15:30]

Report message to a moderator

Re: dynamic combo generation [message #714238 is a reply to message #714010] Wed, 10 August 2011 06:34 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
Why to use combos?why to simplify by using a tree control?

---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: dynamic combo generation [message #714258 is a reply to message #714238] Wed, 10 August 2011 07:09 Go to previous messageGo to next message
Sankar Raman V is currently offline Sankar Raman VFriend
Messages: 15
Registered: July 2011
Junior Member
hi im trying something like sequence generation where from UI, user selects some sequence and when a button is clicked its generating sequence and store in file so here data is in hashmap or tree format and hence preferred combo to select dynamic
Re: dynamic combo generation [message #714262 is a reply to message #714258] Wed, 10 August 2011 07:18 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
This can be done in a tree also,with each node having variable children and on expand or show ,existense of children or showing children can be done..
it would be much cleaner and much simpler.
(offcourse combo one will be much fancier,if thats what u prefer Smile)


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: dynamic combo generation [message #714284 is a reply to message #714262] Wed, 10 August 2011 08:24 Go to previous messageGo to next message
Sankar Raman V is currently offline Sankar Raman VFriend
Messages: 15
Registered: July 2011
Junior Member
ya something fancier Smile but my doubt here is how it could be done with combo and also need to resetting the form as once a sequence is complete clicking a button to reset form and write text to file, it continues...
Re: dynamic combo generation [message #714295 is a reply to message #714284] Wed, 10 August 2011 09:12 Go to previous messageGo to next message
Sankar Raman V is currently offline Sankar Raman VFriend
Messages: 15
Registered: July 2011
Junior Member
well one more reason is if there are many number of values for a combo only one is visible but in tree expand many will occupy, hence difficult to pick and comfortable in combo to scroll that why.... can you please let know as well how to do this....
Re: dynamic combo generation [message #714348 is a reply to message #714295] Wed, 10 August 2011 11:46 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
In the below code
@Override
	public void widgetSelected(SelectionEvent arg0) {
		Combo parent = (Combo) arg0.widget;
		Combo data = (Combo) parent.getData(CHILD);

		if (data == null) {
			Combo c = new Combo(top, SWT.READ_ONLY);

			String items[] = { "Item One", "Item Two", "Item Three",
					"Item Four", "Item Five" };
			c.setItems(items);
			c.addSelectionListener(this);
			arg0.widget.setData(CHILD, c);

		} else {
			data.deselectAll();
			removeChildren(data);
		}
		top.setSize(top.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		s.layout();
	}


instead of creating new combo's on top composite create them on a new composite,
this new composite will have a row layout(verticle) and will have further coposites as parent for each combo with rowlayout(horizontal)..
this will go on recursively...
also u have modify the info in setdata accordingly.

i want u to try it,if not able to do,will help,its easy to spend time and give u the code,but u would not learn from that so try Smile


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: dynamic combo generation [message #714486 is a reply to message #714348] Wed, 10 August 2011 16:05 Go to previous messageGo to next message
Sankar Raman V is currently offline Sankar Raman VFriend
Messages: 15
Registered: July 2011
Junior Member
thanks ya again will try out and if anything din work out then ask, sure... Smile
Re: dynamic combo generation [message #715029 is a reply to message #714486] Fri, 12 August 2011 07:21 Go to previous messageGo to next message
Sankar Raman V is currently offline Sankar Raman VFriend
Messages: 15
Registered: July 2011
Junior Member
	public void widgetSelected(SelectionEvent arg0) {
		Combo parent = (Combo) arg0.widget;
		Combo data = (Combo) parent.getData(CHILD);

		if (data == null) {

			String[] temp = wholeData.get(parent.getText());
			if (temp != null) {
				Combo c = new Combo(top, SWT.READ_ONLY);
				c.setItems(temp);
				c.addSelectionListener(this);
				arg0.widget.setData(CHILD, c);
			} 

		} else {
			data.deselectAll();
			removeChildren(data);
		}
		top.setSize(top.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		s.layout();
	}


wholeData is a HashTable.

i tried like the same and its working but the issue here is suppose we select 1st combo input, 2nd combo pops up and when 2nd combo input selected, 3rd combo pops up, etc.. after selecting upto a point suppose if choose some other element in intermediate combo, it removes all sublevel combos except the next one... but the last combo items does not get updated.... and shows value for previous iteration only... only when we click again on top level combo, we can go again correctly


also i would like to have a label dynamically showing aggregated string of all combo texts, is that possible...?
Re: dynamic combo generation [message #715972 is a reply to message #715029] Tue, 16 August 2011 06:30 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
The fix is in this part of the code
else {
data.deselectAll();
removeChildren(data);
}


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: dynamic combo generation [message #715997 is a reply to message #715972] Tue, 16 August 2011 07:48 Go to previous messageGo to next message
Sankar Raman V is currently offline Sankar Raman VFriend
Messages: 15
Registered: July 2011
Junior Member
hi tried somethings like parent.setData(temp), data.removeSelectionListener(this), data.setData(null) which does nt work out, pls help....
Re: dynamic combo generation [message #715999 is a reply to message #715997] Tue, 16 August 2011 07:48 Go to previous messageGo to next message
Sankar Raman V is currently offline Sankar Raman VFriend
Messages: 15
Registered: July 2011
Junior Member
also the other parts of getting into a label the combo values dynamically... etc..
Re: dynamic combo generation [message #716003 is a reply to message #715999] Tue, 16 August 2011 08:02 Go to previous messageGo to next message
Sankar Raman V is currently offline Sankar Raman VFriend
Messages: 15
Registered: July 2011
Junior Member
i tried and this is working:

data.deselectAll();

//removeChildren(data);

removeChildren(parent);

parent.addSelectionListener(this);


is this optionmal fix?

then reg. other things>?
Re: dynamic combo generation [message #716232 is a reply to message #716003] Tue, 16 August 2011 18:06 Go to previous messageGo to next message
Sankar Raman V is currently offline Sankar Raman VFriend
Messages: 15
Registered: July 2011
Junior Member
I tried to populate combo values into a vector and print tem ina button click and reset combos for next time so that again a sequence can be formed and button is clicked to print it.,.


here the problem i see is populating into vector is missing a sequence at second level (Item11) when 3rd level sequence (Item44,Item88) is changed after fully selecting lower level combos, alternatively 2 or 3 times to enable the button which enables on last combo selected.

here the final variable string printed by button click is missing the second sequence hence....


the order followed is

item1 -> item11 -> item 44 -> item22_var (button gets enabled)

then inbetween selected, item88 -> item22_var (button gets enabled at last combo selected)

then item44 -> item22_var (same enable)

then click on button and click on Yes gives sequence item1 -> item44 -> item2_var, it misses item11.


Please help....


import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;



public class apiIx implements SelectionListener {

private static final String CHILD = "CHILD";



Display d;



Shell s;

Hashtable<String, String[]> wholeData = new Hashtable<String, String[]>();

String[] temp = new String[] { "ITEM1" };

String items[] = { "ITEM1" };

String ITEM1[] = { "ITEM11", "ITEM22", };

//String TEMP[] = { "HI" };

String ITEM11[] = { "ITEM44", "ITEM88", };

String ITEM22[] = { "GROUP", };

String ITEM44[] = { "<ITEM22_VAR>", };

String ITEM88[] = { "<ITEM22_VAR>", };



private Composite top;

public Button btnNewButton =null;

Vector<String> vCombo = new Vector<String>(1,1);

String lineOutput = "";

public apiIx() {

d = new Display();

s = new Shell(d);

s.setSize(250, 250);

s.setText("IX PROC TOR");

ScrolledComposite sc = new ScrolledComposite(s, SWT.H_SCROLL

| SWT.V_SCROLL | SWT.BORDER);

top = new Composite(sc, SWT.None);

sc.setContent(top);



top.setBackground(d.getSystemColor(SWT.COLOR_WHITE));

s.setLayout(new FillLayout());

top.setLayout(new GridLayout());

final Combo c = new Combo(top, SWT.READ_ONLY);



wholeData.put("ROOT", temp);

wholeData.put("ITEM1", ITEM1);

//wholeData.put("TEMP", TEMP);

wholeData.put("ITEM11", ITEM11);

wholeData.put("ITEM22", ITEM22);

wholeData.put("ITEM44", ITEM44);

wholeData.put("ITEM88", ITEM88);



c.setItems(items);

c.addSelectionListener(this);


top.setSize(top.computeSize(SWT.DEFAULT, SWT.DEFAULT));

final MessageBox messageBox = new MessageBox(s, SWT.ICON_INFORMATION | SWT.YES | SWT.NO | SWT.CANCEL);

messageBox.setText("Info");
messageBox.setMessage("Save the changes for this line?");

s.setLayout(new RowLayout());

btnNewButton = new Button(s, SWT.PUSH);

btnNewButton.setEnabled(false);

btnNewButton.setText("Add to File");

btnNewButton.addListener(SWT.Selection, new Listener() {
	
	@Override
	public void handleEvent(Event e) {
		// TODO Auto-generated method stub
		if (e.widget== btnNewButton) {
			int buttonID = messageBox.open();
			switch(buttonID) {
			  case SWT.YES:
			    // saves changes ...
				  Enumeration<String> eCombo=vCombo.elements();
				  
				  while(eCombo.hasMoreElements())
				  {
					  	lineOutput = lineOutput + " " + eCombo.nextElement();
				  
				  }
				  
				  lineOutput=lineOutput + '\n';
				  lineOutput=lineOutput.trim();
				  System.out.println(lineOutput);
				  c.deselectAll();
				  removeChildren(c,vCombo);
				  btnNewButton.setEnabled(false);
				  vCombo.removeAllElements();
				  
			  case SWT.NO:
			    // exits here ...
			    break;
			  case SWT.CANCEL:
			    // does nothing ...
			}
			
			}


		}
		
	
});



s.open();

while (!s.isDisposed()) {

if (!d.readAndDispatch())

d.sleep();

}

d.dispose();

}


public static void main(String[] argv) {

new apiIx();



}



@Override

public void widgetDefaultSelected(SelectionEvent arg0) {

// TODO Auto-generated method stub


}



@Override

public void widgetSelected(SelectionEvent arg0) {


	
	
Combo parent = (Combo) arg0.widget;

Combo data = (Combo) parent.getData(CHILD);



if (data == null) {

String[] temp = wholeData.get(parent.getText());

if (temp != null) {

Combo c = new Combo(top, SWT.READ_ONLY);

c.setItems(temp);

c.addSelectionListener(this);

arg0.widget.setData(CHILD, c);


vCombo.addElement(parent.getText());



} else {

vCombo.addElement(parent.getText());
btnNewButton.setEnabled(true);

}



} else {

data.deselectAll();

removeChildren(parent,vCombo);
if(vCombo.size()>1)
{

vCombo.remove(vCombo.lastElement());

}
parent.addSelectionListener(this);

btnNewButton.setEnabled(false);
}

top.setSize(top.computeSize(SWT.DEFAULT, SWT.DEFAULT));

s.layout();

}



private void removeChildren(Combo parent, Vector<String> vFCombo) {

Combo data = (Combo) parent.getData(CHILD);



if (data != null) {

removeChildren(data, vFCombo);

if(vFCombo.size()>1)
{
vFCombo.remove(vFCombo.size()-1);
}

data.dispose();

parent.setData(CHILD, null);

}

}


}

[Updated on: Tue, 16 August 2011 18:08]

Report message to a moderator

Re: dynamic combo generation [message #716683 is a reply to message #716232] Thu, 18 August 2011 05:25 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
Always remember to remove your listeners when you are done with that control and take care you dont add the same listner multiple times...

Also why store it in a vector,why not get the data directly from the controls?


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: dynamic combo generation [message #717131 is a reply to message #716683] Fri, 19 August 2011 11:49 Go to previous messageGo to next message
Sankar Raman V is currently offline Sankar Raman VFriend
Messages: 15
Registered: July 2011
Junior Member
hi,

yes i was able to get from recursive function itslef than vector, its more small job and here added remove listeners in combo remove areas and working fine.

one question is suppose if a combo contains value as <valueofitem1>, <valueofitem2>, etc then i need to change it to editable combo and get values edited in it or generate new combo child from edited one?
Re: dynamic combo generation [message #717133 is a reply to message #717131] Fri, 19 August 2011 11:51 Go to previous messageGo to next message
Sankar Raman V is currently offline Sankar Raman VFriend
Messages: 15
Registered: July 2011
Junior Member
i mean if value of combo starts with < and ends with > i.e in tags.. i need it.... editable

[Updated on: Fri, 19 August 2011 11:52]

Report message to a moderator

Re: dynamic combo generation [message #718067 is a reply to message #717133] Tue, 23 August 2011 07:05 Go to previous message
Sankar Raman V is currently offline Sankar Raman VFriend
Messages: 15
Registered: July 2011
Junior Member
can someone help pls...
Previous Topic:swt browser - no more handles Error
Next Topic:Splash/Shell resize problems on Linux (Ubuntu 10.04)
Goto Forum:
  


Current Time: Sat Dec 20 10:27:56 GMT 2014

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

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