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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:27:01+00:00 2026-05-14T07:27:01+00:00

I am trying to add MouseMotion event to the label and move it based

  • 0

I am trying to add MouseMotion event to the label and move it based on the dragging of the mouse and make it move along with my mouse.However the mousemotion is very difficult to control making this action not usable.
Here is the code

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class TableTest
{
    public TableTest()
    {
        String[] columnNames =
        { "FileName", "Integer" };
        Object[][] data =
        {
        { new FileName("AAA.jpg", Color.YELLOW), new Integer(2) },
        { new FileName("BBB.png", Color.GREEN), new FileName("BBB.png", Color.GREEN) },
        { new FileName("CCC.jpg", Color.RED), new Integer(-1) }, };
        DefaultTableModel model = new DefaultTableModel(data, columnNames)
        {
            public Class getColumnClass(int column)
            {
                System.out.println("column is" + column);
                return getValueAt(0, column).getClass();
            }
        };
        JTable table = new JTable(model);
        //JTable table = new JTable(data, columnNames);
        table.setDefaultRenderer(FileName.class, new FileNameCellRenderer());
        final JLabel label = new JLabel("TESTING", SwingConstants.CENTER);

        label.setBackground(java.awt.Color.RED);
        label.setBounds(450, 100, 90, 20);
        label.setOpaque(true);
        label.setVisible(true);

        label.addMouseMotionListener(new MouseMotionListener()
        {

            public void mouseDragged(MouseEvent arg0)
            {
                label.setBounds(arg0.getX(), arg0.getY(), 90, 20);

            }

            public void mouseMoved(MouseEvent arg0)
            {
                // TODO Auto-generated method stub

            }

        });
        table.add(label);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(table);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    static class FileNameCellRenderer extends DefaultTableCellRenderer
    {
        public Component getTableCellRendererComponent(JTable table, Object v,
                boolean isSelected, boolean hasFocus, int row, int column)
        {
            super.getTableCellRendererComponent(table, v, isSelected, hasFocus,
                    row, column);
            FileName fn = (FileName) v;
            setBorder(BorderFactory.createMatteBorder(0, 60, 0, 0,
                    new java.awt.Color(143, 188, 143)));
            return this;
        }
    }

    static class FileName
    {
        public final Color color;

        public final String label;

        FileName(String l, Color c)
        {
            this.label = l;
            this.color = c;
        }

        public String toString()
        {
            return label;
        }
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new TableTest();
            }
        });
    }
}

I just want to make the label follow the label follow my mouse and the label should be attached to the table.Is there an easy way than this.

  • 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-14T07:27:01+00:00Added an answer on May 14, 2026 at 7:27 am

    First of all, I think you using the mouse event coordinates (x,y) incorrectly, since those are relative to the source component. In this case the source is the label itself. So you should add to those values the original (x,y) of the source component:

    label.setBounds(label.getX() + arg0.getX(), label.getY() + arg0.getY(),90,20);
    

    It’s been a while since I had to struggle with the mouse events, but maybe you could try this and let us know if it helps.

    Another thing which I’m not sure of is the adding of label to the JTable. The LayoutManager of the JTable might prevent you from actually implementing this way, you may need to use GlassPane or something on top of the table.

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

Sidebar

Related Questions

I'm trying to add a little functionality to wpf control via class presented below.
Trying to add my event handler to ComponentDispatcher.ThreadPreprocessMessage event like this: using System; using
trying to add a prefix before bound field <label><input type=radio name=rbGroup value='r<%#((Type)Container.DataItem).ID %>'/><%# ((Type)Container.DataItem).Action
Trying to add a custom pin for current location, However the location does not
I'm trying add data triggers to the default combobox style so each text item
I am trying add picture boxes dynamically. My code is as PictureBox picture =
Im new to asp.net mvc. I'm trying add new model class but it got
Trying to add init parameter names to a list in init(ServletConfig) method. public void
Trying to add a blank sample app for a rails tutorial to GitHub, but
Trying to add a 'box' to a form at design time, I looked up

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.