2

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.

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
user2102972
  • 251
  • 2
  • 6
  • 13

2 Answers2

3

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

rebeliagamer
  • 1,257
  • 17
  • 33
  • Your soulution doesn't work + what do you mean by exlude "\"? – user2102972 Mar 02 '13 at 18:44
  • Well, it does. For code = "test1 \"test2\" test3 test4 \"test5\"" it prints test2test5 without quotation marks. – rebeliagamer Mar 02 '13 at 18:50
  • But my problem isn't about printing right but about coloring pane's letters well. Letters after quotation mark are still colored what is wrong behaviour. And also I want qutation marks to be colored too. If you want I can give you whole code to see the problem – user2102972 Mar 02 '13 at 18:53
  • 3
    I this that yes +1, but answers could be about DocumentListener, in this case job for DocumentFilter, never to use KeyListener for JTextComponent – mKorbel Mar 02 '13 at 19:21
  • 1
    For an example of using a `DocumentFilter` to perform text highlighting, check out [this](http://stackoverflow.com/questions/14727548/java-change-the-document-in-documentlistener/14727657#14727657) – MadProgrammer Mar 02 '13 at 21:01
0

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

joey rohan
  • 3,505
  • 5
  • 33
  • 70
  • It's not. Lets say I write into JPane: | some text "string" more text |. "some text" will be colored black (default). then " occurs so string is true. from s to g ("string") we color gray. Then " occurs again so string is FALSE. which means "more text" should be black-coloured while it's gray – user2102972 Mar 02 '13 at 18:39