I am new to Java Swing, I have created a JFrame which should get displayed when there is mouse focused over it and should be hidden when the mouse is not over it. Should i add a window listener for it.

You can use a MouseListener to run your code when the mouse enters or leaves the frame, but I'm not sure whether that will work when the frame is hidden - you may need to have a transparent <something> in the place of the frame when it's hidden so that there's something to trigger the mouseEntered event.
When/if you find a complete answer please do share it with us here - I'd like to know.

I would most likely use a JPanel for such a functionality.

I would set up a method to fill and one to empty and call them from the mouseEntered() and mouseExited() calls like

private JPanel myPanel();


private void fillMyPanel(){
      //add stuff here
      myPanel.add(...);
}
private void emptyMyPanel(){
      myPanel.removeAll();
}

public void mouseEntered(MouseEvent e){
     fillMyPanel();
}
public void mouseExited(MouseEvent e){
     emptyMyPanel();
}

Or you could embed two panels into eachother , keep the child filled with stuff you want to show, and set it to visible or not with the parent's mouseEntered & mouseExited. (although the mouseExited might need to be set to the child panel you'd have to test it out)

Yes.
@ajacintha: do you really mean hide the whole JFrame (including window borders, title bar etc) (see my answer) or just hide some things within a window that stays visible (see Philippe's answer)?

I have created a class which extends Jframe and implements MouseListner, and in mouseExited method used the setVisible(false) for the frame. But how to restore the frame when mouse enter's ,since the Jframe is invisible, i am not able to capture the mouse event once it exits.

I would most likely use a JPanel for such a functionality.

I would set up a method to fill and one to empty and call them from the mouseEntered() and mouseExited() calls like

private JPanel myPanel();


private void fillMyPanel(){
      //add stuff here
      myPanel.add(...);
}
private void emptyMyPanel(){
      myPanel.removeAll();
}

public void mouseEntered(MouseEvent e){
     fillMyPanel();
}
public void mouseExited(MouseEvent e){
     emptyMyPanel();
}

Or you could embed two panels into eachother , keep the child filled with stuff you want to show, and set it to visible or not with the parent's mouseEntered & mouseExited. (although the mouseExited might need to be set to the child panel you'd have to test it out)

I have created a frame without borders,title bar etc. I want a similar action that happens when a remote destop is opened in the system where the window disappears when the mouse is away and again its restored when the move is over it.

Yes.
@ajacintha: do you really mean hide the whole JFrame (including window borders, title bar etc) (see my answer) or just hide some things within a window that stays visible (see Philippe's answer)?

OK.I have done something like that, but I just faded the opacity way down to leave it as a "ghost" rather than hiding it completely. I don't know if you will get mouse events for a completely invisible JFrame. Maybe setting the opactity to some very small but non-zero value would work???

Im sorry I missunderstood what you wanted to do.

Im pretty sure it's impossible to get mouse events for anything that was setVisible to false.

James,
How to set the opacity for the Jframe. Is there any other way to make the Jframe visible when the mouse is over and invisible when the mouse is away from it. I am stuck with it.

OK.I have done something like that, but I just faded the opacity way down to leave it as a "ghost" rather than hiding it completely. I don't know if you will get mouse events for a completely invisible JFrame. Maybe setting the opactity to some very small but non-zero value would work???

Ok.Is there any other way to make the Jframe visible when the mouse is over and invisible when the mouse is away from it. I am stuck with it. I want a similar action like how the remote desktop window appears.

Im sorry I missunderstood what you wanted to do.

Im pretty sure it's impossible to get mouse events for anything that was setVisible to false.

You set the opacity with setOpacity(float alpha); which is new in Java 1.7

Like I said, hiding the window on mouse over is trivial. Getting ta mouse event when the window is completely hidden is a problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.