I'm trying to make a textpane using a StyledDocument where when doc.insertString(doc.getLength(), text + "\n", keyWord); it replaces all instances of "§" then a number (so like "§1") with the colour and should remove the code its self after and leave the text coloured. The code is able to colour and remove but the issue is that the when I remove the text from the actual doc(the textpane) the local variable I have isn't updated, and I am not sure what the best way to do that would be(the local variable is "text"). The issue with this is that it starts deleting things that aren't the code, I assume that is caused by the wrong index.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
import javax.swing.JFrame;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import me.woder.bot.Client;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
public class TorchGUI extends JPanel{
private static final long serialVersionUID = 1L;
public JFrame frame;
private JTextField textField;
Client c;
JTextPane chat;
JTextArea status;
final StyleContext cont = StyleContext.getDefaultStyleContext();
final AttributeSet black = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(0,0,0));
final AttributeSet blue = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(0,0,170));
final AttributeSet green = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(0,170,0));
final AttributeSet dark_aqua = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(0,170,170));
final AttributeSet dark_red = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(170,0,0));
final AttributeSet purple = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(170,0,170));
final AttributeSet orange = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(255,170,0));
final AttributeSet grey = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(170,170,170));
final AttributeSet dark_grey = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(85,85,85));
final AttributeSet indigo = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(85,85,255));
final AttributeSet bright_green = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(85,255,85));
final AttributeSet aqua = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(85,255,255));
final AttributeSet red = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(255,85,85));
final AttributeSet pink = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(255,85,255));
final AttributeSet yellow = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(255,255,85));
final AttributeSet white = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, new Color(255,255,255));
final AttributeSet reset = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.black);
final AttributeSet attrBlack = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLACK);
DefaultStyledDocument doc = new DefaultStyledDocument() {
private static final long serialVersionUID = 1L;
};
/** launch it up
*
*/public static void main(String[] args){
TorchGUI window;
window = new TorchGUI();
window.frame.setVisible(true);
window.addText("§0this should be black §1this should be blue");
}
/**
* Create the application.
*/
public TorchGUI(/*Client c*/) {
//this.c = c;
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("TorchBot 2.1");
frame.setBounds(100, 100, 944, 555);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 520, 454);
frame.getContentPane().add(scrollPane);
chat = new JTextPane(doc);
scrollPane.setViewportView(chat);
chat.setEditable(false);
textField = new JTextField();
textField.setBounds(10, 476, 447, 33);
frame.getContentPane().add(textField);
textField.setColumns(10);
status = new JTextArea();
status.setBounds(540, 250, 262, 215);
frame.getContentPane().add(status);
status.setEditable(false);
JTextArea textArea_2 = new JTextArea();
textArea_2.setBounds(540, 12, 262, 228);
frame.getContentPane().add(textArea_2);
}
public void addText(String text){
SimpleAttributeSet keyWord = new SimpleAttributeSet();
try {
int len = doc.getLength();
doc.insertString(len, text + "\n", keyWord);
formatColour(text, len);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
public void formatColour(String text, int offset){
System.out.println("Text: " + text + " length + " + text.length() + " offset is: " + offset);
int before = offset;
if (before < 0) before = 0;
int after = text.length();
int wordL = before;
int wordR = before;
while (wordR < after) {
/*boolean is = true;
if (wordR == after || is) {*/
try{
System.out.println("Now looking at:" + text.substring(wordL, wordR) + " wordR is: " + wordR + " and offset is: " + offset + " worldL is: " + wordL);
if(text.substring(wordL, wordR).matches("§") && text.length() >= (wordR+1-offset)){
if (text.substring(wordL, wordR+1).contains("0")){
doc.setCharacterAttributes(wordL, text.length(), black, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("1")){
doc.setCharacterAttributes(wordL, text.length(), blue, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("2")){
doc.setCharacterAttributes(wordL, text.length(), green, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("3")){
doc.setCharacterAttributes(wordL, text.length(), dark_aqua, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("4")){
doc.setCharacterAttributes(wordL, text.length(), dark_red, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("5")){
doc.setCharacterAttributes(wordL, text.length(), purple, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("6")){
doc.setCharacterAttributes(wordL, text.length(), orange, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("7")){
doc.setCharacterAttributes(wordL, text.length(), grey, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("8")){
doc.setCharacterAttributes(wordL, text.length(), dark_grey, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("9")){
doc.setCharacterAttributes(wordL, text.length(), indigo, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("a")){
doc.setCharacterAttributes(wordL, text.length(), bright_green, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("b")){
doc.setCharacterAttributes(wordL, text.length(), aqua, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("c")){
doc.setCharacterAttributes(wordL, text.length(), red, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("d")){
doc.setCharacterAttributes(wordL, text.length(), pink, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("e")){
doc.setCharacterAttributes(wordL, text.length(), yellow, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("f")){
doc.setCharacterAttributes(wordL, text.length(), white, false);
doc.remove(wordL, 2);
}else if(text.substring(wordL, wordR+1).contains("r")){
doc.setCharacterAttributes(wordL, text.length(), reset, false);
doc.remove(wordL, 2);
}else{
doc.setCharacterAttributes(wordL, wordR - wordL, attrBlack, false);
doc.remove(wordL, 1);
}
}
wordL = wordR;
wordR++;
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Thanks for the help.