Okay, so I have the following

            if (details){ 
            defaultHost = tfServer.getText();
            defaultPort = Integer.parseInt(tfPort.getText());

            System.out.println("Not connected");


where details is true when a button is clicked. This works fine expect that my console is getting spammed by "Not connected" until I press the button and details is true. How can I keep my while loop (!connected) running if I don't have the line System.out.println("Not connected"); ? If I remove it the while loop only executes once ... I am trying to have something like Do nothing until details is true
Any help is appreciated ! :D

2 Years
Discussion Span
Last Post by JamesCherrill

This is a very inefficient way to do it. Instead of continuously checking if a button is pressed, you should "do something" ONLY when the button is pressed.

What are you using to draw your button?

If your using swing, use an ActionListener to print "connected". There's an example here.

Also, you do not need System.out.println("Not connected"); to keep your loop running. It will run even if you don't see anything. The problem is that this uses a lot of resources just to check if a button has been pressed.

Edited by Hiroshe


Ah the print lines were only for me to checked whether or not in enters the loops, and If i remove the not connected line, the loop just stops >.>
I have actionListener on my button and when the button is pressed it just changes the value of details to true and then my run() method executes, or in other words I don't want the run method to execute before I've pressed a button


There's an alarm bell ringing over this line...


You (almost) never call run() directly in Java. The Runnable interface is intended for use with Threads - ie you instantiate a Thread object using the object that has the run() method, then call start() for the Thread object. The Thread starts a new thread, then it calls your run() method in the new thread.

It's essential that you understand threads at this level for your application. At the very minimum you will have one thread that runs at startup to initialise everything, one thread responding to to user input (that's the Swing Event Dispatch Thread, or EDT), and a thread of your own that waits for input from the server and passes that to the GUI.

If ou want help getting this right, don't hestitate to ask here.


Hey again James :) To begin with thanks for your reply!
I think there was a small misunderstanding :p at first my run() was not meant to be the run method of Runnable, but I placed the run to be activate on button click and since my run() has an infinitive while loop which is waiting for messages from the server, i could never leave the button click execution and my gui(if u remember the gui code i posted earlier yesterday) just stucked. So I had to implement Runnable and use this in a new thread, then it worked great. Also here is the solution, in case someone doesn't want to use resources on infinitive while loops rather than event triggered execution. Oh, and since my old run() throws IOE, and the run() in runnable doesn't ,the solution to this is just place your run() in a try/catch block, that will do it :) Here is the main and the run(). Thanks to everyone who replied and also thanks James=)

public static void main(String[] args) throws Exception {

        final newClient program= new newClient("localhost", 9000); //"vps.devrandom.dk"

         ok.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
              System.out.println("details accepted");
              details = true;
              defaultHost = tfServer.getText();
              defaultPort = Integer.parseInt(tfPort.getText());

              (new Thread(program)).start();

 public void run() {
        try {
        //connected = true;
        Socket clientSocket = new Socket(defaultHost, defaultPort);
        BufferedReader inFromUser = new BufferedReader(
            new InputStreamReader(clientSocket.getInputStream()));
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
        while (true){
              modifiedSentence = inFromServer.readLine();
          //messageArea.append(modifiedSentence + "\n");
          Matcher m = msgre.matcher(modifiedSentence);
          String vdeltas = m.group(1);
          String msg = m.group(2);
          Matcher vectorM = vclock.matcher(vdeltas);
            long indx = Long.parseLong(vectorM.group(1));
            long value = Long.parseLong(vectorM.group(2));
          Message nmg = new Message(lastClock,msg);
          Collections.sort(received, new Comparator<Message>() {
            public int compare(Message a, Message b){
              //return 0;
                return -1;
              else if(a.isEqual(b))
                return 0;
                return 1;

          //messageArea.append(modifiedSentence + "\n");
        } catch (IOException e) {

One small point - rather than just print "error" when you get an exception, it's a good idea to execute an e.printStackTrace(); so you will see all the information that Java has collected about why and where that exact error happened.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.