SWT: Wie Textfeld hinzufügen, nach Auswahl von einem bestimmten Combobox-item?

smartin123

Mitglied
Hi,

ich habe ein kleines Tool mit einem SWT-Fenster. Dort befindet sich oberhalb eine Combobox, mit 2 Items.
- "Selection A"
- "Selection B"

Wenn "Selection A" ausgewählt wird, soll unterhalb ein Textfeld erscheinen.
(die Methode createQueryInput würde das Textfeld zeichnen)
Wenn "Selection B" ausgewählt wird, soll unterhalb nichts erscheinen.

Der Combobox habe ich ein SelectionListener hinzugefügt, aber wie es dann weiter geht, bin ich überfragt.

Soll nach der Auswahl von "Selection A" das Textfeld instanziiert werden? Oder soll schon nach dem Programmstart, das Textfeld instanziiert werden und nur auf unsichtbar gesetzt werden?

Hier ist mein Quellcode:

Klasse Main:

Code:
package swt;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


public class Main {

	public Main() {
		Display display = new Display();
		Shell shell = new Shell(display);
		// ------------------------
		init(shell);
		// ------------------------
		shell.setSize(700, 400);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	public static void main(String args[]) {
		new Main();
	}

	public void init(Shell parent) {

		GridLayout gridLayout = new GridLayout();
		parent.setLayout(gridLayout);
		Gui page = new Gui();
		page.createPageControl(parent);
	}
}

Klasse Gui:

Code:
package swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class Gui {

	private transient Combo queryTypeSelectionCombo;
	private transient Text m_queryTextField;

	public Control createPageControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));
		GridLayout paneLayout = new GridLayout();
		paneLayout.marginHeight = paneLayout.marginWidth = 0;

		// create SelectionPanel
		createQueryTypeSelectionCombo(composite);
		
		//testing of the message field
//		createQueryInput(composite);

		return composite;
	}

	
	private String createQueryTypeSelectionCombo(Composite parent) {
		
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		Label label = new Label(parent, SWT.LEFT);
		label.setText("Select the desired selection :");
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.minimumWidth = 150;
		queryTypeSelectionCombo = new Combo(parent, SWT.DROP_DOWN
				| SWT.READ_ONLY);
		queryTypeSelectionCombo.add("Selection A");
		queryTypeSelectionCombo.add("Selection B");
		queryTypeSelectionCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				String comboSelectionInListener = queryTypeSelectionCombo.getText();
				
				if(comboSelectionInListener.equals("Selection A"))
				{
					//createQueryInput(parent);
					//what should I do, to call the method createQueryInput
				}
				else if(comboSelectionInListener.equals("Selection B"))
				{
					//there should be nothing, eventually disable the textfield,
					//if the user clicked firstly combobox - selection A
				}
			}
			
		
		});
		return null;
	}
	
	private String createQueryInput(Composite composite) {
		Label fieldLabel = new Label(composite, SWT.NONE);
		fieldLabel.setText("&Message:");

		m_queryTextField = new Text(composite, SWT.BORDER | SWT.V_SCROLL
				| SWT.H_SCROLL);
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.heightHint = 100;
		m_queryTextField.setLayoutData(data);

		return null;
		// TODO Auto-generated method stub

	}

}

Im Forum hab ich schon nach entsprechenden Stichwörtern gesucht, aber leider nichts passendes gefunden. Herr Google hatte auch keine Idee. :)

Würde mich freuen, wenn jemand mir weiterhelfen könnte.

Viele Dank + Grüße
smartin123
 
Du solltest das Textfeld direkt am Anfang instanziieren und auf disabled setzen. Ansonsten wird die Oberfläche sehr unruhig wenn du Elemente hinzufügst.
 

Neue Beiträge

Zurück