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.

TextArea auf Knopfdruck leeren

Empfohlene Antworten

Veröffentlicht

Hi!

Bin gerade mal wieder dabei mich in Java einzuarbeiten...

Im Moment: AWT und Events.

Wie kann ich über einen Button ein TextArea leeren?

MfG

backdraft

Hallo,


javax.swing.JTextArea area = new JTextArea();
javax.swing.JButton button = new JButton();

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
area.setText("");
}
});[/PHP]

Gruß Jaraz

Dann lag ich ja doch gar nicht so falsch... :D

Hab das so:

		btn_leeren.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txta_Protokoll.setText("");
}
});[/PHP]

Da kommt aber eine Fehlermeldung:

E:\Java\jcreator\Server\Server.java:48: local variable txta_Protokoll is accessed from within inner class; needs to be declared final´

txta_Protokoll.setText("");

MfG

backdraft

Poste mal die Klasse des objektes "txta_Protokoll"

Sorry,

du musst JTextArea als Klassenvariable definieren.

Bei meinem Beispiel sieht es so aus als wenn JTextArea direkt vor der inner Class initialisiert wird.

Gruß Jaraz

	public Server() {


Panel Panel1 = new Panel();
Panel1.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
Label lbl_Protokoll = new Label("Protokoll:");
lbl_Protokoll.setBackground(SystemColor.menu);
Panel1.add(lbl_Protokoll);

Panel Panel2 = new Panel();
Panel2.setLayout(new GridLayout(1, 2));
Button btn_leeren = new Button("Protokoll leeren");
Button btn_beenden = new Button("Server beenden");
Panel2.add(btn_leeren);
Panel2.add(btn_beenden);

TextArea txta_Protokoll = new TextArea("", 10, 40, TextArea.SCROLLBARS_VERTICAL_ONLY);

setLayout(new BorderLayout());

add("North", Panel1);
add("Center", txta_Protokoll);
add("South", Panel2);


ActionListener al_Button_beenden = new ActionListener() {
public void actionPerformed( ActionEvent e ) {
System.exit(0);
}
};

btn_beenden.addActionListener(al_Button_beenden);



btn_leeren.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txta_Protokoll.setText("");
}
});


addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}[/PHP]

txta_Protokoll ist das TextArea.

MfG

backdraft

Hat keiner ne Idee, was mir die Fehlermeldung

sagen will?

:confused:

Original geschrieben von backdraft

Hat keiner ne Idee, was mir die Fehlermeldung

sagen will?

:confused:

Wenn du mal das ausführen würdest, was ich dir geraten habe...

-----------------------------

public TextArea txta_Protokoll;

public Server() {

.

.

.

  txta_Protokoll = new TextArea(....

Gruß Jaraz

Oh... hab ich volkommen überlesen.... :rolleyes:

Danke!

MfG

backdraft

Ich habe mal wieder ein Problem:

E:\Java\jcreator\Server\Server.java:70: non-static variable txta_Protokoll cannot be referenced from a static context

txta_Protokoll.setText(txta_Protokoll.getText() + "\n Warte auf Client-Anfrage...");

:confused: :confused: :confused:

MfG

backdraft

Definiere mal die Variable "static"! Also "static TextArea txta_Protokoll = new TextArea("", 10, 40, TextArea.SCROLLBARS_VERTICAL_ONLY);".

Wenn du das nicht willst, mußt du deine Variable "txta_Protokoll" in deiner static-Methode definieren in der du sie benötigst.

cu Peeter

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.