sending output to JTextArea


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: sending output to JTextArea

  1. #1
    Join Date
    Nov 2004
    Posts
    7

    sending output to JTextArea

    Hi,two questions
    Since I am writting an application ,need a place to send the output there to check the result and also scroll.I was trying to use a JTextArea object (some methods likebut append and insert)but it did not work because it does not append the outputs vertically.I need something like println which can send the output to it but able to scroll (vertically) there.
    Does anybody know about a free JAVA debugger?

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Here is a class that uses a TA the way u want. Note that i have declared the texarea public static . That way this is accessible all over the application like: TAFrame.outputTA.append("\r\nNew line in my report");

    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    
    public class TAFrame extends JFrame {
      BorderLayout bLay1 = new BorderLayout();
      JScrollPane textScroll = new JScrollPane();
      public static JTextArea outputTA = new JTextArea(); // <<<
      JPanel btnPanel = new JPanel();
      JButton closeBtn = new JButton();
      JButton clearBtn = new JButton();
    
      public TAFrame() {
        initialize();
      }
      public static void main(String[] args) {
        TAFrame tf = new TAFrame();
        tf.setBounds(20,20,400,250);
        tf.setVisible(true);
      }
      private void initialize() {
        this.getContentPane().setLayout(bLay1);
        outputTA.setToolTipText("This is my output");
        outputTA.setLineWrap(true); // <--------------- here
        outputTA.setWrapStyleWord(true);
        closeBtn.setText("Close");
        clearBtn.setText("Clear");
        this.getContentPane().add(textScroll, BorderLayout.CENTER);
        this.getContentPane().add(btnPanel, BorderLayout.SOUTH);
        btnPanel.add(closeBtn, null);
        btnPanel.add(clearBtn, null);
        textScroll.getViewport().add(outputTA, null);
        closeBtn.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            closeBtn_actionPerformed(e);
          }
        });
        clearBtn.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            clearBtn_actionPerformed(e);
          }
        });
      }
    
      void closeBtn_actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    
      void clearBtn_actionPerformed(ActionEvent e) {
        this.outputTA.setText("");
      }
    }
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center