1

I need to display result of computation that I used to compile in standard java programming (in Main clas) in java swing GUI test area in netbeans, but I always encountering problem on it. I bet the problem is because my String to be printed is not available in the GUI class, here's my part of code in swing

private void predictActionPerformed(java.awt.event.ActionEvent evt) {                                        

        TextArea1.setText(Engine.Print.printresult(toarray));
        //JOptionPane.showMessageDialog(null,Engine.Print.printresult(toarray));
    //       TextArea1.setText(Retrieving.main(args));
    } 

and here's the code in package Engine, Class Print, method printresult, String[]printresult is passed from another class and it works to run directly without GUI:

 public static void printresult(String[]toarray){
  for(int a=0; a<toarray.length;a++){
    System.out.println(toarray[a]);
  }

really need help. thank you

Rameshwar.S.Soni
  • 630
  • 1
  • 8
  • 22
charcoalite
  • 43
  • 1
  • 2
  • 10
  • Are you sure that you want to mix a console program with a Swing program? Why do you want to do this? Perhaps you're showing us the wrong method from the engine package, since there really is no place for `System.out.println(...)` in a GUI program except perhaps for debugging purposes. – Hovercraft Full Of Eels Mar 20 '12 at 03:18
  • The exception of course is if you have to redirect the standard OutputStream to your JTextArea, such as I show in my answer from earlier today here: [how to visualize console java in JFrame/JPanel](http://stackoverflow.com/a/9776819/522444), but this is an awful lot of trouble for a problem that is usually best solved in other easier ways. – Hovercraft Full Of Eels Mar 20 '12 at 03:26
  • Do you mean you want to display `JUnit` `@Test` results in the NetBeans `Test Results` window? – trashgod Mar 20 '12 at 04:49
  • Actualy I need to display the value of my calculation of my console in swing Text area since this program need to be visualized using GUI, i think it is impossible to just copy all my code into GUI since it contains several class instead, so I only need to send the result into GUI swing textarea but I have no idea what to do. Can anyone tell me how to do that since this way is not working. Thank you – charcoalite Mar 20 '12 at 04:58
  • please learn java naming conventions and stick to them – kleopatra Mar 20 '12 at 13:57

1 Answers1

0

You state:

Actualy I need to display the value of my calculation of my console in swing Text area since this program need to be visualized using GUI, i think it is impossible to just copy all my code into GUI since it contains several class instead, so I only need to send the result into GUI swing textarea but I have no idea what to do. Can anyone tell me how to do that since this way is not working.

What you need to do first and foremost is re-write your calculation code. You don't show us any of this code, which makes it impossible to guess what it's doing or what it's doing wrong, but I suspect that it does calculations and prints them out to the console, which won't work for a GUI.
What I recommend that you do for your non-GUI calculation code:

  • Again, first and foremost, re-write your calculation code so that it because a proper OOPs class
  • It should include private instance fields, at least one constructor, and public methods to allow passing information to this class as well as methods for extracting results from the class, although some methods will do both. None of this code will involve println's to the console unless this is for debugging purposes with the idea that the lines will be removed in the final "production" code.
  • This class or classes described above will be your "model" or the brains behind the GUI.
  • It should have no (or very limited) knowledge about the GUI (it should be GUI "agnostic") and written flexible enough so that it could be used by a Swing GUI a console program, or any other user interface you can think of.
  • If you want to get fancy, give your model a PropertyChangeSupport object so that the GUI can "listen" to the model for changes and respond to these changes.

What I recommend that you do for your GUI dislay code:

  • This will be your "view" model and it too must be written to conform to the principles of object oriented programming.
  • Give the view a instance field of the model that we described above. In other words, your view will hold a model object inside of it.
  • Have your view controllers or listeners interact with the model. For instance, if a JButton is pressed, have its ActionListener inform the model (by calling the appropriate method), that the button has been pressed. If the model needs information from the GUI, such as the text held in JTextFields, then the ActionListener will extract this text and then pass the information into the model using method parameters.
  • Have your GUI register as a "listener" to the model, perhaps using a PropertyChangeListener, and this way it can respond to changes in the model state.

These suggestions are very generic and as such may not be as helpful to you as you or I would like, but this is of necessity given the limited information that we know about your problem. For more specific help, then please give us more specific and detailed information about your problem and your code.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373