charcoalite
charcoalite

Reputation: 43

Java GUI Netbeans, display output in textarea from class in another package

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

Upvotes: 1

Views: 6675

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

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.

Upvotes: 0

Related Questions