[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

JLabels with an ActionListener?

Posted on 1999-12-16
10
Medium Priority
?
2,741 Views
Last Modified: 2013-11-23
Hello, Is there a way I can put a actionlistener on a JLabel?
Thanks!!

Edit>> To make the label clickable!
0
Comment
Question by:dvd99
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 1

Author Comment

by:dvd99
ID: 2285880
Edited text of question.
0
 
LVL 5

Expert Comment

by:sgoms
ID: 2285907
put a mouselistener on ur JLabel.

0
 
LVL 5

Expert Comment

by:sgoms
ID: 2285917
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 2

Accepted Solution

by:
shaveri earned 120 total points
ID: 2286040
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
 
LVL 6

Expert Comment

by:expertmb
ID: 2286066
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
 
LVL 5

Expert Comment

by:sgoms
ID: 2286141
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
 
LVL 2

Expert Comment

by:shaveri
ID: 2286457
sgoms,
is there any rule set by this site to put always comments,if so let me know
0
 
LVL 1

Author Comment

by:dvd99
ID: 2288558
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
 
LVL 2

Expert Comment

by:shaveri
ID: 2288856
what is meant by what's wrong with this,the code is working fine
0
 
LVL 5

Expert Comment

by:sgoms
ID: 2288871
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

612 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