MissCoder87
MissCoder87

Reputation: 2669

Calling non static class java

I'm trying to notice when something happens in particular in my java app, i'm trying to make the window flash (on mac and windows). However, i'm new to Java and getting frustrated with the static/non static methods jazz.

However my frustrations aside, how do I call my toggleVisible class?

I've removed the unnecessary code:

public static void main(String args[]) {

Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            checkAlerts();
        }
        }, 30000, 30000);
    }

public static Boolean checkAlerts(){

          if(count ==  0){
               JOptionPane.showMessageDialog(null, "No results");
           } else {
                toggleVisible();
                JOptionPane.showMessageDialog(null, "Some results back");
           }
    }

public void toggleVisible() {
      setVisible(!isVisible());
      if (isVisible()) {
        toFront();
        requestFocus();
        setAlwaysOnTop(true);
        try {
          //remember the last location of mouse
          final Point oldMouseLocation = MouseInfo.getPointerInfo().getLocation();

          //simulate a mouse click on title bar of window
          Robot robot = new Robot();
          robot.mouseMove(mainFrame.getX() + 100, mainFrame.getY() + 5);
          robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
          robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

          //move mouse to old location
          robot.mouseMove((int) oldMouseLocation.getX(), (int) oldMouseLocation.getY());
        } catch (Exception ex) {
          //just ignore exception, or you can handle it as you want
        } finally {
          setAlwaysOnTop(false);
        }
      }
    }

toggleVisible gives me the error: non static method toggleVisible() cannot be referenced from a static context.

Upvotes: 0

Views: 285

Answers (1)

nIcE cOw
nIcE cOw

Reputation: 24616

Stop using unnecessary use of static keyword. Instead simply create an object of the class, inside the main method and call a method, to which you will leverage the task of taking the flow of the application forward.

In simple words, consider this example:

public class Example {

    private void performTask () {
        /*
         * Now start wriitng code from here.
         * Stop using main, for performing 
         * the tasks, which belongs to the 
         * application.
         */
    }

    public static void main ( String[] args ) {
        new Example ().performTask ();
    }
}

Moreover, it seems, you dealing with Swing. For that reason, consider javax.swing.Timer, over java.util.Timer, as the former is suppose to put GUI related tasks automatically on the Event Disptacher Thread - EDT, though in the latter that responsibility lies purely with the programmer.

Here is one small example, of using Swing's Timer class:

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

public class LabelColourExample {

    private Timer timer;
    private JLabel label;
    private Color [] colours = { 
        Color.red,
        Color.blue,
        Color.green,
        Color.cyan,
        Color.yellow,
        Color.magenta,
        Color.black,
        Color.white
    };
    private int counter;

    private static final int GAP = 5;   

    private ActionListener timerActions = new ActionListener () {
        @Override
        public void actionPerformed ( ActionEvent ae ) {
            label.setForeground ( colours [ counter++ ] );
            counter %= colours.length;
        }
    };

    public LabelColourExample () {
        counter = 0;
    }

    private void displayGUI () {        
        JFrame frame = new JFrame ( "Label Colour Example" );
        frame.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );

        JPanel contentPane = new JPanel ();
        contentPane.setLayout ( new BorderLayout ( GAP, GAP ) );

        label = new JLabel ( "MyName", JLabel.CENTER );
        label.setOpaque ( true );
        contentPane.add ( label, BorderLayout.CENTER );

        JButton button = new JButton ( "Stop" );
        button.addActionListener ( new ActionListener () {
            @Override
            public void actionPerformed ( ActionEvent ae ) {
                JButton button = ( JButton ) ae.getSource ();
                if ( timer.isRunning () ) {
                    timer.stop ();
                    button.setText ( "Start" );
                } else {
                    timer.start ();
                    button.setText ( "Stop" );
                }
            }
        } );
        contentPane.add ( button, BorderLayout.PAGE_END );

        frame.setContentPane ( contentPane );
        frame.pack ();
        frame.setLocationByPlatform ( true );
        frame.setVisible ( true );

        timer = new Timer ( 1000, timerActions );
        timer.start ();
    }

    public static void main ( String[] args ) {
        Runnable runnable = new Runnable () {
            @Override
            public void run () {
                new LabelColourExample ().displayGUI ();
            }
        };
        EventQueue.invokeLater ( runnable );
    }
}

Upvotes: 2

Related Questions