How to hide the sketch window and the task bar icon?

How can I hide the sketch window entirely, including the taskbar icon? Currently, I can create a TrayIcon and call surface.setVisible(false) to hide the window when I need to, but the taskbar icon is still there. How can I hide it entirely? Also, how to make it reappear if I click on Open in the menu of the TrayIcon?

Here’s a snippet of my sketch (I have to use P2D)

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.UIManager;

TrayIcon trayIcon;
final SystemTray tray = SystemTray.getSystemTray();

void settings() {
  fullScreen(P2D);
  pixelDensity(displayDensity());
}

void setup() {
  surface.initOffscreen(this);
  surface.setAlwaysOnTop(true);
  try {
    File file = new File("icon.png"); 
    FileInputStream fis = new FileInputStream(file);  
    trayIconImg = ImageIO.read(fis);
    trayIcon = new TrayIcon(trayIconImg);
  } 
  catch (IOException e) {
    e.printStackTrace();
  }
  addTrayIcon();
}
void addTrayIcon() {
  //Check the SystemTray is supported
  if (!SystemTray.isSupported()) {
    System.out.println("SystemTray is not supported");
    return;
  }
  final PopupMenu popup = new PopupMenu();

  // Create a pop-up menu components
  MenuItem exitItem = new MenuItem("Exit");
  exitItem.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
      tray.remove(trayIcon);
      exit();
    }
  }
  );
  MenuItem openItem = new MenuItem("Open");
  exitItem.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
      surface.setVisible(true);  //NOT working
      bg.state.Target = 100;
    }
  }
  );

  //Add components to pop-up menu
  popup.add(openItem);
  popup.add(exitItem);
  trayIcon.setImageAutoSize(true);

  trayIcon.setPopupMenu(popup);

  try {
    tray.add(trayIcon);
  } 
  catch (AWTException e) {
    System.out.println("TrayIcon could not be added.");
  }
}

Thanks!

It looks to me like this might be a java.awt or a Java Swing question, and not specific to Processing. I don’t have an answer, but my first guesses on where to look based on the object classes you are using:

I know this is an old post, OP might have solved it long ago. But I somehow came across this interesting thread a couple of days ago, and I just noticed this one error.

  MenuItem openItem = new MenuItem("Open");
  exitItem.addActionListener(new java.awt.event.ActionListener() {

Should be

  MenuItem openItem = new MenuItem("Open");
  openItem.addActionListener(new java.awt.event.ActionListener() {

I changed the sketch slightly to work with Processing (since I couldn’t get it to work as-was). But I kind of don’t know what I’m doing, especially with these java.awt and java.swing things. Also, I’ve no idea what swapping out the IOException with Exception does (the former wouldn’t work in Processing, and I don’t know what a corresponding IOException would be). But it worked, so there :slight_smile:

/** Tray icon test

https://discourse.processing.org/t/how-to-hide-the-sketch-window-and-the-task-bar-icon/4901
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.UIManager;

TrayIcon trayIcon;
final SystemTray tray = SystemTray.getSystemTray();

void settings() {
  //fullScreen(P2D);
  size(400,400);
  pixelDensity(displayDensity());
}

void setup() {
  surface.initOffscreen(this);
  surface.setAlwaysOnTop(true);
  surface.setVisible(false);
  
  try {
    //String pathToData = dataPath("") + "/";
    PImage tempicon;
    tempicon = loadImage("icon.png");
    //ImageIcon icon = new ImageIcon(pathToData + "icon.png");
    trayIcon = new TrayIcon(tempicon.getImage());
  } 
  catch (Exception e) {
    e.printStackTrace();
  }
  addTrayIcon();
}

void addTrayIcon() {
  //Check the SystemTray is supported
  if (!SystemTray.isSupported()) {
    System.out.println("SystemTray is not supported");
    return;
  }
  final PopupMenu popup = new PopupMenu();

  // Create a pop-up menu components
  MenuItem exitItem = new MenuItem("Exit");
  exitItem.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
      tray.remove(trayIcon);
      exit();
    }
  }
  );
  MenuItem openItem = new MenuItem("Open");
  openItem.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
      surface.setVisible(true);
    }
  }
  );

  //Add components to pop-up menu
  popup.add(openItem);
  popup.add(exitItem);
  trayIcon.setImageAutoSize(true);

  trayIcon.setPopupMenu(popup);

  try {
    tray.add(trayIcon);
  } 
  catch (AWTException e) {
    System.out.println("TrayIcon could not be added.");
  }
}

I just keep this as an example of how to add a tray icon, if I ever should need one.

1 Like

Nice job. I tried something similar without success.
If you go further I’m interested to see you progress.
Thanks for sharing

I should probably make a new thread then. But I’m not sure I’ll continue with this for now.

Kind of related, I found this neat command to make a custom icon for the sketch window.

    tempicon = loadImage("icon.png");
    surface.setIcon(tempicon);

Which can be a nice touch.

I believe documentation for surface (=PSurface?) is here.

1 Like