The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Java - do action when JFrame is clicked

SeguerSeguer of the VoidSydney, AustraliaRegistered User regular
edited March 2007 in Help / Advice Forum
Hey guys,

I've successfully created some buttons and menu items that change the text on a label (hurray!) but have no idea how to respond to when the frame itself is click (double clicked in this case). I am assuming some sort of listener but everything I find is either for component, window, or internal JFrames - they only deal with minimise, expand, open, close, etc.

How do I do something when the frame itself is double clicked?

Code for creation of JFrame:
public Frame() {
        //calls the superclass constructor passing as a parameter the title of the frame.
        super("Frame");
        //Creates the msglabel, adds it to the frame and sets an initial message.
        msgLabel = new JLabel("Click a button", JLabel.CENTER); 
        getContentPane().add(msgLabel, BorderLayout.CENTER);
        
        initializeButtons();
        initializeMenu();

        getContentPane().setBackground(new Color(255, 255, 208));
        
        pack();
        setSize(300,200); 
        setResizable(false);
        setVisible(true);        
    }

Note: a lot of that code is not mine, given to us as part of the lab exercise. :P

Seguer on

Posts

  • ffordefforde Registered User regular
    edited March 2007
    You kind of answered your own question. A JFrame is decendent of Component, so you should be able to use addMouseListener(...) to check for clicks on the JFrame itself. Why are you checking for double clicks on the actual frame though? Most of the time it will already have all the functionality you need built in. Anything additional most likely ought to be done with additional components.

    fforde on
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited March 2007
    It's just part of the exercise - we're learning about the GUI so they're just getting us to do random stuff :P

    I'll take a look at mouseListener, thanks.

    EDIT: Solved. Thanks fforde - I completely glazed over the fact that a double click is from a mouse, therefore a listener should be looking at the mouse (oops).

    Seguer on
Sign In or Register to comment.