Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I add a label

Posted on 2004-04-04
5
Medium Priority
?
244 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 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

730 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