Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JTextArea focus

Status
Not open for further replies.

GuzaPasha

Technical User
May 15, 2003
311
FR
I am not able to get focus to the last line displayed in JTextArea, when I append to much lines in it. Just few of the first lines appeared. I can see the last lines by using scroll.

I'm using:

chatText = new JTextArea(10, 30);
chatText.setLineWrap(true);
chatText.setEditable(false);
chatText.setForeground(Color.white);
chatText.setBackground(new Color(80,0,0));
chatText.isManagingFocus();

What do I miss? and how to solve it.

Thanks
GuzaPasha

 
Code:
//use JScrollPane for JTextArea
// the following is part of code, copy the part you need
import java.awt.*; 
import java.awt.event.*;
import javax.swing.*; 
import javax.swing.event.*; 
import javax.swing.text.*; 

final int JT_COL = 50;
final int JT_ROW = 40;
JTextArea JT;
    JT = new JTextArea("oneone\ntwoone\nthree",JT_ROW,JT_COL);   
            JScrollPane scroller = new JScrollPane(); 
            scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            JViewport vp = scroller.getViewport(); 
            vp.add(JT); 
    add(scroller);
 
still nothing, here is my code + your code
Code:
JPanel P = new JPanel(new BorderLayout());
      chatText = new JTextArea(10, 30);
      chatText.setLineWrap(true);
      chatText.setEditable(false);
      chatText.setForeground(Color.white);
      chatText.setBackground(new Color(80,0,0));
      chatText.isManagingFocus();

      /*JScrollPane chatTextPane = new JScrollPane(chatText,
         JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
         JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);*/
//your code from here
      JScrollPane chatTextPane = new JScrollPane();
      chatTextPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      chatTextPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      JViewport vp = chatTextPane.getViewport(); 
      vp.add(chatText); 
//..      
P.add(chatTextPane, BorderLayout.CENTER);
//..
Please look at this.

GuzaPasha


 
Code:
import java.awt.*;
import javax.swing.*;
class jt extends JFrame
      {
       public jt()
              {
               super("jt");
               JPanel p = new JPanel(new BorderLayout());
               JTextArea chatText = new JTextArea("one\ntwo\nthree",10, 30);
               chatText.setLineWrap(true);
               chatText.setEditable(true);
               chatText.setForeground(Color.white);
               chatText.setBackground(new Color(80,0,0));

               JScrollPane chatTextPane = new JScrollPane();
               chatTextPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
               chatTextPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
               JViewport vp = chatTextPane.getViewport(); 
               vp.add(chatText); 
               p.add(chatTextPane, BorderLayout.CENTER);
               getContentPane().add(p,BorderLayout.CENTER);
              }
       public boolean isManagingFocus() // all your JComponents should have this method overidden like this if you want to manage focus yourself
              {
               return true;
              }
       public static void main(String args[])
              {
               jt jtObj = new jt();
               jtObj.setSize(400,400);
               jtObj.setVisible(true);
              }
      }
 
Could you please put this into your code
for (i=1;i<50;i++){
chatText.append("line: " + i + "\n");
}

as you can see there is no focus. Maybe the focus is the wrong terminology, but here is exactly what I need.
Let's suppose that the JtextAre has 10 lines in height.
for (i=1;i<50;i++){
chatText.append("line: " + i + "\n");
}
will put the 50 lines of text in it. Scroll will not follow last line received, and will stay showing the first 10 lines. If I want to see the last line, i will have to scroll down manualy. I don't want this. I want the scroll is following the last line received. Is there a way that I can receive text in JTextArea always in the first line, and to scroll down previosuly received text.

Any idea?
guzapasha

 
// If you want to show the last 10 lines
// you should process the text in the JTextArea, store them into an string array with size of 10
// set JTextArea blank
// add 10 lines to JTextArea
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top