es soll in der zweiten zeile ein string eingefügt werden, wenn man einen button drückt.
ich weiss weder, wie ich mit dem getLineStartOffset arbeiten muss, noch wie ich die BadLocationException unterbringen soll, denn der meldet dann, dass das actionPerformed sich nicht mit dieser Exception versteht.
hier der ActionListener actionL_Input, es ist zwar völlig falsch so, aber damit ihr evtl versteht wie es werden soll, hab ichs mal gepostet...
Code:
actionL_Input = new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
textArea.getLineStartOffset(2).setText("bla");
}
};
actionL_Input = new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try {
int posLine1 = textArea.getLineStartOffset(1);
int posLine2 = textArea.getLineStartOffset(2);
int posLine3 = textArea.getLineStartOffset(3);
int posLine4 = textArea.getLineStartOffset(4);
textArea.insert("bla1", posLine1);
textArea.insert("bla2", posLine2);
textArea.insert("bla3", posLine3);
textArea.insert("bla4", posLine4);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
};
wenn ich den zugehörigen button drücke, gibt es folgende exception:
javax.swing.text.BadLocationException: No such line
at javax.swing.JTextArea.getLineStartOffset(Unknown Source)
at gui.Frame$8.actionPerformed(Frame.java:375)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(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)
Die Meldung "No such line" bedeutet, daß es die Zeile, von der du die Position haben willst, noch nicht gibt. In dem Fall kannst du mit "append" neue Zeilen zur JTextArea hinzufügen. Am Zeilenende darf man dabei jeweils den "line-separator" nicht vergessen. Man kann natürlich auch leere Zeilen erzeugen, die dann nur aus dem "line-separator" bestehen.