aspose file tools*
The moose likes Beginning Java and the fly likes Customizing a Document Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Customizing a Document" Watch "Customizing a Document" New topic
Author

Customizing a Document

Paul Gupwell
Greenhorn

Joined: Oct 26, 2001
Posts: 5
I have created a custom document that is a subclass of PlainDocument to limit the content of a JTextField. Within the custom document I have overridden the remove method in AbstractDocument. Within my remove method I want to find out which JTextField I am processing via "getDocument().getProperty("name")" however I get the following compile error:

C:\My Documents\FormattedDocument.java:62: cannot resolve symbol
symbol : variable getDocument
location: class FormattedDocument
if (getDocument.getProperty("name").equals("payment"))
^
Why is this when getDocument is a method in AbstractDocument???
AbstractDocument
PlainDocument
MyCustomDocument
I only want some fields to have all their content removed - i.e. if proposedResult.length=0 and field is 'editable' do not call super.remove, however if field is not 'editable' and is being set within the application then super.remove must be called during execution of setText method to initialise the field before the new content is inserted.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Originally posted by Paul Gupwell:
Within my remove method I want to find out which JTextField I am processing via "getDocument().getProperty("name")" however I get the following compile error:

C:\My Documents\FormattedDocument.java:62: cannot resolve symbol
symbol : variable getDocument
location: class FormattedDocument
if (getDocument.getProperty("name").equals("payment"))
^
Why is this when getDocument is a method in AbstractDocument???

Notice the parens after getDocument in bold? Notice their absence in the error message you are getting?

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Paul Gupwell
Greenhorn

Joined: Oct 26, 2001
Posts: 5
Sorry, I just missed the brackets when typing the message, they are there in the code:
C:\My Documents\FormattedDocument.java:62: cannot resolve symbol
symbol : method getDocument ()
location: class FormattedDocument
if (getDocument().getProperty("name").equals("payment"))
^
Any other ideas??? I have included the class below - ignore all the debug displays.
-----------------------------------------
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.Toolkit;
import java.text.*;
import java.util.Locale;
public class FormattedDocument extends PlainDocument { private Format format;
public FormattedDocument(Format f) {
format = f;
}
public Format getFormat() {
return format;
}
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
String currentText = getText(0, getLength());
String beforeOffset = currentText.substring(0, offs);
String afterOffset = currentText.substring(offs, currentText.length());
String proposedResult = beforeOffset + str + afterOffset;
try {
format.parseObject(proposedResult);
super.insertString(offs, str, a);
} catch (ParseException e) {
Toolkit.getDefaultToolkit().beep();
System.err.println("insertString: could not parse: "
+ proposedResult);
}
}
public void remove(int offs, int len) throws BadLocationException {
double defaultValue = 0;
String currentText = getText(0, getLength());
String beforeOffset = currentText.substring(0, offs);
System.err.println("currentText length:" + currentText.length());
System.err.println("getLength:" + getLength());
System.err.println("offss:" + offs);
System.err.println("len:" + len);
String afterOffset = currentText.substring(len + offs,
currentText.length());
String proposedResult = beforeOffset + afterOffset;
System.err.println("currentText:" + currentText);
System.err.println("beforeOffset:" + beforeOffset);
System.err.println("before length:" + beforeOffset.length());
System.err.println("afterOffset:" + afterOffset);
System.err.println("after length:" + afterOffset.length());
System.err.println("proposedResult:" + proposedResult);
System.err.println("result length:" + proposedResult.length());
System.err.println("offss:" + offs);
System.err.println("len:" + len);
try {
// Document doc = getDocument();
// only payment can be blanked out
if (getDocument().getProperty("name").equals("payment"))
format.parseObject(proposedResult);
super.remove(offs, len);

System.err.println("after super");

} catch (ParseException e) {
Toolkit.getDefaultToolkit().beep();
System.err.println("remove: could not parse: " + proposedResult);
}
}
}
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
getDocument() is amethod of JTextComponent not AbstractDocument or PlainDocument.
How about giving an explanation of what you are trying to do with the document.
[This message has been edited by Paul Stevens (edited October 29, 2001).]
Paul Gupwell
Greenhorn

Joined: Oct 26, 2001
Posts: 5
thanx
setting an additional key called 'emptyValid' and
replacing :
getDocument().getProperty("name").equals("payment")
with:
getDocumentProperties().get("emptyValid") == "no")
worked.
I wanted to change validate some input fields and as part of this prevent them being completly erased within the remove(int offs, int len) method. However because insertString(int offs, String str, AttributeSet a) calls remove(0,0) to clear current contents before inserting a new string the 'empty field is invalid' logic needed to be conditional.
 
Consider Paul's rocket mass heater.
 
subject: Customizing a Document