I have two programs, one is a GUI and the other is a normal (non GUI) program. How do I make a method in the non-GUI program wait until a JButton on the GUI gets clicked before continuing?

(No while loops)

This is frustrating since I'm fairly certain somebody here has answered this question for me before, but I can't find the post, if so.

I'm not sure how you plan to pause a method from executing instructions within it without somehow pausing (or 'blocking') at a particular line in the method.

If you don't want to use a while loop, you can make a conditional clause where the loop sends its data to a Service object and places it in a Queue. When the JButton is called it will service the request and poll the Service object from the Queue. You can emulate this happening once per method call by making a condition such that when something is inside the queue, nothing in the method happens again until the Queue is empty (basically when the Queue is empty, the non-gui method does something (stores a service object in the queue) and when the Queue is full the non-gui method does nothing).

I'm assuming you don't want to halt your program, and you need something to happen in the future. If so, use a Queue system to Service the requests one at a time when needed.

It would really help if you posted a little more information about the nature of the two "programs". Separate JVMs? Concurrently executing threads? You question is a little too unclear to give an answer.

As other commented on your, I, myself, find it a little blurry. Your question, I mean. If what you mean is that you have 2 java classes/ files which one class/file is contented the GUI definition and the other class/file is contented other non-GUI method. And what you are try to do is 'preparing' method(s) in the non-GUI class/ file to be then 'called' when a button is clicked which the button is already defined in the GUI class/ file. If it is so, then here's an input from me,

If you are developing using "visual-aid tools" (such as: NetBeans, Eclipse, etc.) on the GUI interface double click the intended button which will automatically create a onButtonClicked method and direct to that particular method. From there you can call your 'non-GUI method' to add the action of your button.

here's the sample code:

public class nonGUI
{
[INDENT]
...

public void actionOnButtonClick()
{
[INDENT]//your action goes here[/INDENT]
}
[/INDENT]
}


public class GUI
{
[INDENT]
...
public void buttonClicked()
{
[INDENT]
nonGUI ng = new nonGUI();
ng.actionOnButtonClick();
[/INDENT]
}
[/INDENT]
}

But I never said I was using netbeans or otherwise, and I'm not, lol. Sorry for the lack of information. And I'm pretty tired right now, I'm not sure about the threads thing. The 'main' class, when run, calls createAndShowGUI in the other class.

Side note; isn't the only way a new thread is created if

1. the main method is called
2. a new runnable is declared.

My 'main' class uses new runnable when it creates the gui so I guess they're in separate threads.

what are we talking about here? :s
two seperate applications or two seperate classes?

That is what I was trying to determine as well. It sounds like you are just asking about communicating between 2 classes in the same application, which is no trouble at all. Post a little bit of the code and it would be much clearer what you are trying to do.

ehh, forget this question. I was asking about communicating between two classes in the same application. I already know ways to do this, but I was trying to figure out if there was a way for the non-event driven code in the one class to 'wait' for the GUI to be clicked before continuing to execute. I accomplished the same merely by calling a method from within actionPerformed, but I was looking for a cooler way

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.