Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hallo,

und zwar möchte ich eine unbekannte Anzahl von Buttons mit samt der Action in einer Schleife erstellen, jedoch gibt es nun folgendes Problem:

Die Buttons erstellt er mir wunderbar, hochzählend so viele wie "icons" enthält, jedoch kennt er in der ActionListener Methode, welche er für jeden Button erstellen soll, den Zähler nicht mehr, dort sagt er mir immer dass es der maximal wert sei.

Hier der entscheidende Code:


for(i=0; i<icons.length; i++){

			sel_icon[i] = new JButton(icons[i]);

			iconList.add(sel_icon[i]);


			System.out.println(i);


			sel_icon[i].addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent evt){

					System.out.println(i);


				}

			});

		}

Hoffe mir kann jemand dabei helfen..

Gruß

Das liegt wahrscheinlich einfach nur daran, dass durch deine innere Klasse, dort ein Verweis auf deine Variable i gespeichert wird. Und da diese Variable in deiner Schleife ja bis zum Maximalwert hochgezählt wird, zeigen die Verweise dann am Ende halt alle auf diesen Wert.

ahso okay... d.h.?

wie muss ich das dann ändern?

Wenn du im nachhinein die Action noch benutzen willst usw. bietet es sich an eine eigene Action oder Actionlistener zu erstellen und dann an jeden Button anzuhängen. addActionListener(myListener)

Im Listener selbst kannst du dann für unterschiedliche Bearbeitung den ActionEvent nach dem Aufrufer fragen. if( e.getSource() == icon[0] )

Derzeit erstellst du für jeden Button eine neue Action.

Also erst mal danke für die Hilfe habe es nun hinbekommen =)

Die innere Methode, die Action für den Button, wollte unbedingt einen Zähler der mit "final" definiert wird... habe nun innerhalb der Schleife den Wert "i" einem final counter zugewiesen und diesen dann in der Methode benutzt... nun geht es =)


for(i=0; i<icons.length; i++){

			final int counter = i;


			sel_icon[i] = new JButton(icons[i]);

			action[i] = new ActionListener() {

			      public void actionPerformed(ActionEvent actionEvent) {

			        try{

			        	inputText.append("*"+icons[counter].toString().replaceAll(icon_path, "").replaceAll(".gif", "")+"*");

			        	iconList.setVisible( false );

			        }catch(NullPointerException npe){		        	

			        }

			      }

			};


			sel_icon[i].addActionListener(action[i]);

			iconList.add(sel_icon[i]);

		}

Danke.

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.