Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't MouseListener work?

Here is my main class:

import javax.swing.*;

public class WordProcessor {

    public static void main(String[] args) {           
        MainFrame frame = new MainFrame("Word Processor", 10000, 10000);
    }
} 

and i have two other classes

import javax.swing.*;

public class MainFrame extends JFrame {

    JMenuBar menubar = new JMenuBar();            

    public MainFrame(String name, int x, int y) {
        setTitle(name);
        setSize(x, y);
        setVisible(true);
        setJMenuBar(menubar); 
        //creates file menu and adds to menubar
        //TODO populate with JMenuItems 
        JMenu filemenu = new JMenu("file");
        filemenu.setVisible(true);
        menubar.add(filemenu);

        buttonnew buttonnew = new buttonnew("new");
        buttonnew.setVisible(true);
        filemenu.add(buttonnew);
        buttonnew.addMouseListener(buttonnew);
    }
}

and lastly

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

public class buttonnew extends JMenuItem implements MouseListener{

    buttonnew(String s) {
          super();
          super.setText(s);        
    }

    public void mouseClicked(MouseEvent e){         
          System.out.println("hey-o");
    } 

    @Override
    public void mouseExited(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }        
}

Nothing happens when i click on buttonneẅ. I'm so lost!

like image 867
user2465510 Avatar asked Jan 21 '26 18:01

user2465510


2 Answers

Solutions:

  1. Don't use MouseListeners with JMenuItems! They're supposed to use ActionListeners.
  2. Read the tutorials when using a new tool. The Swing menu tutorial would already tell you all of this and how to properly use menus.
  3. Also, it is better to not have your GUI classes implement your listener interface as you're forcing one class to play too many rolls breaking the Cohesion OOP rule.
like image 139
Hovercraft Full Of Eels Avatar answered Jan 23 '26 08:01

Hovercraft Full Of Eels


Read the Swing tutorial on How to Use Menu Items. You should not be using a MouseListener. You should be adding an ActionListener to the menu item.

The tutorial also has section on How to Write an Action Listener and How to Write a Mouse Listener.

public class buttonnew extends JMenuItem implements MouseListener{

Also class name should start with an upper case character, not a lower case character.

buttonnew.setVisible(true);

Swing components (except top level windows) are visible by default to the above code is unnecessary.

MainFrame frame = new MainFrame("Word Processor", 10000, 10000);

Don't hardcode a size for a frame. My screen is only 1376 x 768. You should either use:

frame.pack();

or for full screen you can use:

frame.setExtendedState(...);

Don't make the frame visible until you have added all the compnents to the frame.

setTitle(name);
setSize(x, y);
setVisible(true);
setJMenuBar(menubar);
like image 45
camickr Avatar answered Jan 23 '26 07:01

camickr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!