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

**URL:** <https://discourse.processing.org/t/how-to-hide-the-sketch-window-and-the-task-bar-icon/4901>\
**Category:** Coding Questions\
**Created:** [October 26, 2018, 1:12pm UTC](https://discourse.processing.org/t/how-to-hide-the-sketch-window-and-the-task-bar-icon/4901 "2018-10-26T13:12:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![YSL](https://avatars.discourse-cdn.com/v4/letter/y/4af34b/32.png) [@YSL](https://discourse.processing.org/u/YSL)\
**Post date:** [October 26, 2018, 1:12pm UTC](https://discourse.processing.org/t/how-to-hide-the-sketch-window-and-the-task-bar-icon/4901/1 "2018-10-26T13:12:25Z")

</div>

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)

```auto
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!

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [October 28, 2018, 5:42pm UTC](https://discourse.processing.org/t/how-to-hide-the-sketch-window-and-the-task-bar-icon/4901/2 "2018-10-28T17:42:59Z")

</div>

> [@YSL](#):
>
> including the taskbar icon?

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:

- [java swing hide taskbar icon - Google Search](https://www.google.com/search?q=java+swing+hide+taskbar+icon)
- [java awt hide TrayIcon - Google Search](https://www.google.com/search?q=java+awt+hide+TrayIcon)

---

<div class="post-metadata">

**Author:** ![raron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/raron/32/13651_2.png) [@raron](https://discourse.processing.org/u/raron)\
**Post date:** [September 14, 2020, 2:39pm UTC](https://discourse.processing.org/t/how-to-hide-the-sketch-window-and-the-task-bar-icon/4901/3 "2020-09-14T14:39:08Z")

</div>

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.

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

```

Should be

```auto
  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 🙂

```auto
/** 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.

---

<div class="post-metadata">

**Author:** ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)\
**Post date:** [September 14, 2020, 3:57pm UTC](https://discourse.processing.org/t/how-to-hide-the-sketch-window-and-the-task-bar-icon/4901/4 "2020-09-14T15:57:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![raron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/raron/32/13651_2.png) [@raron](https://discourse.processing.org/u/raron)\
**Post date:** [September 15, 2020, 10:01am UTC](https://discourse.processing.org/t/how-to-hide-the-sketch-window-and-the-task-bar-icon/4901/5 "2020-09-15T10:01:49Z")

</div>

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.

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

```

Which can be a nice touch.

I believe documentation for surface (=PSurface?) is [here](https://processing.github.io/processing-javadocs/core/processing/core/PSurface.html).
