ComboBox mittels neuer Methode befüllen

Jochen012

Mitglied
Hallo, ich bin seit Stunden/Tagen an diesem einen Problem dran, komme aber leider nicht weiter.
Ich versuche eine ComboBox nachträglich zu befüllen. Google hat mir verraten, dass ich dazu die alte ComoBox einfach mit einer Neuen überschreibe muss. Klappt auch alles soweit, wenn sich alles in der selben Methode befindet. Exportiere ich nun den Befehl zum überschreiben in eine eigene Methode und rufe diese auf, funktioniert es nicht mehr und ich finde keine Lösung dafür. Wäre super wenn mir jemand helfen könnte!

Java:
.....
JComboBox comboBox = new JComboBox();
comboBox.setBounds(10, 5, 120, 20);
comboBox.setAlignmentX(3.0f);
panel_2.add(comboBox);

 String[] inEinheit2 = new String[2];
 inEinheit2[0]= new String("meter");
 inEinheit2[1]= new String("Liter");
 comboBoxfill(inEinheit2);
		
	}
public static void comboBoxfill (String[] s) {
comboBox.setModel(new javax.swing.DefaultComboBoxModel (s));	
}



Fehler:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static field comboBox

at GUI.comboBoxfil(GUI.java:146)
at GUI.<init>(GUI.java:135)
at GUI$1.run(GUI.java:45)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
 
Du musst im Grunde einfach das Model neu setzen 😉

Du versuchst hier aus einer Statischen Methode auf eine nicht Statische Variable , sprich auf ein Objekt zuzugreifen , das lässt der compiler nicht zu.

Java:
Cannot make a static reference to the non-static field comboBox

Wenn du eine Statische Hilfsmethode haben möchtest, dann erstelle dir solch eine Methode:

Hier mit Spezifiziertem Typ :
Java:
public static DefaultComboBoxModel<String> getModel(String...input){
		DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
		for (String string : input) {
			model.addElement(string);
		}
		return model;
	}
 
Zuletzt bearbeitet:
Java:
    .....
    JComboBox comboBox = new JComboBox();
}

public static void comboBoxfill (String[] s) {
    comboBox.setModel(new javax.swing.DefaultComboBoxModel (s));	
}

du versichst in einer statischen Methode eine scheinbar nicht statische Variable zu verwenden!?
 
Zuletzt bearbeitet:

Zurück
Oben