java setText in loop - java

Hi im trying to setText to JTextArea in loop but I want to do it, thtat in each loop every line will be seen in frame.
I have tryied with Thread.sleep(500), becouse I thought loop is too fast to set each line, but its didnt help.
Is it possible ?? to do it ? I want to do it to show to user progress with downloading files from database.

the nature of event-based singlethreaded guis makes it so that the changes are only visible once the event is fully handled (returned from the event handler)
blocking the event dispatch thread won't help (and even makes the entire app unresponsive)
you should use a timer to simulate the adding one at the time with a delay in between
final String[] lines;
for(int i=0; i<10; i++){
Timer t = new Timer(500*i,new ActionListener(){
int ind=i;
void actionPerformed(ActionEvent e){
area.setText(area.getText() + "\n ...");
}
});
t.start();
}
this creates 10 timers each adding a line after some time (increments of 500)
there's a better way to do this that reuses the timer and stops it after everything is done but it's a bit more verbose

I think JTextArea.append(text) might be more useful.

Related

JTextfield not updating till end of JButton action performed method? Tried using repaint,sleep still no use [duplicate]

I'm creating a board game using a GUI and JFrames/JPanels where you can play against the computer. I have a method called showPieces() which updates board GUI by changing the image icons on an array of buttons (which are laid out in a grid format). Once the icons have been updated the revalidate() and repaint() methods to update the GUI.
The showPieces() method has a parameter that needs to be passed to it every time it is called.
The main issue I'm having is I want the human to make a move, update the GUI, wait 1 second, the computer makes it's move and then loop until someone wins.
My basic code is the following:
do{
human.makeMove();
gui.showPieces(data);
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
computer.makeMove()
gui.showPieces(data);
}while(playing);
This cause the issue where when the human player makes their move, the GUI will freeze for one second and then after the delay, both moves are made at the same time.
I hope it makes sense, but I'm a novice with Java and may have to look more into threading as I don't understand it well enough.
Thread.sleep() is done on the Event Dispatch Thread which will lock the GUI.
So If you need to wait for a specific amount of time, don't sleep in the event dispatch thread. Instead, use a timer.
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
}
};
new Timer(delay, taskPerformer).start();
As with most all similar Swing questions, you're putting your entire Swing GUI to sleep by calling Thread.sleep(...) on the GUI's event thread (the EDT or Event Dispatch Thread), and when during this period the GUI will not be able to update its images or interact with the user whatsoever. The solution here is not to use Thread.sleep(...) but rather to use a Swing Timer to cause your 1 second delay.
Swing Timer Tutorial.

Java JFrame: Update GUI during an event (not within a timer)

Been trying at this a little while, going no where fast. I have elements in a JFrame that I want to update/toggle viability for when I click my play button (which triggers the ActionEvent). However, these only seem to be updating after the actionPerformed method is finished. This is what I think is happening because the constructor for my SoundPlayer object triggers (Thread.sleep)s, causing a noticeable delay. I did read here that using Thread.sleep() locks up a GUI, but I make my changes before calling SoundPlayer, so I don't think that would be the issue.
As seen below, I've tried repainting the JFrame in general, as well as the individual element. These do not update until after the SoundPlayer's delay is finished, though my println statement prints a the correct time.
MultiThreading has crossed my mind as a solution, though I don't see why I would need to do so. Any help on this matter is appreciated!
public void actionPerformed(ActionEvent e){
int channel = 0, volume = 0; //Assigned for safety.
String musicNotes = ""; //Will be filled with the under-input.
boolean willPlay = true; //Assumes will be played.
/*Stuff that makes 'willPlay' either true of false*/
//If nothing is wrong, plays the String!
if (willPlay) { //If above parameters are good...
badNums.setVisible(false);
prog.setVisible(true);
if (vis.isSelected())
prog.setText("Estimated duration: " + estiDuration(musicNotes)*(0.4) + "seconds");
else
prog.setText("Duration: " + estiDuration(musicNotes)*(0.3) + "seconds");
System.out.println("test");
repaint();
prog.repaint();
new SoundPlayer(channel, volume, musicNotes); //Plays the music!
} else {
vis.setVisible(false);
badNums.setVisible(true);
}
}
Swing is single threaded - all code for painting, events, etc... runs on this Thread (called the EDT). If you have a long running task and place it on the EDT, it can do nothing else until it is complete (note that calling repaint doesn't directly repaint the Component, so running something lengthy after this call does not mean the Component will actually paint itself before). If you need to perform lengthy operations, do so in a different Thread - this can be accomplished either directly using a Thread, or using a SwingWorker. If the former, be sure any calls to Swing get dispatched to the EDT using SwingUtilities.invokeXXX

Timer Schedule - Java

new java.util.Timer().scheduleAtFixedRate(timeleft(), 0, 1000);
This is my code for a method timeleft() that i want to happen every second.
My class is called Timer.
Not sure why I'm getting an error.
It says "void type not allowed here" when I hover over the line.
I am using a JFrame for a GUI and put this inside an event when I press a button along with a timer that counts down from whatever number you enter in a text field.
You state:
I am using a JFrame for a GUI and put this inside an event when I press a button along with a timer that counts down from whatever number you enter in a text field.
I'm going to recommend that you consider throwing out the code that you're asking your question on, to completely re-think your approach.
If you want to create and run a repeated event in a Swing GUI, don't use a java.util.Timer as you're doing, since you can easily run into serious Swing threading issues. Instead use a Swing Timer, a Timer that works well with Swing, since all code in the Timer's ActionListener is guaranteed to run on the Swing event thread.
To start you out, your code could look something like:
int timerDelay = 1000; // 1000 msecs or 1 second
Timer timer = new Timer(timerDelay, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// code that needs to be repeated goes here
}
});
timer.setInitialDelay(0);
timer.start();
Although for a count-down timer, I'm not sure that I'd set the initial delay to 0. Instead, I'd let it wait the period.
As you can see from the javadoc, the scheduleAtFixedRate method accepts a TimerTask as a first argument. You seem to be trying to pass the result of a method, named timeleft(), with a void return type.
If you need to execute some code at a fixed rate, then you need to create a TimerTask object with an appropriate run implementation and pass that to scheduleAtFixedRate.

Java-Swing : The ImageIcon cannot change in runtime?

I have this code in java
private void buttonShowImageActionPerformed(java.awt.event.ActionEvent evt)
{
if(folderFiles != null)
{
for(int i=0; i<folderFiles.size(); i++)
{
icon = new ImageIcon(folderFiles.get(i));
labelImage.setIcon(icon);
timeDelay(2); // A method that delays 2 secs - it works
}
}
}
When I push the button, it waits some time, and from the 1st goes to the last image, and skips the images from the folder one-by-one
The delay method works (I tested it)
Thanks in advance !
Regarding:
timeDelay(2); // A method that delays 2 secs - it works
No, it doesn't work, regardless of how you tested it. Swing doesn't work that way, and you would do well to avoid making such assumptions. Use a Swing Timer.
You will ask, how do I know that it doesn't work, and I'll tell you: that code does not call a background thread, nor does it start a Swing Timer, so the only thing it can do is delay the current thread, likely somewhere with a Thread.sleep(...). If you call this and "test" this, yes, it will cause delays that will show up in your System.out.println(...) statements to the console, but it will also sleep the Swing event thread, and put your application to sleep. So you really don't want to do this.
And in fact the best test to see if this works in Swing is your current code. What happens then? You state:
When I push the button, it waits some time, and from the 1st goes to the last image, and skips the images from the folder one-by-one
So in fact you know for a fact that your delay doesn't work for Swing, as you describe the classic symptoms of code that stomps on the Swing event thread, bringing it to its knees. So again, use a Swing Timer. Here's a link to the Swing Timer Tutorial.
If this were my code, I'd not read in the images each time the button is pushed, but instead read them all at once, and one time only and put them in an ArrayList<ImageIcon> say called iconList. Assuming that you did this, then the code could look like:
private void buttonShowImageActionPerformed(java.awt.event.ActionEvent evt) {
// !! I'd use a variable or constant instead of the magic number 2000
new Timer(2000, new ActionListener() {
int count = 0;
actionPerformed(ActionEvent e) {
if (count != iconList.size()) {
labelImage.setIcon(iconList.get(count));
count++;
} else {
// stop the Timer
((Timer) e.getSource()).stop();
}
}
}).start();
}
Edit
You ask:
My friend one last question ..... I put the "int count = 0;" inside the actionPerformed and nothing happend ... I cannot understand why it works only if it is outside the method .... ?
Please understand that the way a Swing Timer works is that the actionPerformed method is called repeatedly, here every 2000 seconds. What my code does is that when the timer is started, the count is set to 0. Each time the actionPerformed method is called, count increments by 1, and the next ImageIcon is displayed.
With your code, when the actionPerformed method is called, the count is re-set to 0, the first image is displayed, and the count is then incremented. But each time the actionPerformed method is called, your code resets the count back to 0, so the incrementation has no effect.

Slowness in a simple animation using the Timer class

A small app containing a BufferedImage uses the Timer class for animation. Everything works, but not as expected.
Sometimes the system struggles to draw the animation, as though it is draining system resources (especially after installing the JRE), which makes the animation redraw at abnormally low speeds. Other times it works as expected.
What could cause this?
Thank you!
Switch to java.util.Timer, javax.swing.Timer seems to be better suited to actions that are called every minute or second as opposed to java.util.Timer which is far better adapted to tens of calls per second.
Sorry everyone..my bad!
The class I am using is javax.swing.Timer!
Here is part of the code (timer implementation -- not the method timer calls):
Timer t = new javax.swing.Timer(10, new ActionListener(){
public void actionPerformed(ActionEvent ev){
//This little if/step statement actually decides if
//the ellipses I have on screen will be colored all together
//or step by step
if(app.flagStep == false){
for (int i = 0; i<list.size(); i++){
drawBuffer(i);
}
}
else{
if(app.stepBut.isEnabled()){
app.stepBut.setEnabled(false);
}
drawBuffer(app.myStep);
}
}
});

Categories