views:

66

answers:

3

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

+1  A: 

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.

Savvas Dalkitsis
A: 

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

aperkins
+1  A: 

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.

camickr
thanks for the suggestion :)
Hellnar