简体   繁体   中英

action listening for a JFrame

I am trying to listen tab-in tab-out action for my swing gui that is made by JFrame. I have a JTextField added to the JFrame that will be getting the user clipboard whenever the window is selected so the user may tab between programs, copy some url so when back to my program, this JTextField will be populated by the copied url string.

EDIT:

I have tried this:

    frame.addFocusListener(
            new FocusListener() {
                public void focusGained(FocusEvent e) {

                url= getClipboardData();
                }

                @Override
                public void focusLost(FocusEvent arg0) {
                    // TODO Auto-generated method stub

                }
            }

    );

it doesnt work

What you want is a FocusListener not an ActionListener. Check out the java Doc and you'll know how to use it. It's easy.

A frame doesn't recieve a focus event. A component on the frame gets the focus event.

If you want to know when a frame gets focus then use a WindowListener and handle the windowActivated event.

it looks like you are not setting the clipboard data onto the text field.

frame.addFocusListener(new FocusListener() {
    public void focusGained(FocusEvent e) {
        getJTextField().setText(getClipboardData());
    }
    public void focusLost(FocusEvent e) {
        //ignored
    }
});

Something like that will likely solve your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM