ERLEDIGT
JA
JA
ANTWORTEN
2
2
ZUGRIFFE
539
539
EMPFEHLEN
-
Hi, ich habe mal eine Frage:
Ich möchte immer den Text aus der letzten Zeile einer JTextArea bekommen.
Wenn ich etwas in die erste Zeile schreibe, funktioniert alles noch, doch wenn ich einen Zeilenumbruch mache, wird die ganze Zeit eine Exception geworfen.
Hier ist der Code:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; public class Konsole extends JFrame implements CaretListener { private static final long serialVersionUID = 1L; JTextArea t; public Konsole() { t = new JTextArea(); t.addCaretListener(this); getContentPane().add(t); setVisible(true); } public static void main(String[] args) { new Konsole(); } public void caretUpdate(CaretEvent ev) { try { String command = t.getText(t.getLineStartOffset(t.getLineCount()-1),t.getLineEndOffset(t.getLineCount()-1)); System.out.println(command); } catch (BadLocationException e) { e.printStackTrace(); } } }
-
14.06.07 11:42 #2
- Registriert seit
- Oct 2004
- Ort
- Hessen
- Beiträge
- 310
Hallo Schnacki,
ich hab deinen code mal etwas abgeändert. Hiermit funktioniert das was du willst.
Ich weiss allerdings nicht warum die funktion LineStartOffset und LineEndOffset so nicht funktioniert. Ich bekomme dort jeweils immer den gleichen wert zurück in der zweiten zeile.
Noch etwas: Du bekommst jetzt erst bei jedem enter-tasten druck die letzt-eingegebene zeile zurück.
Siehe dies mal als eine art work-around. Hab jetzt auch nicht bis ins Detail mich damit beschäftigt.
Gruß,
Torsten
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.*; import javax.swing.event.*; public class Konsole extends JFrame implements CaretListener, WindowListener { public void windowActivated(WindowEvent arg0) { // TODO Auto-generated method stub System.out.println("Wieder in Konsole!"); } public void windowClosed(WindowEvent arg0) { } public void windowClosing(WindowEvent arg0) { // TODO Auto-generated method stub System.out.println("Konsole geschlossen"); System.exit(0); } public void windowDeactivated(WindowEvent arg0) { // TODO Auto-generated method stub System.out.println("Konsole gewechselt"); } public void windowDeiconified(WindowEvent arg0) { // TODO Auto-generated method stub } public void windowIconified(WindowEvent arg0) { // TODO Auto-generated method stub } public void windowOpened(WindowEvent arg0) { System.out.println("Konsole geöffnet"); } private static final long serialVersionUID = 1L; JTextArea t; int lastLine=0; int lineoffset=0; public Konsole() { t = new JTextArea(30,40); t.addCaretListener(this); getContentPane().add(t); this.addWindowListener(this); pack(); setVisible(true); } public static void main(String[] args) { new Konsole(); } public void caretUpdate(CaretEvent ev) { if (lastLine!=t.getLineCount()-1) { lastLine=t.getLineCount()-1; System.out.println("Row "+lastLine+" Command: "+t.getText().substring(lineoffset)); lineoffset=t.getText().length(); } } }"There's nothing we have to fear, except Fear itself....."
-
Cool, danke.
Ich werde das mal ausprobieren.
Ähnliche Themen
-
SWTBrowser +getText
Von spammich im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 0Letzter Beitrag: 21.02.08, 17:48 -
TextArea - getText() ?
Von batok im Forum JavaAntworten: 2Letzter Beitrag: 28.04.06, 17:32 -
problem bei gettext
Von xyba im Forum PHPAntworten: 2Letzter Beitrag: 27.01.06, 01:07 -
gettext putenv()
Von südpol im Forum PHPAntworten: 2Letzter Beitrag: 24.12.04, 11:33 -
GNU gettext Problem
Von at0m1c im Forum PHPAntworten: 0Letzter Beitrag: 16.08.04, 00:15





Zitieren
Login





