Home » Eclipse Projects » Standard Widget Toolkit (SWT) » dynamic combo generation(swt multiple combo box generation)
|
Re: dynamic combo generation [message #713899 is a reply to message #713282] |
Tue, 09 August 2011 10:37 |
Vijay Raj 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 |
Sankar Raman V 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 #714348 is a reply to message #714295] |
Wed, 10 August 2011 11:46 |
Vijay Raj 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
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
|
|
| |
Re: dynamic combo generation [message #715029 is a reply to message #714486] |
Fri, 12 August 2011 07:21 |
Sankar Raman V 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 |
Vijay Raj 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 #716232 is a reply to message #716003] |
Tue, 16 August 2011 18:06 |
Sankar Raman V 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 |
Vijay Raj 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
|
|
| | | |
Goto Forum:
Current Time: Thu Sep 26 17:29:12 GMT 2024
Powered by FUDForum. Page generated in 0.06772 seconds
|