I am triying to highlight a word, but only .length()-2 at 1st time,delay and then last 2 words.The first words are highlighted but it is not highlighting the last two words after delay.please help. here is the code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
public class newkarao {
private int[] timings = {600, 1000, 400,50};
private String[] words = new String[]{"Hellojjkhl", "java", "whoooooh","bye"};
private DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.GREEN);
private int count = 0;
private int xx=0,tlength,spe;
private boolean fisrTime = true;
private JFrame frame;
private JTextPane jtp;
JButton startButton;
public newkarao() {
initComponents();
}
private void initComponents() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
jtp = new JTextPane();
for (String s : words) {
String tmp = jtp.getText();
if (tmp.equals("")) {
jtp.setText(s);
} else {
jtp.setText(tmp + " " + s);
}
}
startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
startKaraoke();
}
});
frame.add(jtp, BorderLayout.CENTER);
frame.add(startButton, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
private void startKaraoke() {
if (fisrTime) {
startButton.setEnabled(false);
fisrTime = false;
}
new Thread(new Runnable() {
@Override
public void run() {
Timer t = null;
try {
t = createAndStartTimer(timings[count], count);
} catch (InterruptedException ex) {
Logger.getLogger(newkarao.class.getName()).log(Level.SEVERE, null, ex);
}
while (t.isRunning()) {//wait for timer to be done
try {
Thread.sleep(1);
} catch (InterruptedException ex) {
Logger.getLogger(newkarao.class.getName()).log(Level.SEVERE, null, ex);
}
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
count++;
if (count == timings.length) {
JOptionPane.showMessageDialog(frame, "Done");
startButton.setEnabled(true);
count = 0;
} else {
startKaraoke();
}
}
});
}
}).start();
}
private Timer createAndStartTimer(int delay, final int count) throws InterruptedException {
try {
int sp = 0;
for (int i = 0; i < count; i++) {
sp += words[i].length() + 1;
spe=sp;
}
// int a=timings[xx];xx++;
System.out.println("jfd");
tlength=words[count].length();//Thread.currentThread().sleep(5000);
jtp.getHighlighter().addHighlight(sp, sp + words[count].length()-2, highlightPainter);
System.out.println(sp + words[count].length()-2);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
Timer t = new Timer(delay, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
try {System.out.println(spe + words[count].length());
jtp.getHighlighter().addHighlight(spe, spe + words[count].length(), highlightPainter);
System.out.println("sleep");
// Thread.currentThread().sleep(5000);
jtp.getHighlighter().removeAllHighlights();
} catch (BadLocationException ex) {
Logger.getLogger(newkarao.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
t.setRepeats(false);
t.start();
return t;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new newkarao();
}
});
}
}
Thread.sleep()
on EDT.Thread.sleep
was called from anotherThread
and not EDT. This can cause many UI problems – BevThread.currentThread().sleep(5000);
inSwing
, for why reason is delayed action fromSwing Timer
and (again) killed byThread.currentThread().sleep(5000);
– Superheterodyne