Hey, I am trying to make a little MDI control with tabs, but my problems lies in the following:
I want it so that when the mouse is over a tab, a close button i.e. an X is displayed on the tab, then the user may click this button to close the corresponding tab. I have managed to get everything going except that when the mouseover event handler has been reached, the close button is displayed but when i try to move over the close button, the mouseexit event is triggered which then makes the close button invisible again. I want it so that when the mouse is over the tab the close button becomes visible and then the mouse can move over the still visible close button so it can be clicked.
I would like to keep it as simple as possible and only go into using the mouse coordinates and the coordinates of controls etc to do this is absolutely necessary. Below is a snippet of my current code:

Thanks in advance,

Jonathon.

// This code is part of the tab class i.e. "this" is referring to the tab itself, and closeButton is a JButton that is added to the tab.
	public void mouseEntered(MouseEvent e)
	{		
		if ( e.getSource() == this.closeButton || e.getSource() == this )
		{
			this.closeButton.setVisible( true );
		}
	}
	public void mouseExited(MouseEvent e)
	{		
		System.out.println(e.getComponent());		
		this.closeButton.setVisible( false );
	}

Could you just put a simple if-statement in your mouseExited method?

public void mouseExited(MouseEvent e)
{
  if (e.getSource() != this.closeButton)
  {
    // do your current code here
  }
}

Could you just put a simple if-statement in your mouseExited method?

public void mouseExited(MouseEvent e)
{
  if (e.getSource() != this.closeButton)
  {
    // do your current code here
  }
}

Either that or create a thread in your mouse-exited to wait a certain amount of seconds before making the close button invisible again.

Yeah I have tried that but it didn't seem to work either. The thread idea sounds cool but a little much work for such a simple task.

For the first idea the e.getSource() method on the mouseExited event handler still does not make a difference (I think this is because e.getSource() gets the component that it the mouse was over BEFORE it exits the component), I think it may be something to do with how I have implemented the MouseEvent handler. When i have set the handler I have done it like this..

addMouseListener(this);

so I am adding the listener to the current class that "implements MouseListener". Perhaps this is part of the problem, maybe it is only listening on the Tab button. maybe I could write the listener on another class or perhaps an inner class in order to do this, or wouldn't it make a difference?

Place the mouse listener code in your custom tab component and add the listener to that component.

class ButtonTabComponent extends JPanel implements MouseListener {

    private final JTabbedPane pane;
    private final JButton button;

    public ButtonTabComponent(final JTabbedPane pane) {
    ...
    addMouseListener(this);
    ...
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.