user2102972
user2102972

Reputation: 261

JTextPane color everything instead of few chars

So Im trying to write simple editor. I want to color gray all chars witch are between " characters. Fragment which does it is:

class MainPanel extends JPanel {

private int WIDTH = 800;
private int HEIGHT = 500;
private JTextPane codePane = new JTextPane(); //Pole, w które wpisywany jest kod
private JLabel codeLabel = new JLabel("JNotepad");
private StyledDocument doc = codePane.getStyledDocument();
private final String[] keywords; //Słowa kluczowe
private final Map<String, String> shortcuts = new HashMap<>(); //syso -> System.out.println() itp.

MainPanel() {
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    setLayout(new BorderLayout());
    //Dodanie głównego pola w polu przewijanym
    JScrollPane scroll = new JScrollPane(codePane);
    add(scroll, BorderLayout.CENTER);
    add(codeLabel, BorderLayout.SOUTH);
    codePane.addKeyListener(new KeyHandler());
    codePane.setFont(new Font("Monospaced", Font.PLAIN, 15));
    //Załadowanie słów kluczowych
    Scanner in = new Scanner(getClass().getResourceAsStream("res/keywords.txt"));
    List<String> words = new LinkedList<>();
    while (in.hasNext()) {
        words.add(in.nextLine());
    }
    keywords = words.toArray(new String[words.size()]);
    in.close();
}


private class KeyHandler extends KeyAdapter {

    @Override
    public void keyReleased(KeyEvent ev) {
        highlight();
    }


    private void highlight() {
        String code = codePane.getText();
        //Zmiana koloru słów kluczowych
        String[] words = code.replaceAll("\\(|\\)|\\{|\\}|\\[|\\]", " ").split("\\s");
        int lastIndex = 0;
        for (int a = 0; a < words.length; a++) {
            SimpleAttributeSet set = new SimpleAttributeSet();
            if (Arrays.asList(keywords).contains(words[a])) {
                StyleConstants.setForeground(set, Color.BLUE);
            }
            doc.setCharacterAttributes(lastIndex, lastIndex + words[a].length(), set, true);
            //Zwiekszenie ostatniego indexu
            lastIndex += words[a].length() + 1; //+1 bo jeszcze spacja
        }
    }
}

}

When " occurs it paints characters gray, but it paints all characters after first " sign. What's wrong with this code? EDIT: Here you are, this is the full code.

Upvotes: 2

Views: 188

Answers (2)

rebeliagamer
rebeliagamer

Reputation: 1278

Second argument in setCharacterAttributes() method is a length, not end index. It was your problem.

        boolean isString = false;
        char[] text = code.toCharArray();
        for (int i = 0; i < text.length; i++) {
            if (text[i] == '\"') {
                isString = !isString;
                if(!isString) {
                    document.setCharacterAttributes(i, 1, attributes, true);
                }
            }
            if (isString) {
                document.setCharacterAttributes(i, 1, attributes, true);
            }
        }

Original question was shorter with only with few lines of code but mKorbel has right:

... in this case (it's a) job for DocumentFilter, never to use KeyListener for JTextComponent.

You should check that, it could help: How to Write a Document Listener

Upvotes: 3

joey rohan
joey rohan

Reputation: 3566

Because the condition is true.if (string) will be true.String will be given true once it equals("\"")) .So it carry on's for the next string.

In if (string){.....} block, in the end make string=false

Upvotes: 0

Related Questions