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.

Results 1 to 16 of 16

Thread: rtl (right to left) system tray popup menu items

  1. #1

    Default rtl (right to left) system tray popup menu items

    How I can arrange the menu items in the system tray rtl?

     
    TrayIcon trayIcon;
        SystemTray tray;
     
        Image getIcon() {
            return Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("pic/kabe.jpg"));
        }
     
        public void HideToSystemTray() {
    //        //System.out.println("creating instance");
            try {
    //            //System.out.println("setting look and feel");
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                //System.out.println("Unable to set LookAndFeel");
            }
            if (SystemTray.isSupported()) {
                //System.out.println("system tray supported");
                tray = SystemTray.getSystemTray();
     
    //            Image image = Toolkit.getDefaultToolkit().getImage("pic/kabe.jpg");
                MenuItem defaultItem = new MenuItem();
     
    //            defaultItem.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    //            defaultItem.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                defaultItem.setFont(new java.awt.Font("B Nazanin", 1, 14));
                PopupMenu popup = new PopupMenu();
    //            defaultItem.setComponentOrientation( ComponentOrientation.RIGHT_TO_LEFT );
    //            popup.setHorizontalAlignment(SwingConstants.RIGHT);
     
                defaultItem = new MenuItem("نمايش فرم");
                defaultItem.setFont(new java.awt.Font("B Nazanin", 1, 14));
                popup.add(defaultItem);
     
                defaultItem.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        setVisible(true);
                        setExtendedState(JFrame.NORMAL);
                    }
                });
     
                popup.addSeparator();
                defaultItem = new MenuItem("درباره ما");
                defaultItem.setFont(new java.awt.Font("B Nazanin", 1, 14));
                popup.add(defaultItem);
     
                defaultItem.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        Info info = new Info();
                        info.setVisible(true);
                    }
                });
     
                popup.addSeparator();
     
                ActionListener exitListener = new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        //System.out.println("Exiting....");
                        System.exit(0);
                    }
                };
                defaultItem = new MenuItem("خروج");
                defaultItem.setFont(new java.awt.Font("B Nazanin", 1, 14));
                popup.add(defaultItem);
                defaultItem.addActionListener(exitListener);
     
                popup.add(defaultItem);
     
                trayIcon = new TrayIcon(getIcon(), "azan", popup);
    //            trayIcon=new TrayIcon(image, "SystemTray Demo", popup);
                trayIcon.setImageAutoSize(true);
     
                trayIcon.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        setVisible(true);
                        setExtendedState(JFrame.NORMAL);
                    }
                });
     
            } else {
                //System.out.println("system tray not supported");
            }
    //        setIconImage(Toolkit.getDefaultToolkit().getImage("pic/kabe.jpg"));
     
            setVisible(true);
     
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //        trayIcon.displayMessage("Hello, World", "notification demo", MessageType.NONE);
        }
    Last edited by cnmeysam; March 30th, 2021 at 03:44 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: rtl (right to left) system tray popup menu items

    What happens now?
    If you don't understand my answer, don't ignore it, ask a question.

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

    cnmeysam (March 29th, 2021)

  4. #3

    Default Re: rtl (right to left) system tray popup menu items

    i can't set right to left MenuItem in system try or add icon for them
    Last edited by cnmeysam; March 29th, 2021 at 04:49 PM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: rtl (right to left) system tray popup menu items

    What happens now when the code is executed? How are icons positioned?

    How can the code be compiled and executed for testing? It is missing imports, class and main method.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5

    Default Re: rtl (right to left) system tray popup menu items

    This is all code, just change pic name to your picture (pic/kabe.jpg) and call HideToSystemTray(); in a button you to execute the code
    The code executes correctly but i can't find set icon and set to right to left command for MenuItem
    These are the required libraries
     
    import java.awt.Image;
    import java.awt.MenuItem;
    import java.awt.PopupMenu;
    import java.awt.SystemTray;
    import java.awt.Toolkit;
    import java.awt.TrayIcon;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.UIManager;

    and create new jframe named info
    and for formWindowStateChanged use this code
     
    if (evt.getNewState() == ICONIFIED) {
                try {
                    tray.add(trayIcon);
                    setVisible(false);
                    //System.out.println("added to SystemTray");
                } catch (AWTException ex) {
                    //System.out.println("unable to add to tray");
                }
            }
            if (evt.getNewState() == 7) {
                try {
                    tray.add(trayIcon);
                    setVisible(false);
                    //System.out.println("added to SystemTray");
                } catch (AWTException ex) {
                    //System.out.println("unable to add to system tray");
                }
            }
            if (evt.getNewState() == MAXIMIZED_BOTH) {
                tray.remove(trayIcon);
                setVisible(true);
                //System.out.println("Tray icon removed");
            }
            if (evt.getNewState() == NORMAL) {
                tray.remove(trayIcon);
                setVisible(true);
                //System.out.println("Tray icon removed");
            }

    after minimize form system tray is activated
    Last edited by cnmeysam; March 29th, 2021 at 08:04 PM.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: rtl (right to left) system tray popup menu items

    Sorry, that code does not compile for testing. Can you make a small, complete program that compiles, executes and shows the problem?

    I'll be Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

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

    cnmeysam (March 29th, 2021)

  9. #7

    Default Re: rtl (right to left) system tray popup menu items

    https://filebin.net/kjon07425x38shil...rar?t=vb0k2p2m

    this is compiled but menu items don't get right to left
    Last edited by cnmeysam; March 29th, 2021 at 10:09 PM.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: rtl (right to left) system tray popup menu items

    Please post the code here, not a link.
    If you don't understand my answer, don't ignore it, ask a question.

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

    cnmeysam (March 30th, 2021)

  12. #9

    Default Re: rtl (right to left) system tray popup menu items

     
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JFrame;
    import javax.swing.UIManager;
     
     
     
    public class HideToSystemTray extends JFrame{
        TrayIcon trayIcon;
        SystemTray tray;
        HideToSystemTray(){
            super("SystemTray test");
            System.out.println("creating instance");
            try{
                System.out.println("setting look and feel");
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            }catch(Exception e){
                System.out.println("Unable to set LookAndFeel");
            }
            if(SystemTray.isSupported()){
                System.out.println("system tray supported");
                tray=SystemTray.getSystemTray();
     
                Image image=Toolkit.getDefaultToolkit().getImage("pic/bulb.gif");
                ActionListener exitListener=new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Exiting....");
                        System.exit(0);
                    }
                };
                PopupMenu popup=new PopupMenu();
                MenuItem defaultItem=new MenuItem("خروج");
                defaultItem.addActionListener(exitListener);
                popup.add(defaultItem);
                popup.addSeparator();
                defaultItem=new MenuItem("نمایش");
                defaultItem.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        setVisible(true);
                        setExtendedState(JFrame.NORMAL);
                    }
                });
                popup.add(defaultItem);
                trayIcon = new TrayIcon(image, "tray icon", popup);
    //            trayIcon=new TrayIcon(image, "SystemTray Demo", popup);
                trayIcon.setImageAutoSize(true);
            }else{
                System.out.println("system tray not supported");
            }
            addWindowStateListener(new WindowStateListener() {
                public void windowStateChanged(WindowEvent e) {
                    if(e.getNewState()==ICONIFIED){
                        try {
                            tray.add(trayIcon);
                            setVisible(false);
                            System.out.println("added to SystemTray");
                        } catch (AWTException ex) {
                            System.out.println("unable to add to tray");
                        }
                    }
            if(e.getNewState()==7){
                        try{
                tray.add(trayIcon);
                setVisible(false);
                System.out.println("added to SystemTray");
                }catch(AWTException ex){
                System.out.println("unable to add to system tray");
            }
                }
            if(e.getNewState()==MAXIMIZED_BOTH){
                        tray.remove(trayIcon);
                        setVisible(true);
                        System.out.println("Tray icon removed");
                    }
                    if(e.getNewState()==NORMAL){
                        tray.remove(trayIcon);
                        setVisible(true);
                        System.out.println("Tray icon removed");
                    }
                }
            });
            setIconImage(Toolkit.getDefaultToolkit().getImage("bulb.gif"));
     
            setVisible(true);
            setSize(300, 200);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        public static void main(String[] args){
            new HideToSystemTray();
        }
    }

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: rtl (right to left) system tray popup menu items

    What do I need to do to test the code?
    It compiles and shows a window in the upper left corner of my screen.
    An image is shown on System Taskbar
    These messages are printed:

    creating instance
    setting look and feel
    system tray supported

    Now what?

    The code has no comments describing what it is supposed to do and how it is going to do it.
    If you don't understand my answer, don't ignore it, ask a question.

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

    cnmeysam (March 30th, 2021)

  15. #11

    Default Re: rtl (right to left) system tray popup menu items

    This code creates an icon in the system tray after minimizing and that icon has 2 menus
    I want the menus to be right-aligned and have an icon back to each menu
    But I did not find the code to do this

  16. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: rtl (right to left) system tray popup menu items

    I want the menus to be right-aligned and have an icon back to each menu
    What does "right-aligned" mean? Do you mean you want the menuitems arranged horizontally instead of vertically?
    Do you know of any menus that arrange their menuItems horizontally?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #13

    Default Re: rtl (right to left) system tray popup menu items

    In some languages, such as English, the text Start from left to right, but in some languages, such as Arabic or Persian, the text starts from right to left. For example in the text box we use this code
    textField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    or for jmenu we use this codes
    Menu1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    jMenu1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    but this codes not working in system tray
    like this picture
    https://webrubik.com/uploaded/produc...brubikcom.webp
    https://files.rtl-theme.com/products...8b6738b9bc.jpg
    Last edited by cnmeysam; March 30th, 2021 at 11:47 AM.

  18. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: rtl (right to left) system tray popup menu items

    Ok, are you asking how to right align text within a menuitem?
    Sorry, I have no ideas for that. Best ask somewhere that there are more international helpers that could have the knowledge for that.


    A work-around could be to add leading spaces to force the contents to the right.
    If you don't understand my answer, don't ignore it, ask a question.

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

    cnmeysam (March 30th, 2021)

  20. #15

    Default Re: rtl (right to left) system tray popup menu items

    i find solution
    use this libraris

    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;
    create a new project and new jframe an use this code inside source after
    public NewJFrame() {
            initComponents();   
        }
     final JPopupMenu popup = new JPopupMenu();
        TrayIcon ti;
     
        public void systemtray() {
     
            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.GREEN);
                    }
     
                    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) {
                        JFrame frame = new JFrame();
                        frame.setVisible(true);
    //                    setExtendedState(JFrame.NORMAL);
                        ti.displayMessage("Open form", "Frame is opened", MessageType.NONE);
                          popup.removeAll();
                          popup.setVisible(false);
                          setExtendedState(JFrame.NORMAL);
     
                    }
                });
     
                Info.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JFrame frameinfo = new JFrame();
                        frameinfo.setVisible(true);
    //                    setExtendedState(JFrame.NORMAL);
                    }
                });
     
                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;
     
        }



    use this codes in Jframe WindowStateChange

    if (evt.getNewState() == ICONIFIED) {
                //System.out.println("unable to add to tray");
     
                systemtray();
                setVisible(false);
            }
            if (evt.getNewState() == 7) {
                //System.out.println("unable to add to system tray");
     
                systemtray();
                setVisible(false);
            }
            if (evt.getNewState() == MAXIMIZED_BOTH) {
                SystemTray.getSystemTray().remove(ti);
                popup.removeAll();
                popup.setVisible(false);
                setVisible(true);
                //System.out.println("Tray icon removed");
            }
            if (evt.getNewState() == NORMAL) {
                SystemTray.getSystemTray().remove(ti);
                popup.removeAll();
                popup.setVisible(false);
                setVisible(true);
                //System.out.println("Tray icon removed");
            }
    call systemtray(); in main
    run project and after minimize NewJFrame tray icon activated
    Last edited by cnmeysam; April 1st, 2021 at 06:53 PM.

  21. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: rtl (right to left) system tray popup menu items

    Thank you for posting the solution.
    If you don't understand my answer, don't ignore it, ask a question.

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

    cnmeysam (March 30th, 2021)

Similar Threads

  1. Replies: 1
    Last Post: October 25th, 2020, 05:45 AM
  2. Permenant System Tray
    By keepStriving in forum Java Theory & Questions
    Replies: 9
    Last Post: December 9th, 2013, 08:54 PM
  3. System Tray
    By kaviri in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 7th, 2013, 01:49 PM
  4. system tray problems
    By laurence000 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 16th, 2013, 05:11 AM
  5. Is it possible to use card layout for menu and menu items?
    By jai2rul in forum AWT / Java Swing
    Replies: 0
    Last Post: April 11th, 2011, 08:19 AM

Tags for this Thread