Frage

I have a class to which I want to add a MouseListener as an external class. My MouseListener (MouseControl.java) is the following:

public class MouseControl implements MouseListener {

FileGUI fGUI = new FileGUI();

@Override
public void mouseClicked(MouseEvent e) {
    fGUI.input.setText(null);
    fGUI.input.grabfocus();
}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

}

However, when I add the MouseControl to the FileGUI class, I get a StackOverflow error in Eclipse.

    input.addMouseListener(new MouseControl());

"input" is a JTextField.

How can I correctly pass the necessary parameters, so when I perform a mouseClicked event, my "input" does the following?

War es hilfreich?

Lösung

I suspect this line FileGUI fGUI = new FileGUI(); in your MouseControl class.

I suspect in your FileGUI class's constructor you are trying to create new instance of MouseControl and attach as listener, again while creating new instance of MouseControl, MouseControl again tries to create instance of FileGUI and goes recursively until JVM throws a StackOverflowException

If this is the case I suggest you to modify constructor of MouseControl to receive an instance of FileGUI class and use that in MouseControl.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top