How to Add Action Mouse Listener in JLabel with Java GUI | Java Programming Tutorial

Friday, July 29, 2011

Making JLabel with action listener, How can I make JLabel with Action Listener?, Action Listener with JLabel, How the way to make JLabel with Action Listener in JAva GUI Programming, How to add mouse listener to JLabel in Java GUI programming

In this post I will tell you about how to add action listener to JLabel in JAva GUI programming, I'll make a frame. And add a Label and I'll give the label an Action Listener. So, When I click the Program, an event will happen.

May be You thought that JLabel can't make action listener. But with this source code, I will tell you about how to do that. You will get a frame like this :


When you click the Label (text). You will get action like this :


Save this source code with name "labelaction.java"
____________________________________

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

public class labelaction extends JFrame {

public static void main(String[] args) {
labelaction frame = new labelaction();
}

JLabel click = new JLabel("Click Here");
JLabel alert = new JLabel("You Have Clicked A Label");
JPanel panel = new JPanel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);

labelaction(){
super("Login Autentification");
setSize(300,200);
setLocation(500,280);
panel.setLayout (null);


click.setBounds(110,50,80,20);
alert.setBounds(70,100,180,20);
panel.add(click);
panel.add(alert);
alert.setVisible(false);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
mouseactionlabel();
}

void mouseactionlabel(){
click.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent arg0) {
alert.setVisible(true);
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
}
}

____________________________________________-


good luck...

Hopefully with this article increase your programming ability. This is just a simple trick, all I submit to you to develop it.

Thanks...

5 comments:

Thanks for this article, why Oracle docs say that JLabel´s don´t respond to input ?

Java Tutorials Nanoverso

sasitamil said...

It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training

leafcuremed said...

buy glock 19
cheapest place to buy glock 19 online

Anonymous said...

Dafabet Review | Thauberbet
In this Dafabet review, we'll show you all the Dafabet bonuses, banking and bonuses. dafabet We have gathered the most up to date 샌즈카지노 details about the Dafabet 바카라사이트

Scopex said...

Canli

Post a Comment

 
 
 
 
Copyright © Programming Tricks