Solved

How do I add a label

Posted on 2004-04-04
5
237 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

729 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