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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:41:22+00:00 2026-06-15T15:41:22+00:00

Code in Question: textArea.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { posX = e.getX();

  • 0

Code in Question:

    textArea.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            posX = e.getX();
            posY = e.getY();
        }
    });
    textArea.addMouseMotionListener(new MouseAdapter() {
        public void mouseDragged(MouseEvent e) {
            setLocation(e.getXOnScreen() - posX, e.getYOnScreen() - posY);
        }
    });

Background:

I have a JFrame, in that JFrame there is a JScrollPane, and in the JScrollPane there is a JTextArea called “textArea”. This JTextArea take up the entire JFrame and the JFrame is undecorated. So to give some perspective, here is generally what the JFrame looks like…

example image

When the mouse clicks within the JTextArea and moves, the entire window is dragged. Everything is setup to not be focus able for this work, it’s meant to be an overlay.

Issue:

The code listed above works fine and the world is at peace. But once there is enough text for the vertical scroll bar to appear (There is no horizontal because of line wrapping), dragging the window becomes an issue. When you click and just begin to move, the JFrame instantly moves much higher on the screen. The lines in JTextArea, the higher it moves up when you try to move it. I assume that the get*OnScreen() methods are issue because it’s all relevant to the JTextArea.

Class in Question:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main extends JFrame {
    private JTextArea textArea;
    private JScrollPane textAreaScroll;
    private int posX = 0;
    private int posY = 0;

public Main() {
    initComponents();
    initListeners();
    for(int i = 0; i < 20; i++){
        addLine(i+" Hello");            
    }
}

public void addLine(String line){
    textArea.append("\n> "+line);
    textArea.setCaretPosition(textArea.getDocument().getLength());
}

private void initListeners(){
    textArea.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            posX = e.getX();
            posY = e.getY();
        }
    });
    textArea.addMouseMotionListener(new MouseAdapter() {
        public void mouseDragged(MouseEvent e) {
            setLocation(e.getXOnScreen() - posX, e.getYOnScreen() - posY);
        }
    });
}

private void initComponents() {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {}

    textAreaScroll = new JScrollPane();
    textArea = new JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setUndecorated(true);
    setAlwaysOnTop(true);
    setAutoRequestFocus(false);
    setBackground(new Color(130,210,255,130));
    setFocusCycleRoot(false);
    setFocusable(false);
    setFocusableWindowState(false);
    setName("main");
    setOpacity(0.4f);
    setResizable(false);

    textAreaScroll.setBorder(null);
    textAreaScroll.setFocusable(false);
    textAreaScroll.setRequestFocusEnabled(false);

    textArea.setEditable(false);
    textArea.setBackground(new Color(0, 0, 0));
    textArea.setColumns(20);
    textArea.setFont(new Font("Consolas", 0, 14));
    textArea.setForeground(new Color(255, 255, 255));
    textArea.setLineWrap(true);
    textArea.setRows(5);
    textArea.setText("> Hello world!\n> another line!");
    textArea.setBorder(null);
    textArea.setFocusable(false);
    textArea.setRequestFocusEnabled(false);
    textAreaScroll.setViewportView(textArea);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(textAreaScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            );
    layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(textAreaScroll, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
            );

    pack();
}

public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Main().setVisible(true);
        }
    });
}

}

  • 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-15T15:41:23+00:00Added an answer on June 15, 2026 at 3:41 pm

    Well your diagnosis was absolutely spot on:

    When you click and just begin to move, the JFrame instantly moves
    much higher on the screen. The lines in JTextArea, the higher it moves
    up when you try to move it. I assume that the get*OnScreen() methods
    are issue because it’s all relevant to the JTextArea.

    So to resolve this use GlassPane of JFrame to attach MouseXXXListeners thus we can get correct co-ordinates when dragging, the main problem with this solution is glasspane will consume events that are meant for other components on JFrame, this can be overcome by redispatching the MouseEvents appropriately):

    • Create JPanel (this glassPane/JPanel will be transparent via setOpaque(false)), attach xxxAdapters here.

    • Create custom listener class to redispacth MouseEvents to the necessary components (as glasspane will consume all events to the JTextArea/JScollPane)

    • Set JPanel as GlassPane of your JFrame via JFrame#setGlassPane(..) .

    • set JFrame visible than set glassPane visible via setVisible(true) (this has been a Swing glitch for some time if you set it visible before the frame is visible it wont be shown).

    Here is your fixed code:

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.Point;
    import java.awt.Toolkit;
    import java.awt.event.MouseEvent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.event.MouseInputAdapter;
    
    public class Main extends JFrame {
    
        private JTextArea textArea;
        private JScrollPane textAreaScroll;
        private JPanel glassPane;//create variable for glasspane
    
        public Main() {
            initComponents();
            initListeners();
            for (int i = 0; i < 20; i++) {
                addLine(i + " Hello");
            }
        }
    
        public void addLine(String line) {
            textArea.append("\n> " + line);
            textArea.setCaretPosition(textArea.getDocument().getLength());
        }
    
        private void initListeners() {
            GlassPaneListener gpl = new GlassPaneListener(textAreaScroll.getVerticalScrollBar(), this);
            //add the adapters/listeners to the glasspane
            glassPane.addMouseMotionListener(gpl);
            glassPane.addMouseListener(gpl);
        }
    
        private void initComponents() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            }
    
            textAreaScroll = new JScrollPane();
            textArea = new JTextArea();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setUndecorated(true);
            setAlwaysOnTop(true);
            setAutoRequestFocus(false);
            setBackground(new Color(130, 210, 255, 130));
            setFocusCycleRoot(false);
            setFocusable(false);
            setFocusableWindowState(false);
            setName("main");
            setOpacity(0.4f);
            setResizable(false);
    
            textAreaScroll.setBorder(null);
            textAreaScroll.setFocusable(false);
            textAreaScroll.setRequestFocusEnabled(false);
    
            textArea.setEditable(false);
            textArea.setBackground(new Color(0, 0, 0));
            textArea.setColumns(20);
            textArea.setFont(new Font("Consolas", 0, 14));
            textArea.setForeground(new Color(255, 255, 255));
            textArea.setLineWrap(true);
            textArea.setRows(5);
            textArea.setText("> Hello world!\n> another line!");
            textArea.setBorder(null);
            textArea.setFocusable(false);
            textArea.setRequestFocusEnabled(false);
            textAreaScroll.setViewportView(textArea);
            textAreaScroll.setPreferredSize(new Dimension(200, 200));
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textAreaScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE));
            layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textAreaScroll, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE));
    
            //create and make glasspane not opaque
            glassPane = new JPanel();
            glassPane.setOpaque(false);
    
            //set glasspane as JFrame glassPane
            setGlassPane(glassPane);
    
            pack();
    
            setVisible(true);//set JFrame visible
    
            //glassPane can only be setVisible after JFrame is visible
            glassPane.setVisible(true);
        }
    
        public static void main(String args[]) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Main();
                }
            });
        }
    }
    
    class GlassPaneListener extends MouseInputAdapter {
    
        private int posX = 0;
        private int posY = 0;
        Toolkit toolkit;
        private final Container contentPane;
        private final Component textAreaScroll;
        private final Component glassPane;
        private final JFrame frame;
        private boolean wasClickOnInterestedComponent = false;
    
        public GlassPaneListener(Component textAreaScroll, JFrame frame) {
            toolkit = Toolkit.getDefaultToolkit();
            this.textAreaScroll = textAreaScroll;
            this.frame = frame;
            this.glassPane = frame.getGlassPane();
            this.contentPane = frame.getContentPane();
        }
    
        @Override
        public void mouseDragged(MouseEvent e) {
            if (!redispatchMouseEvent(e)) {
                frame.setLocation(e.getXOnScreen() - posX, e.getYOnScreen() - posY);
            }
        }
    
        @Override
        public void mousePressed(MouseEvent e) {
            if (!redispatchMouseEvent(e)) {//check if event was redispatched if not its meant for us :)
                posX = e.getX();
                posY = e.getY();
            }
        }
    
        @Override
        public void mouseReleased(MouseEvent me) {
            wasClickOnInterestedComponent = false;
        }
    
        private boolean redispatchMouseEvent(MouseEvent e) {
            Point glassPanePoint = e.getPoint();
            Container container = contentPane;
            Point containerPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, contentPane);
    
            // The mouse event is probably over the content pane.
            // Find out exactly which component it's over.
            Component component = SwingUtilities.getDeepestComponentAt(container, containerPoint.x,
                    containerPoint.y);
    
            if ((component != null) && (component.equals(textAreaScroll)) || wasClickOnInterestedComponent) {
                wasClickOnInterestedComponent = true;//so that if we drag iur cursor off JScrollBar tghe window wont be moved
                // Forward events over the scrollbar
                Point componentPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, component);
                component.dispatchEvent(new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(),
                        componentPoint.x, componentPoint.y, e.getClickCount(), e.isPopupTrigger()));
                return true;//the event was redispatched
            } else {
                return false;//event was not redispatched
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code: <div class=question> <span class=text_area> <textarea name=text1 id=response1 rows=3 cols=50/> </span>
This isn't a code question for once, but it definitely has me confused. Basically,
This is more of a licencing issue than a code question. I really like
In answering this code golf question , I ran across a problem in my
This is a code review question more then anything. I have the following problem:
Here is the code in question: import wx class MyFrame(wx.Frame): This is MyFrame. It
Hi here is the code in question and the issues that i am getting,
I apologize for making my first question not the hard-hitting code-related question I was
I asked a similar question in Code Review forum but was suggested to ask
I've read this question mentioning Code Bubbles and I've watched their video presentation. The

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.