1 Reply Latest reply: Apr 8, 2013 2:14 AM by UdayaShankaraGandhi RSS

    To redirect System.out stream output on swing's JTextArea

    1000192
      Following is a program that needs to print output as follows:-

      EXPECTED OUTPUT ON GUI:_ In action block (datapayload value) In Routing block (argus value)

      PRESENT PROGRAM OUTPUT ON GUI_ In action block (datapayload value)

      please examine the below code and correct mistakes to get the expected output as given above.....

      I am not getting the second line as output on GUI i.e., In Routing block (argus value)


        
          import java.awt.*;
          import javax.swing.*;
          import java.awt.event.*;
          import java.io.*;
      
          public class Demo extends JFrame implements ActionListener
         {
            JTextField datapayload;
            JLabel Datapayload;
            JButton submit;
            JTextArea textFieldName;
            public Demo()
            {
               DemoLayout customLayout = new DemoLayout();      
               getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12));
               getContentPane().setLayout(customLayout);
      
       
               Datapayload = new JLabel("Enter DataPayload:");
               getContentPane().add(Datapayload);
            
               datapayload = new JTextField("abcd...");
               getContentPane().add(datapayload);
      
               submit = new JButton("submit");
               getContentPane().add(submit);
                
               textFieldName = new JTextArea(80,10);
               textFieldName.setEditable( false );
               JScrollPane scroll = new JScrollPane(textFieldName) ;
               scroll.setBounds( 10, 60, 225, 150 );
               getContentPane().add( scroll );
               submit.addActionListener(this);
                         
               setSize(getPreferredSize());
             }
                                                    
             public void actionPerformed(ActionEvent e) 
             {
              
                String demodata=datapayload.getText();
                   textFieldName.append("In Action Block"+"\t"+demodata);   
                Demo pr = new Demo();                              
                pr.DemoRoute(demodata);
             }
                                                   
             public void DemoRoute(String argus)
             {
                   textFieldName.append("In routing block"+"\t"+argus);
           }
           
              public static void main(String args[])
             {
                   Demo window = new Demo();
                   window.setTitle("Demo");
                   window.pack();
                   window.show();
              }
          }
      
          class DemoLayout implements LayoutManager
          {
             public DemoLayout() {  }
             public void addLayoutComponent(String name, Component comp) {   }
             public void removeLayoutComponent(Component comp) {  }
             public Dimension preferredLayoutSize(Container parent)
             {
               Dimension dim = new Dimension(0, 0);
               Insets insets = parent.getInsets();
               dim.width = 320 + insets.left + insets.right;
               dim.height = 240 + insets.top + insets.bottom;
               return dim;
             }
      
             public Dimension minimumLayoutSize(Container parent)
             {
               Dimension dim = new Dimension(0, 0);
               return dim;
             }
             public void layoutContainer(Container parent) 
             {
               Insets insets = parent.getInsets();
               Component c;
               c = parent.getComponent(0);
               if (c.isVisible()) {c.setBounds(insets.left+90,insets.top+8,172,26);}
               c = parent.getComponent(1);
               if (c.isVisible()) {c.setBounds(insets.left+230,insets.top+8,172,26);}
               c = parent.getComponent(2);
               if (c.isVisible()) {c.setBounds(insets.left+230,insets.top+52,142,26);}
               c = parent.getComponent(3);
               if (c.isVisible()) {c.setBounds(insets.left+90,insets.top+100,472,268);}
             }
          }  
      Edited by: 997189 on Mar 31, 2013 8:52 AM
        • 1. Re: To redirect System.out stream output on swing's JTextArea
          UdayaShankaraGandhi
          Hi,

          change your actionPerformed() method as below to get the required output.
          public void actionPerformed(ActionEvent e) 
          {
                  
                    String demodata=datapayload.getText();
                       textFieldName.append("In Action Block"+"\t"+demodata);   
                    //Demo pr = new Demo();      You are creating a new object to all DemoRoute method. This is the problem                         
                    //pr.DemoRoute(demodata); 
                       this.DemoRoute(demodata);
          }
          And also my suggestion is follow java coding conventions while coding