0

I have a jLabel that is supposed to output the computer name based on an active directory search. The computer name gets assigned to the variable "CN" no problem, but it won't show up on the jLabel unless I run the gui again. How can I get the jLabel text to appear in real time within the same instance of the gui? The CN variable appears towards the bottom of the code posted.

            StringBuffer sbuffer = new StringBuffer();
            BufferedReader in = new BufferedReader(new InputStreamReader(p
                    .getInputStream()));

            try {

                while ((line = in.readLine()) != null) {

                    System.out.println(line);

                    // textArea.append(line);

                    String dn = "CN=FDCD111304,OU=Workstations,OU=SIM,OU=Accounts,DC=FL,DC=NET";
                    LdapName ldapName = new LdapName(dn);
                    String commonName = (String) ldapName.getRdn(
                            ldapName.size() - 1).getValue();


                }
                ComputerQuery.sendParam();

            } catch (IOException e1) {

                // TODO Auto-generated catch block

                e1.printStackTrace();

            } catch (InvalidNameException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } finally

            {
                try {
                    fw.close();

                }

                catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

            try {

                in.close();

            } catch (IOException e1) {

                // TODO Auto-generated catch block

                e1.printStackTrace();

            }

            ComputerQuery.sendParam();


        }
    });

    try (BufferedReader br = new BufferedReader(new FileReader("resultofbatch.txt")))
    {


        final Pattern PATTERN = Pattern.compile("CN=([^,]+).*");
        try {
            while ((sCurrentLine = br.readLine()) != null) {

                String[] tokens = PATTERN.split(","); //This will return you a array, containing the string array splitted by what you write inside it.
                //should be in your case the split, since they are seperated by ","
               // System.out.println(sCurrentLine);
                CN = sCurrentLine.split("CN=",-1)[1].split(",",-1)[0];

                System.out.println(CN);
                 testLabel.setText(CN);

            }


        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

New code added

@Override
    protected Integer doInBackground() throws Exception {
          System.out.println(CN);
           testLabel.setText(CN);

        return null;
    }
user6680
  • 79
  • 6
  • 34
  • 78
  • Can we get a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)? – Binkan Salaryman Jun 26 '15 at 12:17
  • 3
    You're blocking the Event Dispatching Thread, use a `SwingWorker` to execute the command in another thread. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) for more details. – MadProgrammer Jun 26 '15 at 12:17
  • For [example](http://stackoverflow.com/questions/15801069/printing-a-java-inputstream-from-a-process/15801490#15801490) – MadProgrammer Jun 26 '15 at 12:20
  • I added Swingworker like you suggested, but it isn't working as intended. I'm getting no output currently. Any suggestions? – user6680 Jun 26 '15 at 12:53

0 Answers0