Results 1 to 9 of 9
  1. #1
    macwadu is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default JPopupMenu not hiding

    Hi there i need help in my code, jpopupmenu is not hiding and i dont how to do it

    Can anyone help me

    Java Code:
    package T;
    
    
    import java.awt.AWTException;
    import java.awt.Component;
    import java.awt.Font;
    import java.awt.Image;
    import java.awt.MenuItem;
    import java.awt.SystemTray;
    import java.awt.Toolkit;
    import java.awt.TrayIcon;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.lang.String;
    import java.net.MalformedURLException;
    import java.util.StringTokenizer;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.ImageIcon;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    
    import java.io.IOException;
    import java.io.FileOutputStream;
    import java.net.URL;
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    import javax.swing.JOptionPane;
    
    public class Test extends TimerTask{
    
    static Image image = Toolkit.getDefaultToolkit().getImage("src/T/Green.gif");
    static Image image1 = Toolkit.getDefaultToolkit().getImage("src/T/Red.gif");
    static Image image2 = Toolkit.getDefaultToolkit().getImage("src/T/Yellow.gif");
    
    static TrayIcon trayIcon;
    
    static JMenuItem top = new JMenuItem("MW",null);
    static  JPopupMenu popup = new JPopupMenu();
    static SystemTray tray = SystemTray.getSystemTray();
    static int aux1=0;
    
    static float f;
    
    static Font title = new Font("Lucida Console", Font.BOLD,  14);
    static Font font1 = new Font("Lucida Console", Font.BOLD,  12);
    
    static int aux =0;
    
    static FileOutputStream fos = null;
    
    java.net.URL t_url = this.getClass().getResource("src/T/Green.gif");
    
     static boolean isShowingPopup = false;
    
    
      public void run()
    {
            System.out.println(new Date() + "\007");
            System.gc();
    
            popup.removeAll();
            top.removeAll();
    
            try {
                System.out.println("d();");
                d();
            } catch (MalformedURLException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InterruptedException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
    
           menu();
    
            try {
                trayState();
    
            } catch (AWTException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
    
           aux = 1;
    }
    
    
      public static void main(String[] a) throws Exception {
            TimerTask toDo1=new Test();
            Timer clock1=new Timer();
            clock1.schedule(toDo1,0,15000);
    
            //createPopup();
    
             File f = new File("src/T/Green.gif");
             System.out.println(f.getCanonicalPath()+" "+f.exists());
        }
    
       public static void menu (){
             top.setFont(title);
            popup.add(top);
            popup.addSeparator();
    
            try {
    
                File file = new File("c:\\XML_TESTE.xml");
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(file);
                doc.getDocumentElement().normalize();
                //System.out.println("Root element " + doc.getDocumentElement().getNodeName());
                NodeList nodeLst = doc.getElementsByTagName("story");
                System.out.println("System Information");
    
                for (int s = 0; s < nodeLst.getLength(); s++) {
    
                    Node fstNode = nodeLst.item(s);
    
                    if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
    
                        Element fstElmnt = (Element) fstNode;
                        NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("title");
                        Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
                        NodeList fstNm = fstNmElmnt.getChildNodes();
                        //  System.out.println("Name : "  + ((Node) fstNm.item(0)).getNodeValue());
                        NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("topic");
                        Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
                        NodeList lstNm = lstNmElmnt.getChildNodes();
                        //  System.out.println("Per : " + ((Node) lstNm.item(0)).getNodeValue());
                        String b = ((Node) fstNm.item(0)).getNodeValue();
                        //System.out.println("length:" + b.length());
                        String removeSpaces = removeSpaces(b);
                        //System.out.println("length:" + removeSpaces.length());
    
                        String per =((Node) lstNm.item(0)).getNodeValue();
    
                        //convert to float
                        f = Float.valueOf(per.trim()).floatValue();
    
                        //convert to string
    
                        String d = removeSpaces+fillString(' ',7 - removeSpaces.length());
    
                        String c = new StringBuffer().append(d).append("- ").append(f).append("%").toString();
                        System.out.println(c);
    
                        if (f < 99.30){
                            JMenuItem item1= new JMenuItem(c, new ImageIcon("src/T/Red.gif"));
                            item1.setFont(font1);
                            popup.add(item1);
    
                        }else if(f > 99.99){
                            JMenuItem item1= new JMenuItem(c, new ImageIcon("src/T/Green.gif"));
                            item1.setFont(font1);
                            popup.add(item1);
                        }else{
                            JMenuItem item1= new JMenuItem(c, new ImageIcon("src/T/Yellow.gif"));
                            item1.setFont(font1);
                            popup.add(item1);
                        }
    
                    }
    
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
    
            popup.addSeparator();
    
            //item EXIT and action to exit
            JMenuItem miExit = new JMenuItem("Exit",new ImageIcon("src/T/exit.png"));
            ActionListener al = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Goodbye");
                    System.exit(0);
                }
            };
            miExit.addActionListener(al);
            miExit.setFont(font1);
            popup.add(miExit);
    
       }
    
        //change tray icon state
        public static void trayState() throws AWTException{
    
            if(aux > 0){
            tray.remove(trayIcon);
            }
             //trayIcon.setImageAutoSize(true);
    
            System.out.println(f);
    
           if (f < 99.30){
               trayIcon =  new TrayIcon(image1, "Tester2", null);
               tray.add(trayIcon);
            }else if(f > 99.97){
               trayIcon =  new TrayIcon(image, "Tester2", null);
               tray.add(trayIcon);
            }else{
                trayIcon =  new TrayIcon(image2, "Tester2", null);
               tray.add(trayIcon);
            }
    
            popup.pack();
     
             trayIcon.addMouseListener(new MouseAdapter() {
                public void mouseReleased(MouseEvent e) {
                   
                    if (e.isPopupTrigger() == true) {
    
                        popup.setLocation(e.getXOnScreen()+10,e.getYOnScreen()-170);
                        popup.setInvoker(popup);
                        popup.setVisible(true);
    
                    }else{
    
                       System.out.println("Else");
                       popup.setInvoker(null);
                       popup.setVisible(false);
    
                       JOptionPane.showMessageDialog(null,
                        "TrayIcon", "About",
                        JOptionPane.INFORMATION_MESSAGE);
    
    
                    }
                }
            });
    
        }
    
    
        public static String removeSpaces(String s){
            StringTokenizer st = new StringTokenizer(s," ",false);
            String t="";
            while (st.hasMoreElements()) t += st.nextElement();
            return t;
        }
    
    
        public static String fillString(char fillChar, int count){
            // creates a string of 'x' repeating characters
            char[] chars = new char[count];
            while (count>0) chars[--count] = fillChar;
            return new String(chars);
        }
    
    
        //download xml file
        public static void d() throws MalformedURLException, IOException, InterruptedException{
    
            try{
    
                 java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.net.URL("http://10.50.21.30:8080/rep/zab/XML_TESTE.xml").openStream());
    
                 java.io.FileOutputStream fos = new java.io.FileOutputStream("c:\\XML_TESTE.xml");
                 java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
                 byte[] data = new byte[1024];
                 int x=0;
                 while((x=in.read(data,0,1024))>=0)
                 {
                   bout.write(data,0,x);
                 }
                 bout.close();
                 in.close();
    
             } catch (IOException e) {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Connection Problem", "alert", JOptionPane.ERROR_MESSAGE);
                    System.exit(0);
                }
    
          }
    
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

  3. #3
    macwadu is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    But i use that, but my problem is that whem i click outside the JPopupMenu the doesnt hide.


    Java Code:
     trayIcon.addMouseListener(new MouseAdapter() {
                public void mouseReleased(MouseEvent e) {
                   
                    if (e.isPopupTrigger() == true) {
    
                        popup.setLocation(e.getXOnScreen()+10,e.getYOnScreen()-170);
                        popup.setInvoker(popup);
                        popup.setVisible(true);
    
                    }else{
    
                       System.out.println("Else");
                       popup.setInvoker(null);
                       popup.setVisible(false);
    
                       JOptionPane.showMessageDialog(null,
                        "TrayIcon", "About",
                        JOptionPane.INFORMATION_MESSAGE);
    
    
                    }
                }
            });
    
        }

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    It looks like you're adding/removing things from the pop-up possibly while it is showing. That's going to cause event handling problems (such as a popup that doesn't go away). What you want to do is make sure the menu is only modified when it is not showing.

  5. #5
    macwadu is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Ok but if i hide the popup when is actualized that problem dispears, right?

    However i have the same problem that when i click outisde the menu it doesnt desapear.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Depending upon the behavior you wish, you can hide the JPopupMenu based upon another event, for example MouseEvent.mouseExited.

  7. #7
    macwadu is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Can you give a example of that because i tried and the mouseExited have a problem with popup.addSeparator(); it give distinct events for the before the addseparator and after the separator.

  8. #8
    bilTunDev is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default

    hi there
    macwadu , i wonder if you have found a solution; i am having the same probleme here
    mouseExited have a problem with addSeparator()
    and it's not even a good behavior to hide the popup menu when it exit;
    i really prefer to hide it only when user click outside the popup menu area
    so if you can help me you are welcome !

  9. #9
    macwadu is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    I can solve the problem, and dont have any idea how can it be done, i think this is a bug of the JPopupMenu because in popupmenu it works fine but its not possible to add a icon to the menu.

Similar Threads

  1. JTextField in a JPopupMenu
    By baran_khan in forum AWT / Swing
    Replies: 6
    Last Post: 05-02-2009, 09:43 PM
  2. JMenu to JPopupMenu
    By carderne in forum New To Java
    Replies: 0
    Last Post: 02-28-2009, 06:07 PM
  3. JPopupMenu
    By hungleon88 in forum NetBeans
    Replies: 8
    Last Post: 01-15-2009, 05:00 PM
  4. JPopupMenu problem
    By sylvievdb in forum AWT / Swing
    Replies: 3
    Last Post: 10-06-2008, 08:37 AM
  5. dynamising the height of a JPopupMenu
    By iimasd in forum AWT / Swing
    Replies: 6
    Last Post: 11-21-2007, 10:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •