Category: Featured

Featured posts

  • Java TrayIcon Tutorial (With popupmenu and images)

    Java TrayIcon Tutorial (With popupmenu and images)

    Trayicons are really useful widgets for interacting with the application when the main dialog is minimized/hidden or the application is running in the background. In this tutorial, let’s see how to add a Java Tray Icon with example code.

    Note: JavaFX does not have a separate TrayIcon system. If your project is using JavaFX, then the tutorial is still fully applicable. Because the java.awt.SystemTray can be used for JavaFX as well!

    Creating a simple TrayIcon

    The first step on adding a TrayIcon is checking whether the platform supports it. As you know, java supports a wide variety of platforms and not all platforms will support SystemTray feature. So, the first step on configuring it is to check if it is really supported. Let’s see the basic algorithm here

    1. Make sure the platform supports SystemTray
    2. Load image for the trayicon
    3. Create trayicon object and add it to the tray

    Let’s see how it is done in code.

    public void createTrayIcon() {
      //Check for SystemTray support
      if (!SystemTray.isSupported()) {
        System.err.println("System tray feature is not supported");
        return;
      }
    
      //Get system tray object
      SystemTray tray = SystemTray.getSystemTray();
    
      //Create TrayIcon instance
      Image image = ImageIO.read(TrayIconService.class.getResourceAsStream("/path/to/your/icon.png"));
      TrayIcon trayIcon = new TrayIcon(image, "Genuine Coder", null);
      trayIcon.setImageAutoSize(true);
    
      //Attach TrayIcon to SystemTray
      tray.add(trayIcon);
    }
    

    and…That’s it. When you run the code, you will get a nice TrayIcon in your system’s taskbar. Below, you can find the output of running the code on my system.

    Java TrayIcon on windows 10 taskbar

    Creating TrayIcon with PopupMenu

    Now, let’s see how to add a PopupMenu into the TrayIcon to add some functionality. Let’s iterate the steps first.

    1. Create PopumMenu
    2. Create one Menu and multiple MenuItems and organize them in a tree structure
    3. Attach Menu to PopumMenu
    4. Attach PopupMenu to TrayIcon

    Basically, we need to prepare the MenuItems as we intend to and then attcah it to the PopupMenu. As an example, let’s create the following menu.

    Java TrayIcon With PopupMenu

    public void createTrayIcon() {
      //Check for SystemTray support
      if (!SystemTray.isSupported()) {
        System.err.println("System tray feature is not supported");
        return;
      }
      SystemTray tray = SystemTray.getSystemTray();
      Image image = ImageIO.read(TrayIconService.class.getResourceAsStream("/path/to/your/icon.png"));
      TrayIcon trayIcon = new TrayIcon(image, "Genuine Coder", null);
      trayIcon.setImageAutoSize(true);
    
      //Create root menu
      PopupMenu rootMenu = new PopupMenu();
      
      //Add about and restart items
      MenuItem about = new MenuItem("About");
      rootMenu.add(about);
      Menu restartMenu = new Menu("Restart");
      rootMenu.add(restartMenu);
    
      //Add sub-items to server
      MenuItem restartClient = new MenuItem("Client");
      MenuItem restartServer = new MenuItem("Server");
      restartMenu.add(restartClient);
      restartMenu.add(restartServer);
    
      //Attach to trayIcon
      trayIcon.setPopupMenu(rootMenu);
    
      tray.add(trayIcon);
    }
    

    Adding event handling to the TrayIcon

    Now, let’s see how to attach event-handlers to the TrayIcon menu. As an example, let’s show an alert when the “about” MenuItem is clicked.

    about.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "Wow! Java TrayIcon is working!");
        }
    });
    

    With this event-handler, when you click on the “about” MenuItem in the TrayIcon menu, you will get the following alert.
    TrayIcon action with event handler

    Conclusion

    In this tutorial, we have familiarized with Java TrayIcon and added PopupMenu and action listeners into it. TrayIcons can be customized even further by customizing the MenuItem. You can add special MenuItems like CheckBoxMenuItem to add switch behaviour into your SystemTray. You may also want to checkout the following articles

    1. How to send email using Java
    2. Convert JAR to EXE
  • Library Assistant 4.0 – The Best Library Management Software For Free

    Library Assistant 4.0 – The Best Library Management Software For Free

    Library Assistant is a free Java based simple library management software developed mainly for schools and colleges. Library Assistant 4.0 comes with great features such as enhanced search, bar code support etc.

    Simple User Interface

    LA is designed for simplicity by hiding complex things from the user. You can issue a book by just 3 clicks or submit a book with just 2 clicks!

    Library Assistant Main Window

    BarCode Ready

    Library Assistant supports barcode readers. You can increase your library efficiency simply by making use of Bar code readers with LA.

    Barcode Support

    Run it Anywhere and Everywhere

    LA is platform independent. You can run it on your favorite Operating System. All that you need is the Java Runtime Environment installed.

    Full linux compatibility

    Keep Your Data Secure

    A username and password is required for logging in. You can set your username and password during initial configuration.

    Login Screen for LA

    Advanced Book Search

    You can search for a book by its Title, Author, Publisher, Book ID (Bar code Number) or even with its location on the library.
    Book search window
    Search Window

    Not Returned the Books Yet? Notify Them

    LA introducing a new email notification feature that send a notification email to the members with details regarding book issue and fine amount.
    Email Notification window
    Email Notification Window

    Track Members

    Library Assistant can keep of track of member’s each activity. So you can easily differentiate between active and inactive members. 

     

    Member profile window
    Member View

    Have an idea ? Let me know

    LA has been getting updated for last 3 years. If you want a particular feature to be added on to LA, let me know in the comments.

    Before downloading library assistant, your computer must have the Java Runtime Environment (JRE) installed. If it is not installed, please download the latest version from the below link.

    https://www.java.com/en/download/

    http://www.mediafire.com/download/4b783g8vk17hdsc/Library_Assistant_Linux.zip
    http://www.mediafire.com/download/6eql8ck1w7zqajt/Library_Assistant_Windows_5.zipKeywords: Library Management program for pc, Library Software, Library Assistant 4, Library Management Software, Library Program Java, Java Library Software, Library Management Software for Linux, Linux Library Software, simple library management system.