Robdll
Robdll

Reputation: 6253

how to fire MouseListener event methods if it's already running

In my code i Have:

A JFrame with a normal JPanel which contains some JComponent Object. These JComponents has: - a list of JTextFields. - a listener which highlight its border when mouseEntered fires, and hide it when mouseExited fires. Since the mouseExited method fires also when the cursor goes over a jtextField nested into the JComponent, the implementetion of the mouseExited method check if the cursor location of the event is outside the JComponentArea before hiding the border:

The problem is:

If you move the mouse slowly, the code seems to works fine, but if you move it fast between components, sometimes the Border of a JComponent is not hidden. I thought it could be because the mouseExited is fired when the cursor goes over a nested textField and then again when cursor is outside the JComponent, but i m not sure what s the real problem. Thanks in advance for any hint or help!!

The Code:

Code has been written on purpose of the problem described above. here is the Jcomponent class (MyContainer) and the class including the Main (MyClass):

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class MyClass {

    public static void main(String[] args)  {       
        SwingUtilities.invokeLater(new Runnable() {
            public void run(){   
                int i=0;
                //mainWindow
                    JFrame mainWindow = new JFrame("MyFrame");
                    mainWindow.setLayout(new BorderLayout());
                    mainWindow.setMinimumSize(new Dimension(300,300));

                    JPanel viewPort = new JPanel();
                    viewPort.setLayout(new FlowLayout(FlowLayout.LEFT));

                    MyContainer one = new MyContainer();
                    MyContainer two = new MyContainer();
                    MyContainer three = new MyContainer();

                    viewPort.add(one);
                    viewPort.add(two);
                    viewPort.add(three);
                    mainWindow.add(viewPort,BorderLayout.CENTER);
                    mainWindow.setVisible(true);
            }
        });
    }

}





import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JTextField;

    @SuppressWarnings("serial")
    public  class MyContainer extends JComponent {
            JTextField text1 ;
            JTextField text2 ;
            JTextField text3 ;

            public MyContainer(){
                super.setLayout(new FlowLayout(FlowLayout.LEFT));
                super.setPreferredSize(new Dimension(200,50));
                super.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
                super.setBackground(Color.green);

                text1 = new JTextField("LABEL_1");
                text1.setPreferredSize(new Dimension(60,30));
                text1.setEditable(false);
                this.add(text1);

                text2 = new JTextField("LABEL_2");
                text2.setPreferredSize(new Dimension(60,30));
                text2.setEditable(false);
                this.add(text2);

                text3 = new JTextField("LABEL_3");
                text3.setPreferredSize(new Dimension(60,30));
                text3.setEditable(false);
                this.add(text3);

                addListener();
            }

            private void addListener() {
                this.addMouseListener(new MouseListener() {
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        MyContainer.this.setBorder(BorderFactory.createLineBorder(Color.BLUE));
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        Rectangle r = e.getComponent().getBounds();
                        Point p = e.getPoint();
                        if( p.x < 0 || p.y < 0 || p.x >= r.width || p.y >= r.height)
                            MyContainer.this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
                    }

                    @Override public void mouseClicked(MouseEvent e) { /*NOTHING*/ }
                    @Override public void mouseReleased(MouseEvent e) { /*NOTHING*/ }
                    @Override public void mousePressed(MouseEvent e) { /*NOTHING*/ }
                });         
            }
        }

Upvotes: 1

Views: 236

Answers (2)

aterai
aterai

Reputation: 9808

Here is one possible implementation using MyContainer.this.dispatchEvent(SwingUtilities.convertMouseEvent(JTextField, e, MyContainer.this));:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MyClass2 {
  public static void main(String[] args)  {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        int i = 0;
        //mainWindow
        JFrame mainWindow = new JFrame("MyFrame");
        mainWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        mainWindow.setMinimumSize(new Dimension(300, 300));

        JPanel viewPort = new JPanel();
        viewPort.setLayout(new FlowLayout(FlowLayout.LEFT));

        MyContainer one = new MyContainer();
        MyContainer two = new MyContainer(true);
        viewPort.add(makeTitledPanel(one, "move it fast: NG?"));
        viewPort.add(makeTitledPanel(two, "move it fast: OK?"));
        mainWindow.add(viewPort, BorderLayout.CENTER);
        mainWindow.setVisible(true);
      }
    });
  }
  private static JComponent makeTitledPanel(JComponent c, String title) {
    JPanel p = new JPanel(new BorderLayout());
    p.setBorder(BorderFactory.createTitledBorder(title));
    p.add(c);
    return p;
  }
}

class MyContainer extends JComponent {
  JTextField text1 ;
  JTextField text2 ;
  JTextField text3 ;
  private final boolean flag;
  public MyContainer() {
    this(false);
  }
  public MyContainer(boolean flag) {
    super();
    this.flag = flag;

    super.setLayout(new FlowLayout(FlowLayout.LEFT));
    super.setPreferredSize(new Dimension(200, 50));
    super.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    super.setBackground(Color.green);

    text1 = new JTextField("LABEL_1");
    text2 = new JTextField("LABEL_2");
    text3 = new JTextField("LABEL_3");

    MouseListener l = new MouseEventConverter();
    for (JTextField f : Arrays.asList(text1, text2, text3)) {
      f.setPreferredSize(new Dimension(60, 30));
      f.setEditable(false);
      if (flag) {
        f.addMouseListener(l);
      }
      this.add(f);
    }
    this.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseEntered(MouseEvent e) {
        MyContainer.this.setBorder(BorderFactory.createLineBorder(Color.BLUE));
      }
      @Override
      public void mouseExited(MouseEvent e) {
        Rectangle r = e.getComponent().getBounds();
        Point p = e.getPoint();
        if (p.x < 0 || p.y < 0 || p.x >= r.width || p.y >= r.height) {
          MyContainer.this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
        }
      }
    });
  }
}

class MouseEventConverter extends MouseAdapter {
  @Override public void mouseEntered(MouseEvent e) {
    dispatchMouseEvent(e);
  }
  @Override public void mouseExited(MouseEvent e) {
    dispatchMouseEvent(e);
  }
  private void dispatchMouseEvent(MouseEvent e) {
    Component c = e.getComponent();
    Component p = SwingUtilities.getUnwrappedParent(c);
    p.dispatchEvent(SwingUtilities.convertMouseEvent(c, e, p));
  }
}

Upvotes: 3

Gorbles
Gorbles

Reputation: 1168

I recommend not using static methods, though I'm unsure of any actual impact of doing it your way. I'd recommend

public void mouseEntered(MouseEvent e) {
    ((MyContainer) e.getSource()).setBorder(...)
}

// and so forth

Additionally, the issue is probably with your mouseExited method. Instead of using .getBounds() I recommend using instanceof

if(e.getSource() instanceof MyContainer) {
    // do the stuff
} else {
    // do nothing
}

Alternatively:

if(!(e.getSource() instanceof JTextField)) {
    // do the stuff
} else {
    // do nothing
}

The else statement isn't even necessary; I'm just including it for completeness.

Upvotes: 0

Related Questions