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.

dynamischer Methodenaufruf von protected-Funktion

Empfohlene Antworten

Veröffentlicht

Hallo,

ich habe das Problem, dass ich das Event-Handling eines Panels verkürzen möchte und das ganze am Beispiel eines Textfeldes.

Bis jetzt hab ich für jedes Textfeld einen Event-Listener erstellt. Diese sind anonyme Klassen.

Bei 20-30 anonymen Klassen verlier ich aber den Überblick. Also dachte ich mir das wie folgt:


public class TestDymMet {

[INDENT]private JTextfield txtTest = new JTextField();[/INDENT]



[INDENT]TestDymMet() {

[INDENT]txtTest.addKeyListner(new Test_keyAdapter(this));[/INDENT]


}[/INDENT]


[INDENT]protected void keyAdapter(KeyEvent e) {

[INDENT]String methodName = ((JComponent) e.getSource()).getName() + "_keyReleased";

try {

[INDENT]Method method = getClass().getMethod(methodName, e.getClass());

method.invoke(this, e);[/INDENT]


} catch (Exception ex) {

[INDENT]ex.printStackTrace();[/INDENT]


}[/INDENT]

}[/INDENT]


[INDENT]protected void txtTest(KeyEvent e){

[INDENT]txtTest.setTest("test");[/INDENT]


}[/INDENT]


}



class  Test_keyAdapter extends KeyAdapter{


	TestDymMet adaptee;


	Test_keyAdapter(TestDymMet adaptee){

		this.adaptee = adaptee;

	}


	public void keyPressed(KeyEvent e) {

	    adaptee.keyAdapter(e);

	}

}

So... Problem an der Sache ist nur, in der Struktur kommt immer eine java.lang.NoSuchMethodException

Wenn die Funkion txtTest public ist, geht das

Meine Frage also:

kann ich Methoden dynamisch laden wenn diese protected oder private sind ?

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

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.