Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5981307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:55:33+00:00 2026-05-22T21:55:33+00:00

I have some text component (particularly it JEditorPane ), and need as response to

  • 0

I have some text component (particularly it JEditorPane), and need as response to certain event to make some line in the text component visible – i.e. scroll to it if that necessary. How to do this with Swing?

I find setCaretPosition but it not always good. If caret was already at position set for it new, it not make it visible again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:55:34+00:00Added an answer on May 22, 2026 at 9:55 pm

    from tutorials How to Use Editor Panes and Text Panes and How to Use Scroll Panes you can get JViewPort that’s determine visible Rectangle

    example:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class IsRectVisible {
    
        private static void createAndShowUI() {
            JFrame frame = new JFrame("IsRectVisible");
            frame.getContentPane().add(new IsRectVisibleGui());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    createAndShowUI();
                }
            });
        }
    
        private IsRectVisible() {
        }
    }
    
    class IsRectVisibleGui extends JPanel {
    
        public static final Rectangle RECT = new Rectangle(450, 400, 100, 100);
        public static final Dimension INNER_PANEL_SIZE = new Dimension(600, 800);
        private static final Dimension SCROLLPANE_SIZE = new Dimension(250, 300);
        private static final String NOT_VISIBLE = "Not Visible";
        private static final String VISIBLE = "Visible";
        private static final long serialVersionUID = 1L;
        private InnerPanel innerPanel = new InnerPanel();
        private JViewport viewport = new JViewport();
        private JLabel statusLabel = new JLabel(NOT_VISIBLE);
    
        IsRectVisibleGui() {
            JScrollPane scrollpane = new JScrollPane();
            scrollpane.setViewport(viewport);
            viewport.add(innerPanel);
            scrollpane.setPreferredSize(SCROLLPANE_SIZE);
            viewport.addChangeListener(new ChangeListener() {
    
                @Override
                public void stateChanged(ChangeEvent e) {
                    Rectangle viewRect = viewport.getViewRect();
                    if (viewRect.intersects(RECT)) {
                        statusLabel.setText(VISIBLE);
                    } else {
                        statusLabel.setText(NOT_VISIBLE);
                    }
                }
            });
    
            setLayout(new BorderLayout());
            add(scrollpane, BorderLayout.CENTER);
            add(statusLabel, BorderLayout.SOUTH);
        }
    
        class InnerPanel extends JPanel {
    
            private static final long serialVersionUID = 1L;
    
            InnerPanel() {
                setPreferredSize(INNER_PANEL_SIZE);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                g2.setColor(Color.red);
                g2.setStroke(new BasicStroke(4));
                g2.draw(RECT);
            }
        }
    }
    

    and you can move with that by using JScrollPane#scrollRectToVisible(Rectangle aRect)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some text/images that I only want to display in the normal state
I have some text i need to filter out a list of bad words
I have some text that is stored in a variable like this: <div class=foo>text
I have some text that uses Unicode punctuation, like left double quote, right single
I have some text in a UITextView, that I would like to have show
I have some text that has HTML hyper-links in it. I want to remove
I have a html component that includes some javascript. The component is a file
In my Java program, I have a text component that can fire events in
I have some text displaying in a larger font size than what it is
I have some text in a non-English/foreign language in my page, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.