JLabels with MouseListener

my teacher gave us an assignment to do

he gives us what he calls "starter code" and makes us use his starter code

in our own code. sounds good? the thing is one of the lines he has does not

make sense to me.

in a method that is in a class that extends mouseadapter

mouseClicked

the first line of code he has is

JLabel label = (JLabel) e.getComponent();

um... im assuming that if my code of

String x = label.getText();

would give me the string that is on the JLabel

however, i get an error instead

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException

and since i am not aloud to change his code,

i am forced to learn a way to go about getting the contents of a JLabel when i click it using his starter code.

Re: I posted this on the New to Java Technology forum and got one

response from walken16 (thank you by the way). His reply went something like this:

just suppose that the variable "e" is one of these

mouse event //which is a correct assumption

you will find it's methods in the previous link.

here is the doc for JLabel

JLabel

you won't have any luck casting an event to one of these

sincerely

Walken

So... im thinking to myself.. ive been trying for 2 days now and I cannot figure this thing out? Is there a way to make this bad cast work? If this is impossible, please let me know so I can stop wasting my time trying and spend my time giving my teacher hell for this torture.

Thanks

[1573 byte] By [apple_ipoda] at [2007-10-2 6:44:47]
# 1
It doesn't make sense and/or need more information
tjacobs01a at 2007-7-16 13:53:18 > top of Java-index,Java Essentials,Java Programming...
# 2

> Is there

> a way to make this bad cast work?

NO, no way to make a bad cast work

If this is

> impossible, please let me know so I can stop wasting

> my time trying and spend my time giving my teacher

> hell for this torture.

> Thanks

just go ahead and post all of the code your teacher gave to you.

walken16

walken16a at 2007-7-16 13:53:18 > top of Java-index,Java Essentials,Java Programming...
# 3

here you go, hope this helps

at least it might give you something to ask questions about.

the reason you didn't get better response was due to some vagueness on your part.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.event.*;

import javax.swing.border.LineBorder;

public class LabelTest {

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label = new JLabel("apple_ipod");

label.setBorder(new LineBorder(Color.BLACK));

frame.getContentPane().setLayout(new FlowLayout());

frame.getContentPane().add(label);

frame.pack();

frame.setLocationRelativeTo(null);

MouseListener listener = new MouseAdapter(){

public void mouseClicked(MouseEvent me){

Object obj = me.getSource();

JLabel myLabel = (JLabel)obj;

System.out.println("event from label: "+myLabel.getText());

}

};

label.addMouseListener(listener);

frame.show();

}

}

walken16a at 2007-7-16 13:53:18 > top of Java-index,Java Essentials,Java Programming...
# 4

and the response you mentioned was on this thread

[url]http://forum.java.sun.com/thread.jspa?threadID=688283[/url]

and you only waited 15 minutes.

I realize that you are frustrated and i'll try to help but you need to slow

down when you post and give us some more info.

It doesn't appear that your instructors code will work but we don't know.

best option is to change it as required and you'll be the only one that gets it to work.

that is if you are graded on actually completing and running the code

and not just turning in code on a peice of paper.

sincerely

Walken16

walken16a at 2007-7-16 13:53:18 > top of Java-index,Java Essentials,Java Programming...
# 5
i tried removing the line before the bad casting occurs at JLabel myLabel = (JLabel) me and it said bad cast... the teacher said explicitly not to alter the starter code so the first line of that method is the bad cast
apple_ipoda at 2007-7-16 13:53:18 > top of Java-index,Java Essentials,Java Programming...
# 6
your bug is not in that code
tjacobs01a at 2007-7-16 13:53:18 > top of Java-index,Java Essentials,Java Programming...