Solved

How do I add a label

Posted on 2004-04-04
5
239 Views
Last Modified: 2010-03-31
How do I add a label to a canvas and move the label with the mouse on every mouseDragged event.
0
Comment
Question by:amba2004
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10753656
label.setPosition(me.getX(), me.getY()); // 'me' is the MouseEvent
0
 
LVL 92

Expert Comment

by:objects
ID: 10753660
Use a null layout, and set the label location in your mouseDragged.

label.setLocation(event.getX(), event.getY());
0
 
LVL 16

Expert Comment

by:gnoon
ID: 10754832
>How do I add a label to a canvas
You *cannot* add Label to a Canvas since Canvas is not a Container.
0
 
LVL 16

Accepted Solution

by:
gnoon earned 100 total points
ID: 10755069
In you need Canvas anyway, this example can tells you how to do work around.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Frm extends JFrame {

    public Frm() {
        super("");
        Container c = getContentPane();
        c.setLayout(new BorderLayout());
        setSize(400,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        MyCanvas cv = new MyCanvas();
        c.add(cv);
    }

    public static void main(String[] args) {
        Frm f1 = new Frm();
        f1.show();
    }
}

class MyCanvas extends Canvas implements MouseMotionListener {
    int x = 0;
    int y = 10;
    public MyCanvas() {
        addMouseMotionListener(this);
    }
    public void paint(Graphics g) {
        g.setColor(Color.black);
        g.drawString("Label",x,y);
    }
    public void setXY(int x,int y) {
        this.x = x;
        this.y = y;
        repaint();
    }
    public void mouseDragged(MouseEvent e){
        setXY(e.getX(),e.getY());
    }
    public void mouseMoved(MouseEvent e) {}
}
0
 
LVL 16

Expert Comment

by:gnoon
ID: 10771234
:)
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question