Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [JAVA]jmenu non esegue eventi

    ciao a tutti. ho creato una Jmenubar con diversi jmenu e jmenuitem.
    aggiungendo eventi alle item questi non vengono eseguiti.
    allego qui il codice:
    codice:
    import it.bollette.mian.window.GestBolleJD;
    import it.bollette.mian.window.InsEntiJD;
    import it.bollette.mian.window.VisualizzaJD;
    
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.Point;
    import java.awt.Rectangle;
    
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    
    /**
     * @author marialaura
     * 
     */
    public class GenericMenu extends JMenuBar {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private JMenu insJM = null;
    	private JMenuItem enteJMI = null;
    	private JMenuItem bollJMI = null;
    	private JMenu visJM = null;
    	private JMenuItem allJMI = null;
    	private JMenuItem nonJMI = null;
    	private JMenuItem pagJMI = null;
    	private Frame frame = new Frame();
    
    	/**
    	 * 
    	 */
    	public GenericMenu(Frame frame) {
    		super();
    		this.frame = frame;
    		initialize();
    	}
    
    	/**
    	 * This method initializes this
    	 * 
    	 */
    	private void initialize() {
    		try {
    			this.setBounds(new Rectangle(0, 0, 590, 25));
    			this.setPreferredSize(new Dimension(590, 25));
    			this.setFont(new Font("Tahoma", Font.BOLD, 12));
    			this.add(getInsJM());
    			this.add(getVisJM());
    		} catch (java.lang.Throwable e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 * This method initializes insJM
    	 * 
    	 * @return javax.swing.JMenu
    	 */
    	private JMenu getInsJM() {
    		if (insJM == null) {
    			try {
    				insJM = new JMenu();
    				insJM.setFont(new Font("Tahoma", Font.BOLD, 14));
    				insJM.setLocation(new Point(0, 0));
    				insJM.setSize(new Dimension(140, 25));
    				insJM.setText("INSERISCI");
    				insJM.setPreferredSize(new Dimension(140, 25));
    				insJM.add(getEnteJMI());
    				insJM.add(getGetBollJMI());
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return insJM;
    	}
    
    	/**
    	 * This method initializes enteJMI
    	 * 
    	 * @return javax.swing.JMenuItem
    	 */
    	private JMenuItem getEnteJMI() {
    		if (enteJMI == null) {
    			try {
    				enteJMI = new JMenuItem();
    				enteJMI.setPreferredSize(new Dimension(140, 20));
    				enteJMI.setText("Nuovo Ente");
    				enteJMI.setFont(new Font("Tahoma", Font.BOLD, 12));
    				enteJMI.addMouseListener(new java.awt.event.MouseAdapter() {
    					public void mouseClicked(java.awt.event.MouseEvent e) {
    						new InsEntiJD(frame);
    					}
    				});
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return enteJMI;
    	}
    
    	/**
    	 * This method initializes bollJMI
    	 * 
    	 * @return javax.swing.JMenuItem
    	 */
    	private JMenuItem getGetBollJMI() {
    		if (bollJMI == null) {
    			try {
    				bollJMI = new JMenuItem();
    				bollJMI.setFont(new Font("Tahoma", Font.BOLD, 12));
    				bollJMI.setText("Gestione Bollette");
    				bollJMI.setPreferredSize(new Dimension(140, 20));
    				bollJMI.addMouseListener(new java.awt.event.MouseAdapter() {
    					public void mouseClicked(java.awt.event.MouseEvent e) {
    						new GestBolleJD(frame);
    					}
    				});
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return bollJMI;
    	}
    
    	/**
    	 * This method initializes insJM
    	 * 
    	 * @return javax.swing.JMenu
    	 */
    	private JMenu getVisJM() {
    		if (visJM == null) {
    			try {
    				visJM = new JMenu();
    				visJM.setFont(new Font("Tahoma", Font.BOLD, 14));
    				visJM.setLocation(new Point(0, 0));
    				visJM.setSize(new Dimension(140, 25));
    				visJM.setText("VISUALIZZA");
    				visJM.setPreferredSize(new Dimension(140, 25));
    				visJM.add(getAllJMI());
    				visJM.add(getNonJMI());
    				visJM.add(getPagJMI());
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return visJM;
    	}
    
    	/**
    	 * This method initializes enteJMI
    	 * 
    	 * @return javax.swing.JMenuItem
    	 */
    	private JMenuItem getAllJMI() {
    		if (allJMI == null) {
    			try {
    				allJMI = new JMenuItem();
    				allJMI.setPreferredSize(new Dimension(140, 20));
    				allJMI.setText("Tutti");
    				allJMI.setFont(new Font("Tahoma", Font.BOLD, 12));
    				allJMI.addMouseListener(new java.awt.event.MouseAdapter() {
    					public void mouseClicked(java.awt.event.MouseEvent e) {
    						new VisualizzaJD(frame, "");
    					}
    				});
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return allJMI;
    	}
    
    	/**
    	 * This method initializes bollJMI
    	 * 
    	 * @return javax.swing.JMenuItem
    	 */
    	private JMenuItem getNonJMI() {
    		if (nonJMI == null) {
    			try {
    				nonJMI = new JMenuItem();
    				nonJMI.setFont(new Font("Tahoma", Font.BOLD, 12));
    				nonJMI.setText("Non Pagate");
    				nonJMI.setPreferredSize(new Dimension(140, 20));
    				nonJMI.addMouseListener(new java.awt.event.MouseAdapter() {
    					public void mouseClicked(java.awt.event.MouseEvent e) {
    						new VisualizzaJD(frame, "NON");
    					}
    				});
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return nonJMI;
    	}
    
    	private JMenuItem getPagJMI() {
    		if (pagJMI == null) {
    			try {
    				pagJMI = new JMenuItem();
    				pagJMI.setFont(new Font("Tahoma", Font.BOLD, 12));
    				pagJMI.setText("Pagate");
    				pagJMI.setPreferredSize(new Dimension(140, 20));
    				pagJMI.addMouseListener(new java.awt.event.MouseAdapter() {
    					public void mouseClicked(java.awt.event.MouseEvent e) {
    						new VisualizzaJD(frame, "PAG");
    					}
    				});
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return pagJMI;
    	}
    }
    qualcuno di voi può aiutarmi o spiegarmi il motivo di questo comportamento anomalo?
    grazie mille

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Con i JMenuItem il modo più semplice e comodo per ricevere notifica della selezione è tramite un ActionListener (o una Action), non MouseListener.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3

    grazie mille

    grazie mille per il tuo aiuto.ora funziona correttamente.
    nel caso in cui qualcuno avesse il mio stesso problema posto il codice di un item:
    codice:
    private JMenuItem getAllJMI() {
    		if (allJMI == null) {
    			try {
    				allJMI = new JMenuItem();
    				allJMI.setPreferredSize(new Dimension(140, 20));
    				allJMI.setText("Tutti");
    				allJMI.setFont(new Font("Tahoma", Font.BOLD, 12));
    				allJMI.addActionListener(new java.awt.event.ActionListener() {
    					public void actionPerformed(java.awt.event.ActionEvent e) {
    						jd.dispose();
    						new VisualizzaJD(frame, "");
    					}
    				});
    			} catch (java.lang.Throwable e) {
    				e.printStackTrace();
    			}
    		}
    		return allJMI;
    	}

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.