Results 1 to 7 of 7
  1. #1
    Maladict is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default How to select/highlight an entire row in JTextPane

    I stumbled upon a question here when googling for a solution for my problem, it's not quite my problem, but rather close. Originally tried to post the url, but I can't post the link due to low post count. It's titled "How to select/highlight an entire row in JTextArea".

    So, I figured I'd try my luck and see if anyone could help me with my specific problem here:

    I've got a JTextPane full of lines of text. When I click on a particular point in the JTextPane and if there is a line of text across that point, I want that line of text is selected and high-lighted. I'd also like it to search for that particular String of text in an adjacent JTextPane when selecting it. How to do this? I'd prefer avoiding "extends" and "implements" if possible, seeing how the class is already quite full of stuff and either of those could mess things up quite a bit.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Do you want to select text one a single click. Double click on the text line select the entire line of text. Then you can working with getSelectedText() method.

  3. #3
    Maladict is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks, but, the requested functionality for the program (I'm only coding it, not making the demands on how it should work :p) is that one click would highlight the entire line in textpane 1 (a filtered down version of the text) and search for it in textpane 2 (an unfiltered version of the text), for easy comparison between what's been filtered away and what hasn't.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    hmm, interesting. Can't we implement an action listener on a line. May be a moved hover listener or something. Did you try something like that?

  5. #5
    Maladict is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks, but I'm afraid I'm a bit new to Swing (and especially to jtextpane :p), could you give me an example on how to do something like that? Or better yet, explain how to adapt this (java-forums.org/awt-swing/476-how-select-highlight-entire-row-jtextarea.html) solution to a jtextpane without the extends and implements, if possible?

    Sorry about bypassing the link posting rules, but that's such an excellent example of what I'd like to do, which happens to be in this very forum, so I figured the link would provide more benefit than harm for this case.
    Last edited by Maladict; 08-14-2008 at 09:39 AM.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class MatchUp {
        String text = "This component models paragraphs that are " +
            "composed of runs of character level attributes. Each " +
            "paragraph may have a logical style attached to it " +
            "which contains the default attributes to use if not " +
            "overridden by attributes set on the paragraph or " +
            "character run. Components and images may be embedded " +
            "in the flow of text.";
        JTextArea right;
        Action selectLine;
        Rectangle[] rects = new Rectangle[2];
    
        private JPanel getContent() {
            rects[0] = new Rectangle();
            rects[1] = new Rectangle();
            JPanel panel = new JPanel(new GridLayout(1,0,10,0));
            panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
            panel.add(new JScrollPane(getTextPane()));
            panel.add(getTextAreaComponent());
            return panel;
        }
    
        private JTextPane getTextPane() {
            JTextPane textPane = new JTextPane();
            selectLine = getAction(textPane, DefaultEditorKit.selectLineAction);
            StyledDocument doc = textPane.getStyledDocument();
            try {
                doc.insertString(0, text, null);  // uses default style
            } catch(BadLocationException e) {
                System.out.println("bad location: " + e.getMessage());
            }
            textPane.addMouseListener(selector);
            // Style the text:
            // Logical style.
            Style s = doc.addStyle("logical", null);
            StyleConstants.setFontFamily(s, "georgia");
            StyleConstants.setFontSize(s, 18);
            // Paragraph attribute.
            s = doc.addStyle("lineSpace", null);
            StyleConstants.setLineSpacing(s, 0.25f);
            doc.setParagraphAttributes(0, doc.getLength(), s, false);
            doc.setLogicalStyle(0, doc.getStyle("logical"));
            return textPane;
        }
    
        private JScrollPane getTextAreaComponent() {
            right = new JTextArea(text);
            // Let parent viewport draw this component.
            right.setOpaque(false);
            right.setLineWrap(true);
            right.setWrapStyleWord(true);
            right.setMargin(new Insets(2,5,2,5));
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setViewport(viewport);
            viewport.setView(right);
            viewport.setBackground(UIManager.getColor("TextArea.background"));
            return scrollPane;
        }
    
        private Action getAction(JTextPane textPane, String name) {
            Action[] actions = textPane.getActions();
            for (int i = 0; i < actions.length; i++) {
                if (name.equals(actions[i].getValue(Action.NAME).toString())) {
                    return actions[i];
                }
            }
            return null;
        }
    
        private JViewport viewport = new JViewport()  {
            Color color = UIManager.getColor("TextArea.selectionBackground");
    
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D)g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                    RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setPaint(color);
                for(int j = 0; j < rects.length; j++) {
                    g2.fill(rects[j]);
                }
            }
        };
    
        public static void main(String[] args) {
            MatchUp test = new MatchUp();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getContent());
            f.setSize(600,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    
        private MouseListener selector = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                selectLine.actionPerformed(null);
                JTextPane textPane = (JTextPane)e.getComponent();
                int start = textPane.getSelectionStart();
                int end = textPane.getSelectionEnd();
                System.out.printf("start  = %3d  end = %3d%n", start, end);
                right.setCaretPosition(start);
                right.moveCaretPosition(end);
    
                Rectangle startR = null, endR = null;
                try {
                    startR = right.modelToView(start);
                    System.out.printf("startR = [%d, %d, %d, %d]%n",
                                       startR.x, startR.y,
                                       startR.width, startR.height);
                    endR = right.modelToView(end);
                    System.out.printf("  endR = [%d, %d, %d, %d]%n",
                                       endR.x, endR.y, endR.width, endR.height);
                } catch(BadLocationException ble) {
                    System.out.println("bad location: " + ble.getMessage());
                }
                if(startR.y == endR.y) {    // single line
                    int w = endR.x - startR.x;
                    rects[0].setFrame(startR.x, startR.y, w, startR.height);
                    rects[1].setFrame(0,0,0,0);
                } else {                    // multi-line
                    int x = right.getMargin().left;
                    Rectangle r = null;
                    try {
                        int pos = right.viewToModel(new Point(x, endR.y));
                        r = right.modelToView(pos-1);
                        System.out.printf("     r = [%d, %d, %d, %d]%n",
                                           r.x, r.y, r.width, r.height);
                    } catch(BadLocationException ble) {
                        System.out.println("bad location: " + ble.getMessage());
                    }
                    int w = r.x - startR.x;
                    rects[0].setFrame(startR.x, startR.y, w, startR.height);
                    w = endR.x - x;
                    rects[1].setFrame(x, endR.y, w, endR.height);
                }
                right.repaint();
            }
        };
    }

  7. #7
    Maladict is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks, tried it out quickly and it seemed to be exactly what I was looking for; I'll take a closer look on Monday and might have some followup questions :)

Similar Threads

  1. How to highlight text by drag and selection
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-25-2008, 10:35 AM
  2. Replies: 0
    Last Post: 04-09-2008, 06:39 PM
  3. Adding custom highlight to JEditorPane
    By andrewb in forum AWT / Swing
    Replies: 0
    Last Post: 06-22-2007, 06:48 PM
  4. GNU Source-highlight 2.7
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-12-2007, 08:39 AM
  5. How to select/highlight an entire row in JTextArea
    By Valeriano in forum AWT / Swing
    Replies: 2
    Last Post: 05-28-2007, 11:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •