public class OneLineJTextArea extends JTextArea{
private static final long serialVersionUID = 1L;
Integer index = 0;
List<Integer> nList = new ArrayList<>();
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
if(nList.size()>0){
if(index % nList.size() == 0 && index != 0){
index = 0;
}
try {
setCaretPosition(nList.get(index++));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
};
public OneLineJTextArea(){
timer.schedule(task, 0, 2000);
}
public void startDisplay(){
nList = getNs(getText());
index = 0;
}
public void setText(String content){
super.setText(content);
this.setCaretPosition(0);
}
private List<Integer> getNs(String str) {
List<Integer> list = new ArrayList<>();
String[] buffs = str.split("\n");
int count = 0;
for(String s:buffs){
count +=s.length();
if(count>0){
list.add(count);
}else{
list.add(count);
}
}
return list;
}
}
private static final long serialVersionUID = 1L;
Integer index = 0;
List<Integer> nList = new ArrayList<>();
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
if(nList.size()>0){
if(index % nList.size() == 0 && index != 0){
index = 0;
}
try {
setCaretPosition(nList.get(index++));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
};
public OneLineJTextArea(){
timer.schedule(task, 0, 2000);
}
public void startDisplay(){
nList = getNs(getText());
index = 0;
}
public void setText(String content){
super.setText(content);
this.setCaretPosition(0);
}
private List<Integer> getNs(String str) {
List<Integer> list = new ArrayList<>();
String[] buffs = str.split("\n");
int count = 0;
for(String s:buffs){
count +=s.length();
if(count>0){
list.add(count);
}else{
list.add(count);
}
}
return list;
}
}