JTextArea -> disable editing from caret position 0 to specific position

i have a textarea which filled with comments that uers entered.

the problem is that i do not want to allow the user to touch the previuos comments since i save each time all the text to the comments field in DB.

I currently use 2 text areas but i want to merge them into 1 text area.

[302 byte] By [sadounja] at [2007-11-27 5:37:00]
# 1
Read in the tutorial Implementing a Document Filter http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#filter
Rodney_McKaya at 2007-7-12 15:08:39 > top of Java-index,Desktop,Core GUI APIs...
# 2
Or, depening on your exact requirement, you may want to try a NavigationFilter http://forum.java.sun.com/thread.jspa?forumID=57&threadID=5132843&start=10
camickra at 2007-7-12 15:08:39 > top of Java-index,Desktop,Core GUI APIs...
# 3

> Or, depening on your exact requirement, you may want

> to try a NavigationFilter

>

> http://forum.java.sun.com/thread.jspa?forumID=57&threa

> dID=5132843&start=10

it's working ONLY FOR MOVEMENTS WITH ARROWS but when pressing back space it is going to the area which should not be altered

sadounja at 2007-7-12 15:08:39 > top of Java-index,Desktop,Core GUI APIs...
# 4
Well, you where give two suggestions so you will need to try using a Documentation Filter, or maybe a combination of both to meet your requirement.
camickra at 2007-7-12 15:08:39 > top of Java-index,Desktop,Core GUI APIs...
# 5
can you please help me in how to use the document filterthanks.
sadounja at 2007-7-12 15:08:39 > top of Java-index,Desktop,Core GUI APIs...
# 6

Actually a DocumentFilter won't work either, since no characters are being added to the Document.

So it looks like you need to create a custom backspace action. Something like this:

import java.awt.event.*;

import javax.swing.*;

import javax.swing.text.*;

public class NavigationFilterPrefixWithBackspace extends NavigationFilter

{

private int prefixLength;

private Action deletePrevious;

public NavigationFilterPrefixWithBackspace(int prefixLength, JTextComponent component)

{

this.prefixLength = prefixLength;

deletePrevious = component.getActionMap().get("delete-previous");

component.getActionMap().put("delete-previous", new BackspaceAction());

}

public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias)

{

fb.setDot(Math.max(dot, prefixLength), bias);

}

public void xxxmoveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias)

{

fb.moveDot(Math.max(dot, prefixLength), bias);

}

class BackspaceAction extends AbstractAction

{

public void actionPerformed(ActionEvent e)

{

JTextComponent component = (JTextComponent)e.getSource();

if (component.getCaretPosition() > prefixLength)

{

deletePrevious.actionPerformed( null );

}

}

}

public static void main(String args[]) throws Exception {

JTextField textField = new JTextField("Prefix_", 20);

textField.setCaretPosition(7);

textField.setNavigationFilter( new NavigationFilterPrefixWithBackspace(7, textField) );

JFrame frame = new JFrame("Navigation Filter Example");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(textField);

frame.pack();

frame.setLocationRelativeTo( null );

frame.setVisible(true);

}

}

camickra at 2007-7-12 15:08:39 > top of Java-index,Desktop,Core GUI APIs...
# 7

> Actually a DocumentFilter won't work either, since no characters are being added to the Document

Actually DocumentFilter will work, you just have to override all 3 functions of [url http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/text/DocumentFilter.html]DocumentFilter[/url]

Rodney_McKaya at 2007-7-12 15:08:39 > top of Java-index,Desktop,Core GUI APIs...
# 8
camickr thanks. it works okyou get the dukes
sadounja at 2007-7-12 15:08:39 > top of Java-index,Desktop,Core GUI APIs...
# 9

> Actually DocumentFilter will work, you just have to override all 3 functions of DocumentFilter

Thanks, I should have read the API more closely. The example from the tutorial only overrode two of the methods.

So I would say the best solution is probably a combination of the DocumentFilter to prevent the removal of the text and then maybe a NavigationFilter if you want to control the movement of the caret.

camickra at 2007-7-12 15:08:39 > top of Java-index,Desktop,Core GUI APIs...