🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #8908

Setting the Focus ina JTextArea

#8908
From: Thomas, Lionel SMETS
Date: Sun, 25 Jun 2000 00:00
118 lines
3555 bytes
I'm appending String to a JTextArea.
Now the problem is very simple the first line never moves
up to have the
new added line in the viewable area of the JTextArea.
Could some one give me a hint ?

Tx,

thomas,

<code>
package dbg.tst_swing;

import javax.swing.*;
import javax.swing.text.* ;
import java.awt.*;
import java.awt.event.*;

public class TextPanel extends JPanel {
  JTextField textField = null;
  JTextArea textArea = null;
  JTextPane textPane = null;
  public TextPanel() {
    // Set the layout to a BorderLayout
    setLayout(new BorderLayout());

    // Create the three basic text components
    textField = new JTextField();
    textArea = new JTextArea();
    textPane = new JTextPane();

    //Set the textpane's font properties
    MutableAttributeSet attr = new SimpleAttributeSet();
    StyleConstants.setFontFamily(attr, "Serif");
    StyleConstants.setFontSize(attr, 18);
    StyleConstants.setBold(attr, true);
    textPane.setCharacterAttributes(attr, false);

    add(textField, BorderLayout.NORTH);
    add(new JScrollPane(textArea), BorderLayout.CENTER);
    add(new JScrollPane(textPane), BorderLayout.SOUTH);
  }

  public static void main(String[] args)
      throws IllegalAccessException,
ClassNotFoundException,
                InstantiationException,
UnsupportedLookAndFeelException,
                    InterruptedException
  {
     String[] s = {
      "aa",      "bb",      "cc",      "dd",      "ee",
"ff",
      "gg",      "ff",      "gg",      "hh",      "ii",
"jj",
      "kk",      "ll",      "mm",      "nn",      "oo",
"pp",
      "qq",      "rr",      "ss",      "tt",      "uu",
"vv",
      "ww",      "xx",      "yy",      "zz"      };
    int _SLEEP_TIME_ = 750;


    JFrame f = null;
   

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

    //Create the top-level container and add contents to
it.
    f = new JFrame("Test");
    //ToolBox.setNonDefaultsValues(this);
    TextPanel tp = new TextPanel();
    f.getContentPane().add( tp, BorderLayout.CENTER);
    f.addWindowListener(
      new WindowAdapter()
      {
        public void windowClosing(WindowEvent e)
        {
          System.exit(0);
        }
      });

    f.pack();
    f.setVisible(true);

    try
    {
      for ( int i = 0;  ; i++) {
        tp.textArea.append("\n" + s[i]);
        System.out.println("s[" + i + "] = " + s[i]);
        // Rest focus to the TextArea
        
        Thread.sleep(_SLEEP_TIME_);
        if ( i<5 )
          f.pack();

      }
    } catch (ArrayIndexOutOfBoundsException AIOOBEe)
    {
    }

  }
}

</code>


-- 
Thomas, Lionel SMETS
Av. de la Brabançonne 133 / 3
1030 Bruxelles
Belgium
e-mail : tsmets@altern.org
Technical Details
Message-ID: <39567CF6.7F2FBEFC@altern.org>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 111d45,5ec2cddb7dc4486b,start
X-Google-Attributes: gid111d45,public
From: "Thomas, Lionel SMETS" <tsmets@altern.org>
Subject: Setting the Focus ina JTextArea
Date: 2000/06/25
Message-ID: <39567CF6.7F2FBEFC@altern.org>
X-Deja-AN: 638873166
Content-Transfer-Encoding: quoted-printable
X-Accept-Language: en
Content-Type: text/plain; charset=iso-8859-1
X-Complaints-To: abuse@chello.be
X-Trace: news.chello.be 961969193 213.132.132.245 (Sun, 25 Jun 2000 23:39:53 MET DST)
Organization: Chello Usenet Service
MIME-Version: 1.0
NNTP-Posting-Date: Sun, 25 Jun 2000 23:39:53 MET DST
Newsgroups: borland.public.jbuilder.java.swing