Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 42 of 42

Thread: close JPopupMenu after focus lost

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,071
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: close JPopupMenu after focus lost

    not working
    Please explain what that means?

    When I compile and execute, it shows the trayicon. Then what do I need to do to see the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27

    Default Re: close JPopupMenu after focus lost

    I can't found the reason
    pleas help
    Last edited by cnmeysam; April 3rd, 2021 at 07:43 PM.

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,071
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: close JPopupMenu after focus lost

    Sorry, I do not know what the problem is. Can you explain?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29

    Default Re: close JPopupMenu after focus lost

    It works well in your class when I use mouse input and exit in your code to change the color of the menus, but it does not work when I use the codes in graphically
    GUI in the form design menu. I have sent you my codes to try in previous posts
    https://www.javaprogrammingforums.co...tml#post171238
    this part of code don't work in gui (create new project-->create new jframe form-->use your code inside of them)
     
            Info.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    Info.setBackground(Color.GREEN);
                }
     
                public void mouseExited(java.awt.event.MouseEvent evt) {
                    Info.setBackground(UIManager.getColor("control"));
                }
            });
            Exit.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    Exit.setBackground(Color.RED);
                }
     
                public void mouseExited(java.awt.event.MouseEvent evt) {
                    Exit.setBackground(UIManager.getColor("control"));
                }
            });
     
            Open.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    Open.setBackground(Color.GREEN);
                }
     
                public void mouseExited(java.awt.event.MouseEvent evt) {
                    Open.setBackground(UIManager.getColor("control"));
                }
            });
    Last edited by cnmeysam; April 3rd, 2021 at 07:03 PM.

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,071
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: close JPopupMenu after focus lost

    the color of the menus
    Are you able to set the color of the menus at any time? Not just in a listener.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31

    Default Re: close JPopupMenu after focus lost

    I just want the color to change when the mouse hovers over one, and the color to return to normal when it hovers over it and goes to the next menu.
    It works just like when I used this code in the class you created
    It does not matter how it is
    when i use in gui menus color It does not work in any way

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,071
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: close JPopupMenu after focus lost

    when i use in gui menus color It does not work in any way
    Sorry, I do not know anything about the gui menus.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33

    Default Re: close JPopupMenu after focus lost

    it is not about gui menu it is about gui form when i use your code in gui form menu color in system tray dont work

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,071
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: close JPopupMenu after focus lost

    You need to make and post a small, complete program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35

    Default Re: close JPopupMenu after focus lost

    i send small, complete program before
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.imageio.ImageIO;
     
    public class trayform extends javax.swing.JFrame {
     
        private static trayform obj = null;
     
        private trayform() {
            initComponents();
     
            JPopupMenu popup = new JPopupMenu();
            loadPopup(popup);
            setTI();
        }
     
        public static trayform getobj() {
            if (obj == null) {
                obj = new trayform();
            }
            return obj;
        }
     
        public trayform(JPopupMenu popup, int x, int y) {
    //      Window win = new Window(new Frame()); //null);
            JDialog win = new JDialog(new Frame()); //null);
            win.setUndecorated(true);
     
            JMenuBar mnBar = new JMenuBar();           //Works
            final JMenu men = new JMenu("Test");      //<< Shows Test and a Right caret
            JMenuItem mi1 = new JMenuItem("one");      //
            men.add(mi1);
            mnBar.add(men);
            win.setLayout(new BorderLayout());
    //      win.add(mnBar, BorderLayout.NORTH);
     
            win.setBounds(x, y, 100, 100);
            win.setOpacity(0.05f);
    //      win.setBackground(
            win.setVisible(true);
     
            win.setFocusable(true);
    //      loadPopup(popup);
     
            win.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    System.out.println("mC at " + e.getPoint());                     // WORKS
                    //  Place and show popup
                    popup.setLocation(e.getPoint());
                    popup.setVisible(true);
                    if (e.getPoint().y > 90) {
                        System.exit(0);
                    }
                }
            });
     
            mnBar.addFocusListener(new FocusAdapter() {
                @Override
                public void focusLost(FocusEvent evt) {
                    System.out.println("focusLost");         //<<<< never 
                    popup.setVisible(false);
                }
     
                @Override
                public void focusGained(FocusEvent evt) {
                    System.out.println("focusGained");       //  ditto
                }
            });
            win.addWindowFocusListener(new WindowFocusListener() {
                @Override
                public void windowLostFocus(WindowEvent evt) {
                    System.out.println("WfocusLost");         //<<<< THIS ONE 
                    popup.setVisible(false);
                }
     
                @Override
                public void windowGainedFocus(WindowEvent evt) {
                    System.out.println("WfocusGained");       //  ditto
                }
            });
            win.requestFocus();                 // nothing
            men.requestFocusInWindow();         // nothing
     
            System.out.println("isFocusable()=" + win.isFocusable()); // isFocusable()=true
     
        }
     
        //----------------------------------------------
        TrayIcon ti;
     
        public void setTI() {
            try {
                ti = new TrayIcon(ImageIO.read(getClass().getResource("/images/kabe.jpg")), "Have a nice day");
                ti.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        System.out.println("mC for ti at " + e.getPoint());
                        //  What x,y for the popup????
                        int x = e.getPoint().x;
                        int y = e.getPoint().y;
     
                        // Create our own local popup to respond to TrayIcon click
                        JPopupMenu popup = new JPopupMenu();
                        new trayform(popup, x - 20, y - 20);
                        loadPopup(popup);
                        popup.setLocation(x - 12, y - 95);
                        popup.setVisible(true);
     
                    }
                });
                SystemTray.getSystemTray().add(ti);      //<<<<<<<
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
     
        //--------------------------------------------------------------------------
        public void loadPopup(JPopupMenu popup) {
            JMenuItem Exit = new JMenuItem("خروج");
            Exit.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N
            Exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); //exit.png"))); // NOI18N
            JMenuItem Open = new JMenuItem("نمایش");
            Open.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N
            Open.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/kabe.jpg"))); //kabe.jpg"))); // NOI18N
            JMenuItem Info = new JMenuItem("درباره ما");
            Info.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N
            Info.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/info.png"))); //info.png"))); // NOI18N
     
     
    //this part in gui not working
     
            Info.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    Info.setBackground(Color.GREEN);
                }
     
                public void mouseExited(java.awt.event.MouseEvent evt) {
                    Info.setBackground(UIManager.getColor("control"));
                }
            });
            Exit.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    Exit.setBackground(Color.RED);
                }
     
                public void mouseExited(java.awt.event.MouseEvent evt) {
                    Exit.setBackground(UIManager.getColor("control"));
                }
            });
     
            Open.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    Open.setBackground(Color.GREEN);
                }
     
                public void mouseExited(java.awt.event.MouseEvent evt) {
                    Open.setBackground(UIManager.getColor("control"));
                }
            });
     //this part in gui not working
     
     
     
            Exit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
     
            Open.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
    //                JFrame frame = new JFrame();
    //                frame.
                    setVisible(true);
                    popup.setVisible(false);
    //                frame.
                    setExtendedState(JFrame.NORMAL);
     
                }
            });
     
            Info.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ti.displayMessage("Open form", "Frame is opened", TrayIcon.MessageType.NONE);
                }
            });
     
            popup.add(Open);
            popup.addSeparator();
            popup.add(Info);
            popup.addSeparator();
            popup.add(Exit);
     
    //            popup.setComponentOrientation(ComponentOrientation  .RIGHT_TO_LEFT);
            popup.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            popup.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N
     
        }  // end loadPopup
     
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            addWindowStateListener(new java.awt.event.WindowStateListener() {
                public void windowStateChanged(java.awt.event.WindowEvent evt) {
                    formWindowStateChanged(evt);
                }
            });
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 300, Short.MAX_VALUE)
            );
     
            setSize(new java.awt.Dimension(416, 339));
            setLocationRelativeTo(null);
        }// </editor-fold>                        
     
        private void formWindowStateChanged(java.awt.event.WindowEvent evt) {                                        
            // TODO add your handling code here:
            if (evt.getNewState() == ICONIFIED) {
                //System.out.println("unable to add to tray");
                setVisible(false);
            }
            if (evt.getNewState() == 7) {
                //System.out.println("unable to add to system tray");
     
                setVisible(false);
            }
            if (evt.getNewState() == MAXIMIZED_BOTH) {
     
                setVisible(true);
                //System.out.println("Tray icon removed");
            }
            if (evt.getNewState() == NORMAL) {
     
                setVisible(true);
                //System.out.println("Tray icon removed");
            }
        }                                       
     
        public static void main(String args[]) {
     
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(trayform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(trayform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(trayform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(trayform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
     
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new trayform().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify                     
        // End of variables declaration                   
    }
    Last edited by cnmeysam; April 3rd, 2021 at 07:47 PM.

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,071
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: close JPopupMenu after focus lost

    Are you able to set the original color of any of the menuitems BEFORE there is any mouse movement?
    Try setting the color immediately after the call to setIcon.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37

    Default Re: close JPopupMenu after focus lost

    if you use code like this newer can add color to menu
    For example in netbeans ( create new project--> create new jframe form and use code like this
    go to source and use this inputs first
     
     
    import java.awt.Color;
    import java.awt.ComponentOrientation;
    import java.awt.EventQueue;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.Insets;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.SystemTray;
    import java.awt.Toolkit;
    import java.awt.TrayIcon;
    import java.awt.TrayIcon.MessageType;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.ArrayList;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.UIManager;
    insert this codes
    private static trayform obj = null;
     
        private trayform() {
            initComponents();
     
            JPopupMenu popup = new JPopupMenu();
            loadPopup(popup);
            setTI();
     
        }
     
        public static trayform getobj() {
            if (obj == null) {
                obj = new trayform();
            }
            return obj;
        }

    insert this codes after that ( you'r class code )
     
    final JPopupMenu popup = new JPopupMenu();
        TrayIcon ti;
     
        public void systemtray() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception ex) {
            }
            try {
                ti = new TrayIcon(ImageIO.read(getClass().getResource("/images/kabe.jpg")), "Have a nice day");
     
                JMenuItem Exit = new JMenuItem("خروج");
                Exit.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N
                Exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); // NOI18N
                JMenuItem Open = new JMenuItem("نمایش");
                Open.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N
                Open.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/kabe.jpg"))); // NOI18N
                JMenuItem Info = new JMenuItem("درباره ما");
                Info.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N
                Info.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/info.png"))); // NOI18N
     
                Info.addMouseListener(new java.awt.event.MouseAdapter() {
                    public void mouseEntered(java.awt.event.MouseEvent evt) {
                        Info.setBackground(Color.GREEN);
                    }
     
                    public void mouseExited(java.awt.event.MouseEvent evt) {
                        Info.setBackground(UIManager.getColor("control"));
                    }
                });
                Exit.addMouseListener(new java.awt.event.MouseAdapter() {
                    public void mouseEntered(java.awt.event.MouseEvent evt) {
                        Exit.setBackground(Color.RED);
                    }
     
                    public void mouseExited(java.awt.event.MouseEvent evt) {
                        Exit.setBackground(UIManager.getColor("control"));
                    }
                });
     
                Open.addMouseListener(new java.awt.event.MouseAdapter() {
                    public void mouseEntered(java.awt.event.MouseEvent evt) {
                        Open.setBackground(Color.GREEN);
                    }
     
                    public void mouseExited(java.awt.event.MouseEvent evt) {
                        Open.setBackground(UIManager.getColor("control"));
                    }
                });
     
                Exit.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        SystemTray.getSystemTray().remove(ti);
                        System.exit(0);
                    }
                });
     
                Open.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        setVisible(true);
    //                    setExtendedState(JFrame.NORMAL);
     
                        popup.removeAll();
                        popup.setVisible(false);
                        setExtendedState(JFrame.NORMAL);
     
                    }
                });
     
                Info.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        ti.displayMessage("Open form", "Frame is opened", MessageType.NONE);
                    }
                });
     
                popup.add(Open);
                popup.addSeparator();
                popup.add(Info);
                popup.addSeparator();
                popup.add(Exit);
     
    //            popup.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                popup.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                popup.setFont(new java.awt.Font("B Nazanin", 1, 18)); // NOI18N
     
                Open.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        setVisible(true);
                        setExtendedState(JFrame.NORMAL);
                    }
                });
     
                Exit.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.exit(0);
                    }
                });
     
                ti.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 1) {
                            Rectangle bounds = getSafeScreenBounds(e.getPoint());
                            Point point = e.getPoint();
                            int x = point.x;
                            int y = point.y;
                            if (y < bounds.y) {
                                y = bounds.y;
                            } else if (y > bounds.y + bounds.height) {
                                y = bounds.y + bounds.height;
                            }
                            if (x < bounds.x) {
                                x = bounds.x;
                            } else if (x > bounds.x + bounds.width) {
                                x = bounds.x + bounds.width;
                            }
                            if (x + popup.getPreferredSize().width > bounds.x + bounds.width) {
                                x = (bounds.x + bounds.width) - popup.getPreferredSize().width;
                            }
                            if (y + popup.getPreferredSize().height > bounds.y + bounds.height) {
                                y = (bounds.y + bounds.height) - popup.getPreferredSize().height;
                            }
                            popup.setLocation(x, y);
                            if (popup.isVisible()) {
                                popup.setVisible(false);
                                System.out.println("isVisible");
                            } else {
                                popup.setVisible(true);
                            }
                        }
                    }
                });
                SystemTray.getSystemTray().add(ti);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
     
        public static Rectangle getSafeScreenBounds(Point pos) {
     
            Rectangle bounds = getScreenBoundsAt(pos);
            Insets insets = getScreenInsetsAt(pos);
     
            bounds.x += insets.left;
            bounds.y += insets.top;
            bounds.width -= (insets.left + insets.right);
            bounds.height -= (insets.top + insets.bottom);
     
            return bounds;
     
        }
     
        public static Insets getScreenInsetsAt(Point pos) {
            GraphicsDevice gd = getGraphicsDeviceAt(pos);
            Insets insets = null;
            if (gd != null) {
                insets = Toolkit.getDefaultToolkit().getScreenInsets(gd.getDefaultConfiguration());
            }
            return insets;
        }
     
        public static Rectangle getScreenBoundsAt(Point pos) {
            GraphicsDevice gd = getGraphicsDeviceAt(pos);
            Rectangle bounds = null;
            if (gd != null) {
                bounds = gd.getDefaultConfiguration().getBounds();
            }
            return bounds;
        }
     
        public static GraphicsDevice getGraphicsDeviceAt(Point pos) {
     
            GraphicsDevice device = null;
     
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice lstGDs[] = ge.getScreenDevices();
     
            ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);
     
            for (GraphicsDevice gd : lstGDs) {
     
                GraphicsConfiguration gc = gd.getDefaultConfiguration();
                Rectangle screenBounds = gc.getBounds();
     
                if (screenBounds.contains(pos)) {
     
                    lstDevices.add(gd);
     
                }
     
            }
     
            if (lstDevices.size() > 0) {
                device = lstDevices.get(0);
            } else {
                device = ge.getDefaultScreenDevice();
            }
     
            return device;
     
        }

    is not working until remove this part from main
     
    try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(OqatSharee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(OqatSharee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(OqatSharee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(OqatSharee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }

    but if dont use this part

     
    private static trayform obj = null;
        public static trayform getobj() {
            if (obj == null) {
                obj = new trayform();
            }
            return obj;
        }

    after and before
     
    public NewJFrame1() {
            initComponents();
     
        }

    never work menu color
    Is there no way it can work menu color properly without clearing the mine codes? Because then the appearance of the program will be spoiled

  13. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,071
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: close JPopupMenu after focus lost

    I can not do anything with all those little pieces of code.
    Did you see my question? What happens when you do this:
    Are you able to set the original color of any of the menuitems BEFORE there is any mouse movement?
    Try setting the color immediately after the call to setIcon.


    working until remove this part from main
    Then that is your solution.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39

    Default Re: close JPopupMenu after focus lost

    in gui never menu item color change Nowhere and in no way
    The program works properly when a new class is created and the code inside the graphic form is placed in that class and the mine section is edited.
    But when the same codes are used in the body of gui form, no change even with the change of the main part, the colors do not work for the menus, even if you write the command to change the color of the menus without conditions and intermediaries. directly

  15. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,071
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: close JPopupMenu after focus lost

    What happened when you tried what I suggested?

    Have you read the API documentation for the setBackground method to see if there is anything there that could help you?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #41

    Default Re: close JPopupMenu after focus lost

    I found solution in gui i must first setOpaque(true) and use like this
    Info.setOpaque(true);
    Open.setOpaque(true);
    Exit.setOpaque(true);
    Last edited by cnmeysam; April 4th, 2021 at 12:26 AM.

  17. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,071
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: close JPopupMenu after focus lost

    Glad you found the solution.
    If you don't understand my answer, don't ignore it, ask a question.

  18. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (April 3rd, 2021)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. how can i set an object in focus
    By ankushpruthi in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 1st, 2013, 07:01 AM
  2. Replies: 1
    Last Post: June 20th, 2013, 01:45 PM
  3. Focus is in the wrong spot
    By walker6o9 in forum AWT / Java Swing
    Replies: 0
    Last Post: January 15th, 2013, 11:51 PM
  4. [SOLVED] KeyListeners Require Focus...
    By snowguy13 in forum AWT / Java Swing
    Replies: 2
    Last Post: January 6th, 2012, 08:15 AM
  5. KeyListeners: Automatic Focus?
    By bgroenks96 in forum Java Theory & Questions
    Replies: 32
    Last Post: June 24th, 2011, 09:03 PM

Tags for this Thread