Solved

How do I add a label

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now