0

A newbie here so forgive me.

This is my code:

Parent/main/JFrame:

public class Home extends javax.swing.JFrame {
    public Home() {
        initComponents();
        setIcon("icon"); // set the taskbar icon
    }

    public static void main(String args[]) {
        // main code here, including tray initialization
        new Home().setVisible(true);
    }

    public void start() {
        // code here
    }
}

Child class:

public class Tray extends Home {

    static TrayIcon trayIcon;

    private static void ShowTrayIcon(String status) {

        if (!SystemTray.isSupported()) {
            System.out.println("Tray not supported");
            System.exit(0);
            return;
        }

        final PopupMenu popup = new PopupMenu();

        final SystemTray tray = SystemTray.getSystemTray();
        trayIcon = new TrayIcon(CreateIcon("/Images/off.png", "desc"));

        MenuItem StartItem = new MenuItem("Start");
        popup.add(StartItem);

        trayIcon.setPopupMenu(popup);

        // open from tray
        StartItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                start();
            }
        });

        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
        }
    }

    protected static Image CreateIcon(String path, String desc) {
        URL ImageURL = Tray.class.getResource(path);
        if (ImageURL == null) {
            System.err.println("Resource not found: " + path);
            return null;
        } else {
            // System.err.println("Resource found: " + path);
            return (new ImageIcon(ImageURL, desc)).getImage();
        }
    }

    public void start() {
        super.start();
    }
}

the start() method in child class works, but when I try to call that from the actionlistener of the trayicon, it keeps saying 'non static method start() cannot be referenced from static context'.

Tried using super.start() in the actionlistener method but that doesn't work either.

What I'm trying to do; Home class is my JFrame and main class, I have another class set up that handles my tray icons, and got some buttons in the tray that will call some methods from the main class when invoked.

Any help would be appreciated.

Martin
  • 122
  • 9
Pengiuns
  • 131
  • 2
  • 9

1 Answers1

0

If I'm right, this is where the error appear:

StartItem.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {

         start();
    }
});

That's, because the code above is in a static method. It do not have any reference to a Tray instance, only for the Tray class. And the start() method belongs to a Tray instance, not for the class. You should has a reference for a Tray instance (variable).

There's two ways for this:

  • remove the static keyword before the method
    In this case, the ShowTrayIcon method will belong to an instance, and could call the start method
  • add a Tray parameter for the method.
    In this case, the method will look something like this

Code

private static void ShowTrayIcon(String status, Tray tray) {
    // some code here
    tray.start();
}

Hope, this helps.

Nagy Vilmos
  • 1,878
  • 22
  • 46