Textfeld mit inhalt aus Combobox füllen

Henig

Mitglied
Wunderschönen guten Tag.
Ich bin ein kleiner Java neueinsteiger und Benutzte Eclipse 3.1 mit Jigloo...

Ich habe mir ne kleine GUI zusammengebaut mit einer Combobox und einem Texfeld...
Jetzt will ich, dass der Aktuell ausgewählte Wert der Combobox in das Texfeld geschrieben wird... Habe schon ansätze gefunden aber das Funktioniert so wie ich glaube es funktionieren müsste leider nicht :confused:.

So sieht es bei mir aus:
Code:
			{
				ComboBoxModel LMarkeModell = new DefaultComboBoxModel(
						new String[] { "Alle", "Dubbel", "Schild", "Vollgas", "Billig" });
				LMarke = new JComboBox();
				LMarke.setModel(LMarkeModell);
			}


			{
				blubb = new JTextField();
				blubb.setEditable(false);
				String neu = (String) LMarke.getSelectedItem();
				blubb.setText(neu);
			}

Die haben natürlich auch ne Größe undso aber ich glaub nicht, dass die wirklich relevant sind... also das der Fehler irgentwo in diesem Bereich liegen muss...
Gegoogelt hab ich auhc schon aber bin leider zu keinem mir helfendem Ergebnis gekommen...

Links und Hilfestellung sind willkommen ;)
Vielen Dank schonmal im Voraus
 
Du musst deine JComboBox einem ActionListener zuordnen.
Der ActionListener löst aus, wenn ein Element ausgewählt wird.
siehe http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html#listeners

In der Methode des ActionListeners holst du dir nun das aktuell ausgewählte Element und schreibst es in das Textfeld.
siehe http://java.sun.com/javase/7/docs/api/javax/swing/JComboBox.html#getSelectedItem() & http://java.sun.com/j2se/1.4.2/docs...JTextComponent.html#setText(java.lang.String)
 
Zuletzt bearbeitet:
Hahaa perfekt danke habs hin bekommen =)
Und sogar so hin bekommen, dass wenn ich mehrere JComboBoxen habe einzelne ausgeblendet kriege, wenn ich einen Bestimmten Wert in der ersten angebe...
Danke vielmals!!

:)
 
Zuletzt bearbeitet:
Hallo Henig,

ich habe gerade zu fällig genau das gleiche Problem...Könntest du dein aktualisiertes Skript nachdem es ja nun bei dir klappt nochmal reinstellen ...bin nich so java fit
Danke
 
Ich hab das ganze mit Jigloo gemacht und gebe dir einfach mal den kompletten Code für ein Fenster mit Combobox und Textfeld wenn du das dann so Copy-Paste mäßig machst und dir das genauer anguckst, dann solltest du eigendlich dahinter kommen wie das Funktioniert.
Unten ist der Actionlistener, der bei einer "Aktion" in der Combobox aufgerufen wird... Der ist eine Funktion sowas solltest du vom Prizip her ja auch als Neuling kennen...
also sobald du da in der Box was machst wird diese Funktion aufgerufen und setzt mit

Code:
jTextField1.setText((String) jComboBox1.getSelectedItem());
den Inhalt für JTexfield1 auf den Inhalt der JCombobox als String.

hier der KOMPLETTE Code für das Programm:
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewJFrame extends javax.swing.JFrame {
	private JTextField jTextField1;
	private JComboBox jComboBox1;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				NewJFrame inst = new NewJFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public NewJFrame() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				jTextField1 = new JTextField();
			}
			{
				ComboBoxModel jComboBox1Model = 
					new DefaultComboBoxModel(
							new String[] { "Item One", "Item Two" });
				jComboBox1 = new JComboBox();
				jComboBox1.setModel(jComboBox1Model);
				jComboBox1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jComboBox1ActionPerformed(evt);
					}
				});
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(26, Short.MAX_VALUE)
				.addComponent(jComboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(39, 39));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(118, 118)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(jComboBox1, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, Short.MAX_VALUE))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE)))
				.addContainerGap(125, 125));
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void jComboBox1ActionPerformed(ActionEvent evt) {
		
		jTextField1.setText((String) jComboBox1.getSelectedItem());
		
		
	}

}


Hoffe das hilft dir weiter =)
 
Zurück