This discussion is archived
5 Replies Latest reply: Oct 8, 2004 5:06 PM by 807587 RSS

JDesktop automatically get focus

807587 Newbie
Currently Being Moderated
I have a JDesktop with many different classes of JInternalFrames. when the user clicks a JButton in frame#1 and then wants to click a JButton in frame#2, the user must obtain focus on frame#2 and then only after obtaining focus can the user click the jbutton in that frame. basically, unless the user has been "working" in this particular frame, the first time he does something he must "double click"

What is my solution around this? here is what i want:
if a user clicks any X,Y point on the JDesktop and that X,Y point happens to be the location of a JInternalFrame, then it would be the same as if the user already had focus to the frame and clicked that area.

I tried to do some code like this for the JDesktop:

public class myFrame extends JFrame {
JDesktopPane myDesktop;

myFrame() {
....
myDesktop = new JDesktopPane();
getContentPane().add(myDesktop, BorderLayout.Center)
this.addMouseMotionListener(new DesktopListener(this));
}

private class DesktopListener implements MouseMotionListener {
JFrame parent;
public DesktopListener(JFrame frame) {
parent = frame;
}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
// OKAY HERE IS WHERE I AM STUCK
}
}
}

Okay, i am stuck on what to do with mouseMoved() of the motionlistener. i know i can e.getX() and e.getY(), but how do to:
1) determine if that coordinate is actually a JInternalFrame
2) give the JInternalFrame focus at that coordinate focus

and 3) is this the best solution to do what i am trying to accomplish? i am trying to avoid forcing the user to obtain frame focus when clicking components in a frame. i dont necessary want to bring the JInternalFrame to the front, i just want to avoid having to click to get focus

thanks
  • 1. Re: JDesktop automatically get focus
    807587 Newbie
    Currently Being Moderated
    I found 3 ways in giving a JInternalFrame a focus:
    lets say we have
    JInternalFrame jif;
    JDesktopPane jdp;
    1)
    jif.requestFocus()
    2)
    jdp.setSelectedFrame(jif)
    or
    3)
     jdp.getDesktopManager().activateFrame(jif) 
    I recomend using the third one!
    hope it will help
  • 2. Re: JDesktop automatically get focus
    807587 Newbie
    Currently Being Moderated
    the activateFrame() is definitely helpful. but how do i find out what JInternalFrame the mouse is located at on the jdesktoppane? in other words, for the function:

    private class DesktopListener implements MouseMotionListener {
    JFrame parent;
    public DesktopListener(JFrame frame) {
    parent = frame;
    }
    public void mouseDragged(MouseEvent e) {}
    public void mouseMoved(MouseEvent e) {
    // OKAY HERE IS WHERE I AM STUCK
    // I WOULD LIKE TO DO SOMETHING LIKE:
    Object obj = (parent.getContentPane().getComponentAt(e.getX(),e.getY()));
    if(obj instanceof JInternalFrame) {   // CODE WILL NOT WORK
    myDesktop.getDesktopManager().activateFrame(obj);
    }
    }

    i would like to do something like the above. that code will not work, of course. can you assist?
    I found 3 ways in giving a JInternalFrame a focus:
    lets say we have
    JInternalFrame jif;
    JDesktopPane jdp;
    1)
    jif.requestFocus()
    2)
    jdp.setSelectedFrame(jif)
    or
    3)
     jdp.getDesktopManager().activateFrame(jif)
    I recomend using the third one!
    hope it will help
  • 3. Re: JDesktop automatically get focus
    807587 Newbie
    Currently Being Moderated
    Maybe you should try a different approach:
    try adding a MouseMotionListener to the internalframes which will do this.
    //jdp is an instance of JDesktopPane
    
    MouseMotionAdapter  mma=new MouseMotionAdapter()
    {
       public void mouseMoved(MouseEvent me)
       {
              JInternalFrame jif=(JInternalFrame)me.getSource();
              jdp.getDesktopManager().activateFrame(jif);
       }
    };
    
    JInternalFrame jif=new JInternalFrame("whatever");
    jif.addMouseMotionListener(mma);
    I belive it should work well.

    p.s.
    what do you mean (parent.getContentPane().getComponentAt(e.getX(),e.getY())); did not work?
    if you do not want my approach check what does that function return to you.
  • 4. Re: JDesktop automatically get focus
    807587 Newbie
    Currently Being Moderated
    I implemented your approach, which definitely makes good sense. i ran into two problems:
    1) it only fires the event when the mouse is on the border. if i move the mouse slowly into the internalframe, the event will probably fire. but if i move quickly into the frame, then it never fires. if i put the mouse on the border and stay there for a second, it will always fire.
    2) it brings the internalframe to the front. if the internalframe is covered by another frame, i would prefer that it doesnt come to the front, but just gets the focus.

    any ideas?

    Maybe you should try a different approach:
    try adding a MouseMotionListener to the internalframes
    which will do this.
    //jdp is an instance of JDesktopPane
    
    MouseMotionAdapter  mma=new MouseMotionAdapter()
    {
    public void mouseMoved(MouseEvent me)
    {
    JInternalFrame
    rnalFrame jif=(JInternalFrame)me.getSource();
    jdp.getDesktopManager().activateFrame(jif);
    }
    };
    
    JInternalFrame jif=new JInternalFrame("whatever");
    jif.addMouseMotionListener(mma);
    I belive it should work well.

    p.s.
    what do you mean
    (parent.getContentPane().getComponentAt(e.getX(),e.getY
    ))); did not work?
    if you do not want my approach check what does that
    function return to you.
  • 5. Re: JDesktop automatically get focus
    807587 Newbie
    Currently Being Moderated
    This is way late, but I put it here for posterity:

    The MMA is definitely the way to go to solve this problem, IMHO. And, I understand why it only works if you move slowly. That's because once you cross the border of the frame, you're into a different component. The component inside the frame gets the MM event, not the JInternalFrame itself! SO, you must give the component a MMA.

    For example, I have a JList inside of a JInternalFrame. What I do is create a MMA inside the JList. When I receive motion events, I tell the JList to do a
    jif.setSelected(true)
    where jif is the parent frame that my JList is inside! This works great for me. Oh, and I also have the JList requestFocus(). I'm not sure if that step is necessary.

    Unfortunately, setSelected() brings the JInternalFrame to the front. Not sure what to do about that, except use the Desktop Manager to save and restore window layouts... that might be a sticky problem.

    But before my technique, I had a heck of a time trying to drag list names out and drop them into another JInternalFrame. I had to click into the list first, then drag to another JInternalFrame. Now I am quite happily clicking and dragging between the JList in its Internal Frame, and other InternalFrame's.

    This is for JDK 1.4.2
    -Mike Schwager