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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:47:48+00:00 2026-06-17T14:47:48+00:00

I developed a software solution to connect Java components visually and it works fine.

  • 0

I developed a software solution to connect Java components visually and it works fine. My only problem now is that my dashboard is surrounded by a jscrollpane and thats good. But if i get out of the dashboard so that the jscrollpane have to grow, all the lines drawed to connect the components are moving from they position. I have search for methods to get the real screen size of the jscollpane but found nothing.

To grow the jscrollpane if needed i use:

Point p = (mainPanel.getMousePosition());
if(mainPanel.getWidth() - (p.x) < tmp.getWidth() && mainPanel.getHeight() - (p.y) < tmp.getHeight())
{
    dim = new Dimension(dim.width+(mainPanel.getWidth()-p.x),dim.height+mainPanel.getHeight() - (p.y));
}
else if(mainPanel.getHeight() - (p.y) < tmp.getHeight())
{
    dim = new Dimension(dim.width,dim.height+mainPanel.getHeight() - (p.y));
}
else if(mainPanel.getWidth() - (p.x) < tmp.getWidth())
{
    dim = new Dimension(dim.width+(mainPanel.getWidth()-p.x),dim.height);
}
mainPanel.setPreferredSize(dim);

While the mouse is dragged.

So i need to get the coordinates of the left top point in the jscrollpane to calculate the new coordinates or something that shifts the lines correct back.

To draw the lines i use:

@Override
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

    // test
    Line2D tmp_line;
    for (int i = 0; i < abuttons.size(); i++) {
        // System.out.println(abuttons.get(i).out);
        if (abuttons.get(i).out != null) {
            int x1 = abuttons.get(i).getLocation().x
                    + abuttons.get(i).connectBtn_right.getLocation().x 
                    + 12;
            int y1 = abuttons.get(i).getLocation().y
                    + abuttons.get(i).connectBtn_right.getLocation().y 
                    + 9 + 75;
            int x2 = abuttons.get(i).out.getLocation().x
                    + abuttons.get(i).out.connectBtn_left.getLocation().x
                    + 12;
            int y2 = abuttons.get(i).out.getLocation().y
                    + abuttons.get(i).out.connectBtn_left.getLocation().y
                    + 9 + 75;

            tmp_line = new Line2D.Double(x1, y1, x2, y2);
            g2d.setPaint(Color.BLACK);
            g2d.setStroke(new BasicStroke(1.5f));
            g2d.draw(tmp_line);
        }
    }
    if (point1 != null && point2 != null) {
        line2d = new Line2D.Double(point1, point2);
        g2d.setPaint(Color.RED);
        g2d.setStroke(new BasicStroke(1.5f));// set stroke size
        g2d.draw(line2d);
    }
}

Thanks for any help

Here the SSCCE:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JButton;


public class LineDraw extends JFrame {

private JPanel contentPane;
JScrollPane scrollPane = new JScrollPane();
JPanel panel = new JPanel();
JButton btnNewButton = new JButton("New button");
JButton btnNewButton_1 = new JButton("New button");

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                LineDraw frame = new LineDraw();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public LineDraw() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    scrollPane.setBounds(0, 0, 424, 251);
    contentPane.add(scrollPane);

    scrollPane.setViewportView(panel);
    panel.setLayout(null);

    btnNewButton.setBounds(27, 98, 89, 23);
    panel.add(btnNewButton);

    btnNewButton_1.setBounds(213, 137, 89, 23);
    panel.add(btnNewButton_1);
    initListener();
}

private void initListener()
{
    btnNewButton.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent me) {
                super.mouseDragged(me);
                Point pos = panel.getMousePosition();
                System.out.println(pos);
                if(panel.getWidth()-pos.x<50 && panel.getHeight()-pos.y<50)
                {
                    panel.setPreferredSize(new Dimension(panel.getWidth()+50,panel.getHeight()+50));
                }
                else if(panel.getHeight()-pos.y<50)
                {
                    panel.setPreferredSize(new Dimension(panel.getWidth(),panel.getHeight()+50));
                }                   
                else if(panel.getWidth()-pos.x<50)
                {
                    panel.setPreferredSize(new Dimension(panel.getWidth()+50,panel.getHeight()));
                }
                btnNewButton.setLocation(pos);
                repaint();
                validate();
            }
        });
    btnNewButton_1.addMouseMotionListener(new MouseAdapter() {
        @Override
        public void mouseDragged(MouseEvent me) {
            super.mouseDragged(me);
            Point pos = panel.getMousePosition();
            if(panel.getWidth()-pos.x<50 && panel.getHeight()-pos.y<50)
            {
                panel.setPreferredSize(new Dimension(panel.getWidth()+50,panel.getHeight()+50));
            }
            else if(panel.getHeight()-pos.y<50)
            {
                panel.setPreferredSize(new Dimension(panel.getWidth(),panel.getHeight()+50));
            }                   
            else if(panel.getWidth()-pos.x<50)
            {
                panel.setPreferredSize(new Dimension(panel.getWidth()+50,panel.getHeight()));
            }
            btnNewButton_1.setLocation(panel.getMousePosition());
            repaint();
            validate();
        }
    });
}

@Override
public void paint(Graphics g)
{
    super.paint(g);
    g.drawLine(btnNewButton.getLocation().x+10,btnNewButton.getLocation().y+32, btnNewButton_1.getLocation().x+10,btnNewButton_1.getLocation().y+32);
}
}
  • 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-06-17T14:47:49+00:00Added an answer on June 17, 2026 at 2:47 pm

    OVerride paintComponent() of panel instead of paint() of JFrame

    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class LineDraw extends JFrame {
    
        private JPanel contentPane;
        JScrollPane scrollPane = new JScrollPane();
        JPanel panel = new JPanel(){
            @Override
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                g.drawLine(btnNewButton.getLocation().x,btnNewButton.getLocation().y, btnNewButton_1.getLocation().x,btnNewButton_1.getLocation().y);
            }
    
        };
        JButton btnNewButton = new JButton("New button");
        JButton btnNewButton_1 = new JButton("New button");
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        LineDraw frame = new LineDraw();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public LineDraw() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);
    
            scrollPane.setBounds(0, 0, 424, 251);
            contentPane.add(scrollPane);
    
            scrollPane.setViewportView(panel);
            panel.setLayout(null);
    
            btnNewButton.setBounds(27, 98, 89, 23);
            panel.add(btnNewButton);
    
            btnNewButton_1.setBounds(213, 137, 89, 23);
            panel.add(btnNewButton_1);
            initListener();
        }
    
        private void initListener()
        {
            btnNewButton.addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseDragged(MouseEvent me) {
                    super.mouseDragged(me);
                    Point pos = panel.getMousePosition();
                    System.out.println(pos);
                    if(panel.getWidth()-pos.x<50 && panel.getHeight()-pos.y<50)
                    {
                        panel.setPreferredSize(new Dimension(panel.getWidth()+50,panel.getHeight()+50));
                    }
                    else if(panel.getHeight()-pos.y<50)
                    {
                        panel.setPreferredSize(new Dimension(panel.getWidth(),panel.getHeight()+50));
                    }
                    else if(panel.getWidth()-pos.x<50)
                    {
                        panel.setPreferredSize(new Dimension(panel.getWidth()+50,panel.getHeight()));
                    }
                    btnNewButton.setLocation(pos);
                    validate();
                    repaint();
                }
            });
            btnNewButton_1.addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseDragged(MouseEvent me) {
                    super.mouseDragged(me);
                    Point pos = panel.getMousePosition();
                    if(panel.getWidth()-pos.x<50 && panel.getHeight()-pos.y<50)
                    {
                        panel.setPreferredSize(new Dimension(panel.getWidth()+50,panel.getHeight()+50));
                    }
                    else if(panel.getHeight()-pos.y<50)
                    {
                        panel.setPreferredSize(new Dimension(panel.getWidth(),panel.getHeight()+50));
                    }
                    else if(panel.getWidth()-pos.x<50)
                    {
                        panel.setPreferredSize(new Dimension(panel.getWidth()+50,panel.getHeight()));
                    }
                    btnNewButton_1.setLocation(panel.getMousePosition());
                    validate();
                    repaint();
                }
            });
        }
    
    }
    

    And remove the paint() method entirely.

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

Sidebar

Related Questions

I have developed a Mac software (using DiscRecordingFramework and IOKit) that creates hybrid Video-DVD.
I always designed/developed/released a software or web project myself/independent activity using java/vb.net and php
Recently I developed a piece of software that downloads logfiles from our online production
I have a software solution for Exchange 2000/2003/2007 and now hopefully after this for
The in-house developed software where I work connects directly to a mysql server here
I've developed some software on my Desktop which runs Windows 7 and the software
I've developed a software in PyQt which plays sound.I'm using Phonon Library to play
When a software is developed,various types of testing is done - unit,integration,functional,manual.In my current
I'm part of a software development company where we do custom developed applications for
I've been a Software Developer now for over 10 years. I've mostly worked in

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.