Reputation: 629
I implemented this code, whose aim is to have a JtextArea that looks like an editor, in which some keywords change color when typed or insert. This is the code I wrote:
private void openBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt files", "txt", "text");
chooser.setFileFilter(filter);
int option = chooser.showOpenDialog(null);
if (option == JFileChooser.APPROVE_OPTION) {
f = chooser.getSelectedFile();
filename = f.getAbsolutePath();
String modelSpec = readSpecification();
spec = modelSpec;
final StyleContext cont = StyleContext.getDefaultStyleContext();
final javax.swing.text.AttributeSet attrGray = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.gray);
final javax.swing.text.AttributeSet attrGreen = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.GREEN);
final javax.swing.text.AttributeSet attrBlue = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLUE);
final javax.swing.text.AttributeSet attrRed = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED);
final javax.swing.text.AttributeSet attrBlack = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLACK);
DefaultStyledDocument doc = new DefaultStyledDocument() {
@Override
public void insertString (int offset, String str, javax.swing.text.AttributeSet a) throws BadLocationException {
super.insertString(offset, str, a);
//String text = getText(0, getLength()-1);
int before = findLastNonWordChar(spec, offset);
if (before < 0) before = 0;
int after = findFirstNonWordChar(spec, offset + str.length());
int prevWord = before;
int postWord = before;
while (postWord <= after) {
if (postWord == after || String.valueOf(spec.charAt(postWord)).matches("\\W")) {
if (spec.substring(prevWord, postWord).matches("(\\W)*(System)"))
setCharacterAttributes(prevWord, postWord - prevWord, attrBlue, false);
else if (spec.substring(prevWord, postWord).matches("(\\W)*(PlasmaMembrane)"))
setCharacterAttributes(prevWord, postWord - prevWord, attrBlue, false);
else if (spec.substring(prevWord, postWord).matches("(\\W)*(Cytosol)"))
setCharacterAttributes(prevWord, postWord - prevWord, attrBlue, false);
else if (spec.substring(prevWord, postWord).matches("(\\W)*(Organelle)"))
setCharacterAttributes(prevWord, postWord - prevWord, attrBlue, false);
else if (spec.substring(prevWord, postWord).matches("(\\W)*(Nucleous)"))
setCharacterAttributes(prevWord, postWord - prevWord, attrBlue, false);
else if (spec.substring(prevWord, postWord).matches("(\\W)*(volume)"))
setCharacterAttributes(prevWord, postWord - prevWord, attrGray, false);
else if (spec.substring(prevWord, postWord).matches("(\\W)*(rate)"))
setCharacterAttributes(prevWord, postWord - prevWord, attrRed, false);
else
setCharacterAttributes(prevWord, postWord - prevWord, attrBlack, false);
prevWord = postWord;
}
postWord++;
}
}
@Override
public void remove (int offs, int len) throws BadLocationException {
super.remove(offs, len);
String text = getText(0, getLength());
int before = findLastNonWordChar(text, offs);
if (before < 0) before = 0;
int after = findFirstNonWordChar(text, offs);
if (text.substring(before, after).matches("(\\W)*(System)"))
setCharacterAttributes(before, after - before, attrBlue, false);
else if (text.substring(before, after).matches("(\\W)*(PlasmaMembrane)"))
setCharacterAttributes(before, after - before, attrBlue, false);
else if (text.substring(before, after).matches("(\\W)*(Cytosol)"))
setCharacterAttributes(before, after - before, attrBlue, false);
else if (text.substring(before, after).matches("(\\W)*(Organelle)"))
setCharacterAttributes(before, after - before, attrBlue, false);
else if (text.substring(before, after).matches("(\\W)*(Nucleous)"))
setCharacterAttributes(before, after - before, attrBlue, false);
else if (text.substring(before, after).matches("(\\W)*(volume)"))
setCharacterAttributes(before, after - before, attrGray, false);
else if (text.substring(before, after).matches("(\\W)*(rate)"))
setCharacterAttributes(before, after - before, attrRed, false);
else {
setCharacterAttributes(before, after - before, attrBlack, false);
}
}
};
textModel.setText(spec);
}
}
There are no errors or Excpetions caught, but none of those words change the foreground color. Why this code doesn't do anything? Thanks for helping
Upvotes: 0
Views: 134
Reputation: 324108
Why this code doesn't do anything?
A JTextArea doesn't support attributes. All the text must be a single color. The attributes are ignored by the PlainDocument.
You need to use a JTextPane which uses a StyledDocument which does support the attributes.
See the section from the Swing tutorial on Text Component Features for a working example of a text pane that support different attributes.
Upvotes: 2