Hello Forum
I am new in java.I am createing a Frame.Which have two label lb1,lb2.
I want to set MouseListener for both label.How can i write plz tell me
Thxs in advance..
Printable View
Hello Forum
I am new in java.I am createing a Frame.Which have two label lb1,lb2.
I want to set MouseListener for both label.How can i write plz tell me
Thxs in advance..
First define the mouse listener as follows.
Then simply add it to the label (any control).Code:MouseListener mouseListener = new MouseListener() {
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
};
Did you try on the web, there you can found lots of examples. And also on the suns' web site you can find about How to Write a Mouse Listener
i try to add on this way but how can i add this for two label.I add some more even.Like ActionListener and it is working.
But how i MouseListener for like this.Becouse when i add like thatCode:public void actionPerformed(ActionEvent e) {
String actionCmd = e.getActionCommand();
if (actionCmd.equals("Show File ")) {
try {
File file = new File("");
String[] cmdArray = {"cmd", "/c", "start", "\"\"", file.getAbsolutePath()};
Runtime.getRuntime().exec(cmdArray);
}
catch (Exception x)
{
x.printStackTrace();
}
}
else if (actionCmd.equals("Help Topics"))
{
try {
File file = new File("chm.chm");
String[] cmdArray = {"cmd", "/c", "start", "\"\"", file.getAbsolutePath()};
Runtime.getRuntime().exec(cmdArray);
}
catch (Exception x)
{
x.printStackTrace();
}
}
}
Then i am geting error inCode:public void mouseClicked(MouseEvent e){
if(e.getSource().equals(JLabel)
{
// Some code here
}
Showing errorn on JLabel..Code:if(e.getSource().equals(JLabel)
How can i remove this problem..
Plz help me
Sir i am useing MouseListener this way..
What happen both link is open click on any label.So How can i set the this sir.Code:JLabel1.addMouseListener(this);
JLabel2.addMouseListener(this);
public void mouseClicked(MouseEvent e){
if(e.getClickCount()==2)
{
try {
File file = new File("");
String[] cmdArray = {"cmd", "/c", "start", "\"\"", file.getAbsolutePath()};
Runtime.getRuntime().exec(cmdArray);
}
catch (Exception x)
{
x.printStackTrace();
} }
else
{
File file = new File("chm.chm");
String[] cmdArray = {"cmd", "/c", "start", "\"\"", file.getAbsolutePath()};
Runtime.getRuntime().exec(cmdArray);
}
Plz help me
Hi All
Plz help me any one..
Thx's in Advance..
In simple way to solve this is implement two mouse listeners and add them to two labels. So you can implement different functionality there.
Mr. Eranga is very help full man.So what he told you ,Just go through this..
Any way this a code sample ,which may be help you..
Code:public void mouseClicked(MouseEvent e){
if(e.getSource() instanceof JLabel)
{
JLabel clicked = (JLabel)(e.getSource());
if(clicked == one)
{
}
if(clicked==two)
{
}
Actually Mir, what your code doing is, get the click label event and depend on that do the processing. In that way you need only one mouse listener.
There is two choices, he can you any he likes actually.
That's better. You learn something here from the community, and the best thing is give something back our community. Well done pal. But always make sure that you gave the right answer. ;)