0

I've looked many place and can't find out how to refresh automatically my frame. It does work (since i've used many examples already there) when I click buttons, but not automatically.

I want to create a MineSweeper game, and I am adding the Timer in it, everything works except the timer counts, but only updates when a button is clicked. Therefore, simply redoing the layout won't work...

Tough I know my timer works, if I put a System.out in it, i see it at stable second rate.

I will put below a little program, not my game since the code is too long, but a little one I built that is about the same circumstances, fixing it will fix my problem overall.

In this case, i don't have the well developed button from my other program, but the time will only refresh when i'm messing with the window.

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ClockTest {
JFrame frame = new JFrame();
JButton test1 = new JButton();
JTextField timer1 = new JTextField();
int hour = 0, min = 0, sec = 0;
String timing;

public ClockTest() {
    frame.setLayout(new BorderLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(center(), BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
}

public Component center() {
    JPanel pan = new JPanel();
    pan.setLayout(new GridLayout(1,2));
    test1.setText("Dummy");
    timer1.setEditable(false);
    timer1.setText(timing);
    pan.add(test1);
    pan.add(timer1);
    return pan;
}

public void count() {
    sec++;
    if(sec == 60) {
        sec -= 60;
        min++;
    }
    if(min == 60) {
        min -= 60;
        hour++;
    }
    timing = hour + ":" + min + " : " + sec;
//This is what i do in my game to refresh, to set flags, get mine "exploded"...
//But since i'm not pressing anything... it does not happen.
    frame.add(center(), BorderLayout.CENTER);
}

public static void main(String[]args) {
    final   ClockTest test1 = new ClockTest();
    TimerTask task = new TimerTask() {
        public void run() {
            test1.count();
        }
    };
    Timer t = new Timer();
    t.schedule(task,0,1000);
}
}
1

2 Answers 2

1

JPanel in puzzle game not updating

After changing the components, you need to 'refresh' the Swing component by calling invalidate() or revalidate()

3
  • Well, it won't work like that for few reasons. 1. this is in main, therefore it needs to be called properly, test1.pan.updateUI(); 2. my pan is not called by the class but by my center() function, therefore can't be called that way. Tough, i found that simply adding a frame.setVisible(true); after the change made the update, tough it is not that fluid if there are other updates for the same frame. Is it possible to update somehow more precisely that particular component? without messing with the rest of the frame??? Why can't i press on the button?
    – Draeven
    Apr 7, 2013 at 12:10
  • i found this link i think it could help stackoverflow.com/questions/3078178/… "After changing the components, you need to 'refresh' the Swing component by calling invalidate() or revalidate()" Apr 7, 2013 at 17:57
  • Okay, i tried revalidate(), it has to be done to every component individually, so it doesn't messes with the other ones, GREAT!!! I added in count() (which is the task in my timer loop) the timer1.setText(timing), then timer1.revalidate();, now works like a charm, THANK YOU!!! WORK COMPLETE!!!
    – Draeven
    Apr 8, 2013 at 20:10
0

Here is the answer with JavaNewb idea to use revalidate();

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class ClockTest {
JFrame frame = new JFrame();
JButton test1 = new JButton();
JTextField timer1 = new JTextField();
int hour = 0, min = 0, sec = 0;
String timing;

public ClockTest() {
    frame.setLayout(new BorderLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(center(), BorderLayout.CENTER);
    frame.setResizable(true);
    frame.pack();
    frame.setVisible(true);
}

public JPanel center() {
    JPanel pan = new JPanel();
    pan.setLayout(new GridLayout(1,2));
    test1.setText("Dummy");
    timer1.setEditable(false);
    timer1.setText(timing);
    pan.add(test1);
    pan.add(timer1);
    return pan;
}

public void count() {
    sec++;
    if(sec == 60) {
        sec -= 60;
        min++;
    }
    if(min == 60) {
        min -= 60;
        hour++;
    }
    timing = " " + hour + ":" + min + " : " + sec;
// Make the modification here on your components, then .revalidate(); them all
    timer1.setText(timing);
    timer1.revalidate();
}
public static void main(String[]args) {
    final   ClockTest test1 = new ClockTest();
    TimerTask task = new TimerTask() {
        public void run() { 
            test1.count();
        }
    };
    Timer t = new Timer();
    t.schedule(task,0,1000);
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.