Results 1 to 3 of 3
  1. #1
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Wink GUI components not display

    Hi all,

    I've create an application that open a dialog on tray icon click. Tray icon is ok now and I try to add a dialog with some controls. Actually two buttons are there right now. Here is the full code I have try.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import java.awt.Toolkit;
    
    public class Main extends JFrame 
    {
        protected JButton buttonOne, buttonTwo;
        
        public Main()
        {
            
           buttonOne = new JButton("Ok");
           buttonOne.setVerticalTextPosition(AbstractButton.CENTER);
           buttonOne.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
           //buttonOne.setMnemonic(KeyEvent.VK_D);
           //buttonOne.setActionCommand("disable");
            
           buttonTwo = new JButton("Exit");
           buttonTwo.setVerticalTextPosition(AbstractButton.BOTTOM);
           buttonTwo.setHorizontalTextPosition(AbstractButton.CENTER);
          // buttonTwo.setMnemonic(KeyEvent.VK_M);
            
        // Set the actions
            buttonOne.addActionListener(this);
            //buttonTwo.addActionListener(this);
            
        // Add components to the container
            add(buttonOne);
            add(buttonTwo);
            final TrayIcon trayIcon;
    
            if (SystemTray.isSupported()) 
            {
                SystemTray tray = SystemTray.getSystemTray();
                Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");
    
                MouseListener mouseListener = new MouseListener() 
                {
                    public void mouseClicked(MouseEvent e) 
                    {
                        System.out.println("Tray Icon - Mouse clicked!");                 
                    }
                    public void mouseEntered(MouseEvent e) 
                    {
                        System.out.println("Tray Icon - Mouse entered!");                 
                    }
                    public void mouseExited(MouseEvent e) 
                    {
                        System.out.println("Tray Icon - Mouse exited!");                 
                    }
                    public void mousePressed(MouseEvent e) 
                    {
                        System.out.println("Tray Icon - Mouse pressed!");                 
                    }
                    public void mouseReleased(MouseEvent e) 
                    {
                        System.out.println("Tray Icon - Mouse released!");                 
                    }
                };
    
                ActionListener exitListener = new ActionListener() 
                {
                    public void actionPerformed(ActionEvent e) 
                    {
                        System.out.println("Exiting...");
                        System.exit(0);
                    }
                };
                
                PopupMenu popup = new PopupMenu();
                MenuItem defaultItem = new MenuItem("Exit");
                defaultItem.addActionListener(exitListener);
                popup.add(defaultItem);
    
                trayIcon = new TrayIcon(image, "Tray Demo", popup);
    
                ActionListener actionListener = new ActionListener() 
                {
                    public void actionPerformed(ActionEvent e) 
                    {
                        trayIcon.displayMessage("Action Event", 
                            "An Action Event Has Been Peformed!",
                            TrayIcon.MessageType.INFO);
                    }
                };
                
                trayIcon.setImageAutoSize(true);
                trayIcon.addActionListener(actionListener);
                trayIcon.addMouseListener(mouseListener);
    
                    try 
                    {
                         tray.add(trayIcon);
                    } 
                    catch (AWTException e) 
                    {
                        System.err.println("TrayIcon could not be added.");
                    }
    
            } 
            else 
            {
                System.err.println("System tray is currently not supported.");
            }
        }
        
        public static void createAndShowGUI()
        {
            JFrame frame = new JFrame("Main Frame!");
            frame.setSize(new Dimension(300, 300));
            
           frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
           int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
           int frameHeight = frame.getHeight();
           int taskbarHeight = screenHeight - frameHeight;
           
           System.out.println(screenHeight);
           System.out.println(frameHeight);
           System.out.println(taskbarHeight);
           
            Main newContentPane = new Main();
            //newContentPane.setOpaque(true); // Opaque
            frame.setContentPane(newContentPane);
            
            frame.pack();
            frame.setVisible(true);
            
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }    
        
        public static void main(String[] args)
        {
            Main main = new Main();
        }     
    }
    Can you guys tell me why the dialog is not displayed. I'm weak on AWT/Swing implements.

    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default SystemTray

    You can get some images to use from this reference page Steal Our Code.
    Under the header/link
    "Learning to Use the System Tray API in Java SE 6"
    click on the link:
    "Download an Executable JAR File (Source Code Included)"
    You can extract the three gif images from the jar file.
    See Using JAR Files: The Basics if you want more info about jar files.
    You app works okay now.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import java.awt.Toolkit;
    
    public class SysTray implements ActionListener
    {
        protected JButton buttonOne, buttonTwo;
    
        public void actionPerformed(ActionEvent e)
        {
            JButton button = (JButton)e.getSource();
            String ac = button.getActionCommand();
    
            // Another way to identify source component:
            if(ac.equals("disable"))
                System.out.println("ActionEvent set by buttonOne");
    
            if(button == buttonOne)
                System.out.println("Ok");
            if(button == buttonTwo)
                System.exit(0);
        }
        
        private Box getContent()
        {
            buttonOne = new JButton("Ok");
            buttonOne.setVerticalTextPosition(AbstractButton.CENTER);
            //aka LEFT, for left-to-right locales
            buttonOne.setHorizontalTextPosition(AbstractButton.LEADING);
            //buttonOne.setMnemonic(KeyEvent.VK_D);
            buttonOne.setActionCommand("disable");
            
            buttonTwo = new JButton("Exit");
            buttonTwo.setVerticalTextPosition(AbstractButton.BOTTOM);
            buttonTwo.setHorizontalTextPosition(AbstractButton.CENTER);
            // buttonTwo.setMnemonic(KeyEvent.VK_M);
            
            // Set the actions
            buttonOne.addActionListener(this);
            buttonTwo.addActionListener(this);
    
            Box box = Box.createHorizontalBox();
            // Add components to the container
            box.add(Box.createHorizontalGlue());
            box.add(buttonOne);
            box.add(Box.createHorizontalGlue());
            box.add(buttonTwo);
            box.add(Box.createHorizontalGlue());
            return box;
        }
    
        private void initSystemTray()
        {
            final TrayIcon trayIcon;
    
            if (SystemTray.isSupported()) 
            {
                SystemTray tray = SystemTray.getSystemTray();
                Image image = Toolkit.getDefaultToolkit()//.getImage("tray.gif");
                                     .createImage("images/sysTray/logo.gif");
    
                MouseListener mouseListener = new MouseListener()
                {
                    public void mouseClicked(MouseEvent e)
                    {
                        System.out.println("Tray Icon - Mouse clicked!");
                    }
                    public void mouseEntered(MouseEvent e)
                    {
                        System.out.println("Tray Icon - Mouse entered!");
                    }
                    public void mouseExited(MouseEvent e)
                    {
                        System.out.println("Tray Icon - Mouse exited!");
                    }
                    public void mousePressed(MouseEvent e)
                    {
                        System.out.println("Tray Icon - Mouse pressed!");   
                    }
                    public void mouseReleased(MouseEvent e) 
                    {
                        System.out.println("Tray Icon - Mouse released!");    
                    }
                };
    
                ActionListener exitListener = new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        System.out.println("Exiting...");
                        System.exit(0);
                    }
                };
    
                PopupMenu popup = new PopupMenu();
                MenuItem defaultItem = new MenuItem("Exit");
                defaultItem.addActionListener(exitListener);
                popup.add(defaultItem);
    
                trayIcon = new TrayIcon(image, "Tray Demo", popup);
    
                ActionListener actionListener = new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        trayIcon.displayMessage("Action Event",
                            "An Action Event Has Been Peformed!",
                            TrayIcon.MessageType.INFO);
                    }
                };
    
                trayIcon.setImageAutoSize(true);
                trayIcon.addActionListener(actionListener);
                trayIcon.addMouseListener(mouseListener);
    
                try
                {
                    tray.add(trayIcon);
                } 
                catch (AWTException e)
                {
                    System.err.println("TrayIcon could not be added.");
                }
            }
            else
            {
                System.err.println("System tray is currently not supported.");
            }
        }
    
        public static void createAndShowGUI()
        {
            JFrame frame = new JFrame("Main Frame!");
            frame.setSize(new Dimension(300, 300));
    
    //        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
            int frameHeight = frame.getHeight();
            int taskbarHeight = screenHeight - frameHeight;
    
            System.out.println(screenHeight);
            System.out.println(frameHeight);
            System.out.println(taskbarHeight);
    
            SysTray sysTray = new SysTray();
            sysTray.initSystemTray();
            frame.getContentPane().add(sysTray.getContent(), "Last");
    
    //        frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args)
        {
            createAndShowGUI();
        }
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Where is it best to declare swing components?
    By MacNstuff in forum AWT / Swing
    Replies: 1
    Last Post: 02-06-2008, 01:59 AM
  2. HTML on Swing Components
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-27-2007, 10:51 AM
  3. Java Components are not displayed(sometimes)
    By archanajathan in forum AWT / Swing
    Replies: 3
    Last Post: 11-05-2007, 09:34 AM
  4. Useful Java Application Components 0.9.27
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-02-2007, 05:14 PM
  5. Gui Components
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-28-2007, 05:04 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
  •