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 6122363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:53:25+00:00 2026-05-23T15:53:25+00:00

I want to draw the lines between 2 JScrollPanes (first scroll pane on the

  • 0

I want to draw the lines between 2 JScrollPanes (first scroll pane on the left side, second on the right). These JScrollPanes contain images. I want to draw lines between these 2 images (use some layers, use some trick etc.). I tried do it different ways, but i failed. Is it possible? (if not, i will have to make 2 images in one JScrollPane and it won’t be nice).

EDIT

I want to draw between 2 images – throught components – get some points from images and draw lines between them. I apologize for poorly formulated question.

  • 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-23T15:53:26+00:00Added an answer on May 23, 2026 at 3:53 pm

    In order to accomplish this, I believe you’ll need to make use of the Glass Pane. The Glass Pane sits on top of everything in the JRootPane and fills the entire view. This particular position allows two distinct capabilities:

    • Intercepting mouse and keyboard events
    • Drawing over the entire user interface

    I believe your question is addressed by the second capability. The following is an example implementation, which you can later tailor to meet your own needs. Note that I’ve left out a lot of detail with regard to Glass Pane that you’ll need to research on your own.

    CODE

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.RenderingHints;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;
    import java.awt.event.KeyAdapter;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class GlassPaneDemo {
            private static BufferedImage bi;
    
            public static void main(String[] args){
                try {
                    loadImages();
    
                    SwingUtilities.invokeLater(new Runnable(){
                        @Override
                        public void run() {
                            createAndShowGUI();             
                        }
                    });
                } catch (IOException e) {
                    // handle exception
                }
            }
    
            private static void loadImages() throws IOException{
                bi = ImageIO.read(new File("src/resources/person.png"));
            }
    
            private static void createAndShowGUI(){
                final JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(false);
                frame.setGlassPane(new CustomGlassPane());
                frame.getContentPane().add(getButtonPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.getGlassPane().setVisible(true);
                frame.setVisible(true);
            }
    
            private static final JPanel getButtonPanel(){
                @SuppressWarnings("serial")
                final JPanel panel = new JPanel(){
                    @Override
                    protected void paintComponent(Graphics g){
                        Graphics gCopy = g.create();
    
                        gCopy.setColor(Color.BLUE.darker());
                        gCopy.fillRect(0, 0, getWidth(), getHeight());
    
                        gCopy.dispose();
                    }
                };
    
                final JLabel labelOne = new JLabel();
                labelOne.setIcon(new ImageIcon(bi));
                final JLabel labelTwo = new JLabel();
                labelTwo.setIcon(new ImageIcon(bi));
                panel.add(labelOne);
                panel.add(labelTwo);
    
                return panel;
            }
    
            @SuppressWarnings("serial")
            private static class CustomGlassPane extends JComponent{
                private Point p1;
                private Point p2;
                private boolean lineDrawn;
    
                public CustomGlassPane(){
                    addMouseListener(new MouseAdapter(){
                        @Override
                        public void mouseClicked(MouseEvent e){
                            if(p1 == null || lineDrawn){
                                if(lineDrawn){
                                    p1 = null;
                                    p2 = null;
                                    lineDrawn = false;
                                }
                                p1 = e.getPoint();
                            }else{
                                p2 = e.getPoint();
                                repaint(); // not optimal
                                lineDrawn = true;
                            }
                        }
                    });
    
                    // Block all other input events
                    addMouseMotionListener(new MouseMotionAdapter(){});
                    addKeyListener(new KeyAdapter(){});
                    addComponentListener(new ComponentAdapter(){
                        @Override
                        public void componentShown(ComponentEvent e){
                            requestFocusInWindow();
                        }
                    });
                    setFocusTraversalKeysEnabled(false);
                }
    
                @Override
                protected void paintComponent(Graphics g){
                    if(p1 != null && p2 != null){
                        Graphics2D g2 = (Graphics2D) g.create();
    
                        g2.setRenderingHint(
                                RenderingHints.KEY_ANTIALIASING, 
                                RenderingHints.VALUE_ANTIALIAS_ON);
                        g2.setColor(Color.RED);
                        g2.drawLine((int)p1.getX(), (int)p1.getY(), (int)p2.getX(), (int)p2.getY());
    
                        g2.dispose();
                    }
                }
            }
      }
    

    OUTPUT

    enter image description here

    EXPLANATION

    In this example, I clicked two arbitrary points within each JLabel, and then drew a connecting line.

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

Sidebar

Related Questions

I'm implementing an application which want to draw lines in the panel. But the
I want to draw some lines and rectangles on a panel. Sometimes it does
I want to draw a graph in my Django-based site, to look like these
I want to draw a line between different components in a JPanel, but the
I want draw a line between to specify point in java 3d. how can
Hi I want to draw a line between 2 GeoPoints on Google map in
In R, using maps package, and gcIntermediate function, how do I draw lines between
I want to draw lines ( about 100 ) with different colors. The lines
I want to draw lines on my canvas element at every 48px high. Here's
I'm using pygame to draw a line between two arbitrary points. I also want

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.