import java.awt.Color;
import java.awt.Dimension;
import java.io.FileReader;
import java.io.IOException;
import java.util.Timer;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField tf;
public Main() {
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
toFront();
// setzt die Position des Jrames
setLocation(550, 80);
// nimmt min. max. close weg
setUndecorated(true);
// JTextArea textArea = new JTextArea();
tf = new JTextField();
// kannst du nicht bearbeiten
tf.setEditable(false);
JScrollPane scrollPane = new JScrollPane(tf);
scrollPane.setPreferredSize(new Dimension(230, 50));
add(scrollPane);
pack();
setVisible(true);
tf.setCursor(null);
tf.setBackground(Color.red);
tf.setForeground(Color.green);
tf.setCaretPosition(tf.getText().length());
tf.setHighlighter(null);
new Timer().schedule(new TextMarqueeTimer(tf), 0L, 30L);
}
public String readLine(FileReader reader) throws IOException {
String line = null;
for (int readInt = 0; (readInt = reader.read()) != 13 && readInt != -1;) {
String s = String.valueOf((char) readInt);
if (line == null)
line = s;
else
line += s;
}
reader.read();
return (line);
}
public static void main(String[] args) {
new Main();
}
}
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.TimerTask;
class CheckFileContentTimer extends TimerTask {
private static final File FILE = new File("C:\\loi\\data.txt");
private long lastMod;
private TextMarqueeTimer textMarqueeTimer;
CheckFileContentTimer(TextMarqueeTimer textMarqueeTimer) {
super();
this.textMarqueeTimer = textMarqueeTimer;
}
public void run() {
while(true) {
try {
long atmLastMod = FILE.lastModified();
if(atmLastMod > lastMod) {
FileReader fileReader = new FileReader(FILE);
StringBuilder stringBuilder = new StringBuilder();
for (String incomingLine = null; !(incomingLine = readLine(fileReader)).equals(".");) {
stringBuilder.append(incomingLine.concat(" "));
}
fileReader.close();
lastMod = atmLastMod;
textMarqueeTimer.setNextText(stringBuilder.toString());
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public String readLine(FileReader reader) throws IOException {
StringBuilder builder = new StringBuilder();
for (int readInt = 0; (readInt = reader.read()) != 13 && readInt != -1;) {
builder.append((char) readInt);
}
reader.read();
return (builder.toString());
}
}
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JTextField;
class TextMarqueeTimer extends TimerTask {
private String atmText = "";
private String nextText = "";
private JTextField textField;
TextMarqueeTimer(JTextField textField) {
new Timer().schedule(new CheckFileContentTimer(this), 0L, 1000L);
this.textField = textField;
}
public void run() {
if(atmText != null && nextText != null) {
if(!atmText.equals(nextText)) {
atmText = nextText;
textField.setText(atmText);
} else {
if(atmText.length() > 0) {
String changedText = textField.getText();
String withoutFirstChar = changedText.substring(1, changedText.length());
char firstChar = changedText.charAt(0);
String newText = withoutFirstChar.concat(String.valueOf(firstChar));
textField.setText(newText);
}
}
}
}
public void setNextText(String nextText) {
this.nextText = nextText;
}
}