Highlight current row in JTextPane

后端 未结 3 1592
别那么骄傲
别那么骄傲 2021-02-08 21:21

I\'m trying for more than 2 days to implement a specific requirement for a text editor window... unfortunately without success so far :(

The goal is to get a text editor

相关标签:
3条回答
  • 2021-02-08 21:23

    Below is the code to extract text from current line. You can use same logic to get required indexes and highlight text

        private String getCurrentEditLine() {
            int readBackChars = 100;
            int caretPosition = scriptEditor.getCaretPosition();
    
            if (caretPosition == 0) {
                return null;
            }
    
            StyledDocument doc = scriptEditor.getStyledDocument();
    
            int offset = caretPosition <= readBackChars ? 0 : caretPosition
                    - readBackChars;
    
            String text = null;
            try {
                text = doc.getText(offset, caretPosition);
            } catch (BadLocationException e) {
            }
    
            if (text != null) {
                int idx = text.lastIndexOf("\n");
                if(idx != -1) {
                    return text.substring(idx);
                }else {
                    return text;
                }
            }
    
            return null;
        }
    
    0 讨论(0)
  • 2021-02-08 21:32

    I think this might be difficult to achieve using highlighters - I don't think it is what they were designed for. You may need to do it with custom painting code:

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    
    import javax.swing.JFrame;
    import javax.swing.JTextPane;
    import javax.swing.text.BadLocationException;
    
    public class HighlightLineTest {
        private static class HighlightLineTextPane extends JTextPane {
            public HighlightLineTextPane() {
                // Has to be marked as transparent so the background is not replaced by 
                // super.paintComponent(g);
                setOpaque(false);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                g.setColor(getBackground());
                g.fillRect(0, 0, getWidth(), getHeight());
                try {
                    Rectangle rect = modelToView(getCaretPosition());
                    if (rect != null) {
                        g.setColor(Color.CYAN);
                        g.fillRect(0, rect.y, getWidth(), rect.height);
                    }
                } catch (BadLocationException e) {
                }
                super.paintComponent(g);
            }
    
            @Override
            public void repaint(long tm, int x, int y, int width, int height) {
                // This forces repaints to repaint the entire TextPane.
                super.repaint(tm, 0, 0, getWidth(), getHeight());
            }
        }
    
        public static void main(String[] args) {
            JFrame frame = new JFrame("Highlight test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new HighlightLineTextPane());
            frame.setBounds(100, 100, 300, 400);
            frame.setVisible(true);
        }
    }
    
    0 讨论(0)
  • 2021-02-08 21:43

    http://tips4java.wordpress.com/2008/10/29/line-painter/

    I think this is what you are looking for. I took that LinePainter class and copied your constructor over into a main method, took out your highlighter parts and added a new LinePainter(textPane); Works like a charm

    0 讨论(0)
提交回复
热议问题