//use inset
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
class jt extends JFrame implements ActionListener
{
JTextArea chatText;
JButton jb;
public jt()
{
super("jt");
jb = new JButton("click");
JPanel p = new JPanel(new BorderLayout());
chatText = new JTextArea("one\ntwo\nthreeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"+
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",10, 30);
chatText.setMargin(new Insets(50,50,50,50));
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_NEVER );
chatTextPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JViewport vp = chatTextPane.getViewport();
vp.add(chatText);
p.add(chatTextPane, BorderLayout.CENTER);
getContentPane().add(p,BorderLayout.CENTER);
getContentPane().add(jb,BorderLayout.SOUTH);
try {
System.out.println(chatText.getLineStartOffset(2));
System.out.println(chatText.getLineEndOffset(2));
System.out.println( (chatText.getText()).substring(chatText.getLineStartOffset(2),chatText.getLineEndOffset(2)) );
}
catch (BadLocationException be)
{System.out.println("invalid line no");}
jb.addActionListener(this);
}
public boolean isManagingFocus() // all your JComponents should have this method overidden like this if you want to manage focus yourself
{
return true;
}
public void actionPerformed(ActionEvent event)
{
try {
System.out.println(chatText.getLineStartOffset(2));
System.out.println(chatText.getLineEndOffset(2));
System.out.println( (chatText.getText()).substring(chatText.getLineStartOffset(2),chatText.getLineEndOffset(2)) ); // get third line by finding the second new line character
}
catch (BadLocationException be)
{System.out.println("invalid line no");}
}
public static void main(String args[])
{
jt jtObj = new jt();
jtObj.setSize(400,400);
jtObj.setVisible(true);
}
}
//
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.