Auto Clicking Application

Hey, I could really use some help with this application I'm trying to make (new to Java).

I followed the Swing Tutorial with Netbeans very closely, yet when I run the application, a GUI doesn't even show up.

If someone could look at the code (it's pretty short) and see why I can't seem to get the GUI working, I'd be grateful. You don't have to look at the actual logic of the program (to see if it would work when I get the GUI working), but if you could, it would help immensely :)

What is this code supposed to do? It never enters the while loop since clicked is false, and if it did all you would do would be generate an error from trying to read a String that's null.

I see several other significant problems with your code including an infinite while loop that if it worked would freeze your GUI as well as a bunch of unnecessary use of static variables and methods. Then there's the empty catch block...

You may wish to go through the basic Java tutorials before trying GUI programming. It will make your GUI programming experience much more rewarding and less frustrating.

Well boolean clicked is supposed to be true once the user click the JButton,

Nope. Your code considers the while loop, rejects it, and considers it no more. It's just doing what you wrote it to do.

and they can click it again to stop it. It's supposed to click the mouse at whatever interval the user enters.

If you code did work as you wanted it to, that while(true) would freeze your app, and the user would never be able to stop it.

Wouldn't the GUI pop up before it even considers the while loop?

Nope. You may want to read the Swing tutorials (not the netbeans version) to get a handle on event-driven programming.

Follow the logic please:

Java Code:

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
// Your Swing GUI has been set in motion in its own thread
new TheClicker().setVisible(true);
}
});
// meanwhile the main thread comes here, sees that clicked is false
// and then skips over this while loop
while (clicked == true) { // better to do while (clicked) {...
clicking();
}
// and once the while loop has been skipped over, it will never
// be approached again
}

So to clarify, you're saying that the GUI won't pop up because it tries to execute the while loop simultaneously with the gui's startup, and since it fails to do the loop, the program ends and the GUI never shows?

again, since clicked is a boolean, it's cleaner to do if (clicked) rather than if (clicked == true). Also, you run a much lower risk of doing the pernicious error of if (clicked = true). But even this is unnecessary. If all you want to do is toggle clicked, all you need to do is

But regardless of whether you use your code or my code, please understand that all that happens when you push the button is that clicked toggles from true to false and visa versa. Nothing else happens. The JTextField is never read, no robot is initiated, nothing? Why because if you want anything to happen on button click, you must call it in the button ActionListener's actionPerformed method.

You change msS String, but only if enter is pressed in the JTextField since this is called in the JTextField ActionListener's actionPerformed method. Again, this will not be read when the button is clicked since it's not in the button ActionListener's actionPerformed method. So if someone changes the text field's text, does not press enter, but then clicks the button, the msS string will not change.

has the exact same problem that I outlined in my previous post. Again, if this were successful at doing what you wanted, the clicking method would continually be called, creating thousands of Robot objects, overwhelming the JVM and cause your program to crash. Before that happened though, the while (true) portion would hog Swing's main thread, the event dispatch thread (EDT) causing your program to be unresponsive and helpless as the inevitable heap overload and program crash approached.

This:

Java Code:

} catch (Exception e) {
}

Should never be done unless you really really know what you're doing. You are ignoring all exceptions, and so if a run-time error occurs, you'll never know why it's occuring. Simply don't do this. At least have an e.printStackTrace() inside of the exception block.

is a static method, and there should be no static methods or variables in this type of program (other than the main method and its closely associated methods). Also, calling wait on the Robot and on the EDT is not a good thing.