Hey Folks,

Playing with Java Swing for a quiet a month now. Its more interesting than I thought of. This article is about adding AtionListener to a JLabel. You think its useless? Well.. read on!

Its usual to associate an action with a JButton. Its click, when I click a button an action is performed. (Clicking Next button in an app, takes me to next JFrame). But there exists situations where you want to associate a ActionListener to JLabel. I encountered one such recently.

The scenario is, an imageIcon Label (A label with image painted on it) is added to a panel. When I single-click the icon (a JLabel actually) , the resize border appears on the icon, letting me to resize the icon. When I right-click the icon, a pop-up menu appears showing the option to Delete this icon. When I double-click the icon, it should take me to the next frame.

Lets leave the single-click and right-click. I am posting a simple example of

  • Listening to double-click
  • Associate an action with a JLabel double-click
  • Frame Navigation, of course

There are 3 files

  1. LinkingFrames.java  – Main class creates the parent JFrame. Has a label named NEXT. It takes me to next frame (Child Frame)
  2. ChildFrame.java – Has a label named BACK. Takes me back to parent frame
  3. MouseListenerHandler.java – Listens for mouse double-click and triggers the  action corresponding to the frame you are in. (If u r in parent frame, u r taken to child and vice-versa)
CLASS : LinkingFrames.java
package linkingframes;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
 *
 * @author poochedi
 */
public class LinkingFrames {
    public static JFrame parent;
    ChildFrame childframe;
    public LinkingFrames(){
        parent = new JFrame(“Parent Frame”);
        parent.setName(“MY NAME IS CHILD”);
        parent.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        parent.setSize(400,400);
        parent.setVisible(true);
        JLabel next = new JLabel(“NEXT”, JLabel.CENTER);
        next.setToolTipText(“Double click to navigate”);
        parent.add(next, BorderLayout.CENTER);
        next.setName(“next”);
        next.addMouseListener(new MouseListenerHandler());
        //JButton nextButton = new JButton(“CLICK TO GO TO NEXT FRAME”);
        //parent.add(nextButton, BorderLayout.CENTER);
        //nextButton.addActionListener(new FrameActionHandler());
        //nextButton.setActionCommand(“next”);
    }
    /**
     * @param args the command line arguments
     */
        public static void main(String[] args) {
        // TODO code application logic here
        new LinkingFrames();
    }
}
//================================================================================================================================================
CLASS :  ChildFrame.java

package linkingframes;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
*
* @author poochedi
*/

public class ChildFrame {

public static JFrame child;
JButton backButton;
static boolean instanceExists = false;

public ChildFrame() {

child = new JFrame(“Child Frame”);
child.setName(“MY NAME IS CHILD”);
instanceExists = true;
// backButton = new JButton(“CLICK TO GO TO PARENT FRAME”);
// backButton.setActionCommand(“previous”);
// child.add(backButton, BorderLayout.CENTER);
child.setSize(600,500);
child.setVisible(true);
// backButton.addActionListener(new FrameActionHandler());
JLabel back = new JLabel(“BACK”, JLabel.CENTER);
back.setToolTipText(“Double click to navigate”);
child.add(back, BorderLayout.CENTER);
back.setLabelFor(backButton);
back.setName(“back”);
  back.addMouseListener(new MouseListenerHandler());

}
}

//====================================================================================================================================================

CLASS : MouseListenerHandler.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package linkingframes;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
 *
 * @author poochedi
 */
public class MouseListenerHandler implements MouseListener {
        public MouseListenerHandler() {
        }
        @Override
        public void mouseClicked(MouseEvent e) {
            if(e.getClickCount() >=2) {
                String actionName = e.getComponent().getName();
                System.out.println(actionName);
                if(actionName.equals(“next”)){
                    if(ChildFrame.instanceExists)
                              ChildFrame.child.setVisible(true);
                        else
                            new ChildFrame();
                        LinkingFrames.parent.setVisible(false);
                }
                else if(actionName.equals(“back”)) {
                    LinkingFrames.parent.setVisible(true);
                    ChildFrame.child.setVisible(false);
                }
            }
        }
        @Override
        public void mousePressed(MouseEvent e) {
          //  throw new UnsupportedOperationException(“Not supported yet.”);
        }
        @Override
        public void mouseReleased(MouseEvent e) {
           // throw new UnsupportedOperationException(“Not supported yet.”);
        }
        @Override
        public void mouseEntered(MouseEvent e) {
           // throw new UnsupportedOperationException(“Not supported yet.”);
        }
        @Override
        public void mouseExited(MouseEvent e) {
            //throw new UnsupportedOperationException(“Not supported yet.”);
        }
    }
the code is self-explanatory.  Hope u found it useful. Bye 🙂
Advertisements