JLabels with an ActionListener?

Hello, Is there a way I can put a actionlistener on a JLabel?
Thanks!!

Edit>> To make the label clickable!
LVL 1
dvd99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dvd99Author Commented:
Edited text of question.
0
sgomsCommented:
put a mouselistener on ur JLabel.

0
sgomsCommented:
In the following program the JLabel's text shows the count of times uev clicked on it. It keeps incrementing for every click that u make on it.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ClickJLabel extends JApplet{
  JLabel label;
      static int count=0;
      public void init(){
        resize(300,300);
            
            label =new JLabel("You have Clicked me "+count+" times");
            label.addMouseListener(new MouseAdapter(){
               public void mouseClicked(MouseEvent e){
                           count++;
                               label.setText("You have Clicked me "+count+" times");
                   }
            });
            getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
            getContentPane().add(label);
      }

}

-sgoms
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

shaveriCommented:
I've put the mouseListener on label...try this

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class arr{
boolean flag=false;
      public arr(){
            JFrame f=new JFrame();
    final JLabel l=new JLabel("Press me");
            l.addMouseListener(new MouseAdapter(){
                  public void mouseClicked(MouseEvent e){
                    if(flag==true)
                              l.setText("you clicked me");
                        else
                              l.setText("you pressed me");
                        flag=!flag;
                  }
             });      

    f.getContentPane().add(l);
            f.pack();
            f.setVisible(true);
      }
      public static void main(String a[]){
            new arr();
      }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
expertmbCommented:
hi,

 int i = 0;

  void label_mouseClicked(MouseEvent e) {

      if(i == 0)
      {
         label.setText("Hello");
         i = 1;
      }
      else
      {
         label.setText("Hi");
         i = 0;
     }
}

mb...
0
sgomsCommented:
shaveri,

i have also posted a progarm to capture the mouseclick on th label. how come u posted an answer? Follow the practice of posting COMMENT'S first.

-sgoms
0
shaveriCommented:
sgoms,
is there any rule set by this site to put always comments,if so let me know
0
dvd99Author Commented:
Well Whats wrong with this?



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class arr{
boolean flag=false;
public arr(){
JFrame f=new JFrame();
TextField t = new TextField(20);
f.getContentPane().add(t);
final JLabel l=new JLabel("Press me");
l.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(flag==true)
t.setText("you clicked me");  
flag=!flag;
}
});

f.getContentPane().add(l);

f.pack();
f.setVisible(true);
}
public static void main(String a[]){
new arr();
}
}
0
shaveriCommented:
what is meant by what's wrong with this,the code is working fine
0
sgomsCommented:
1. Do not use AWT & SWING. So go in for JTextField instead of TextField.

2. 'cos u have declared 't' inside the constructor & its non-final its not available inside ur inner class. so make it as global or declare it as final.

3. you have not added the textfiled to the frame

By default the layout is BorderLayout so u can add ur label to "North" & ur textfield to "Center"

the fst time u click the label nothing gets displayed. if u click it again 'uve clicked me' appears & does not change after that. is that what u want?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class arr{
boolean flag=false;
JTextField t;
public arr(){
JFrame f=new JFrame();
t= new JTextField(20);
f.getContentPane().add(t);
final JLabel l=new JLabel("Press me");
l.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(flag==true)
t.setText("you clicked me");  
flag=!flag;
}
});

f.getContentPane().add("North",l);
f.getContentPane().add("Center",t);

f.setSize(200,200);
f.setVisible(true);
}
public static void main(String a[]){
new arr();
}
}

-sgoms
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.