[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

Invoking GUI method

Hi Experts,

I am trying to invoking a GUI method when a string has been returned, the GUI will then display the String sequentially(line by line).

Problem #1:
Need best approach to invoke the GUI method.

Problem #2:
How do I implement a linebreak in the JTextArea.

#3:
Appriciate any other comment on my code(Flaws, design, etc)

CODE:
import javax.swing.JFrame;
import java.awt.*;
import javax.swing.JTextArea;

class Display extends JFrame
{
      private String fromServer;
      private Container container;

////////////////////////////////////////////////////////////////////////////////////      
//Constructor
////////////////////////////////////////////////////////////////////////////////////      

      public Display()
      {
            super("Status bar");
            setSize(700,700);
            setVisible(true);
      }      

////////////////////////////////////////////////////////////////////////////////////      
//Returns text to JTextArea
////////////////////////////////////////////////////////////////////////////////////      

      public String getFromServer()
      {
            return fromServer;
      }
      
////////////////////////////////////////////////////////////////////////////////////      
//Get text from Server Class
////////////////////////////////////////////////////////////////////////////////////      
      
      public void setServerMsg(String status)
      {
            fromServer = status;
            //backMessages();
      }      

////////////////////////////////////////////////////////////////////////////////////      
//Display Server/Client status messages.
////////////////////////////////////////////////////////////////////////////////////      
      
      public void backMessages(Graphics g)
      {
            super.paint(g);
            JTextArea hello = new JTextArea(10,15);
            container = getContentPane();
            
            /**********      Append text to       ******/
            hello.setText("This is a status report\n");
            hello.append(getFromServer());
            
            /**********      GUI layout       ******/
            hello.setEnabled(false);
            getContentPane().setLayout(new BorderLayout());                                    
            getContentPane().add(hello);
            g.setColor(Color.BLACK);
            
            /**********      Dos Window printOut       ******/
            System.out.println(getFromServer());
      }
}
0
Idarhillgaar
Asked:
Idarhillgaar
  • 4
  • 4
1 Solution
 
IdarhillgaarAuthor Commented:
note:
I don't really need to put in 'Graphics'. So picture the code without it. :)
0
 
InteractiveMindCommented:
For 'Problem #2', you should be able to do this:

hello.append(getFromServer() + "\n");

0
 
IdarhillgaarAuthor Commented:
Made some changes, seems to run better now. Still have some problems displaying the text row by row, so if any1 has a good idea of how to solve this that would be appriciated.

CODE Update:
import javax.swing.JFrame;
import java.awt.*;
import javax.swing.JTextArea;

class Display extends JFrame
{
      private String fromServer;
      private Container container;

////////////////////////////////////////////////////////////////////////////////////      
//Constructor
////////////////////////////////////////////////////////////////////////////////////      

      public Display()
      {
            super("Status bar");
            setSize(700,700);
      }      

////////////////////////////////////////////////////////////////////////////////////      
//Returns text to JTextArea
////////////////////////////////////////////////////////////////////////////////////      

      public String getFromServer()
      {
            return fromServer;
      }
      
////////////////////////////////////////////////////////////////////////////////////      
//Get text from Server Class
////////////////////////////////////////////////////////////////////////////////////      
      
      public void setServerMsg(String status)
      {
            fromServer = status;
            backMessages();
      }      

////////////////////////////////////////////////////////////////////////////////////      
//Display Server/Client status messages.
////////////////////////////////////////////////////////////////////////////////////      
      
      public void backMessages()
      {
            JTextArea hello = new JTextArea(1,0);
            container = getContentPane();
            
            /**********      Append text to       ******/
            hello.append(getFromServer());
            
            /**********      GUI layout       ******/
            hello.setEnabled(false);
            getContentPane().setLayout(new FlowLayout());                                    
            getContentPane().add(hello);
            setVisible(true);

            
            /**********      Dos Window printOut       ******/
            System.out.println(getFromServer());
      }
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
InteractiveMindCommented:
Have you seen my idea?..

hello.append(getFromServer() + "\n");

0
 
IdarhillgaarAuthor Commented:
Good idea, but I'm creating a new JTextArea for every String returned - Gives me neat screen layout. However I'm not sure how efficient this approach is.
If its slow then I'll implement u'r solution.
0
 
IdarhillgaarAuthor Commented:
Went for your solution :)
0
 
InteractiveMindCommented:
Okay  :-)
0
 
InteractiveMindCommented:
Cheers.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now