2

I face a problem in my java application that, when new jFrames open by clicking jButton, litte-bit freeze and after its opening(freeze time 1-2 minutes/3 minutes). I couldn't find yet whats wrong going on. but I have some doubts of below attached code. that code for taking system time and date and show all jFrames. so this code is in all jFrames. now my question is, is this freeze happening by this code..? or may any other reasons..? if this code have any wrongs plz tell me that also... I'm using NEtbeans 8.2. thanks in advance.

code:

public AdminHome() {
    initComponents();

    new Thread(new Runnable() {
        @Override
        public void run() {

            while (true) {
            Date d=new Date();

            SimpleDateFormat sd=new SimpleDateFormat("yyyy - MM - dd");
            String s =  sd.format(d);
            String s1 = d.toString();
            String ar[]=s1.split(" ");

            jLbl_Date.setText(s);  
            jLbl_Time.setText(ar[3]);
            }  
        }
    }).start();

}
siper camm
  • 77
  • 1
  • 8

3 Answers3

2

These two calls:

jLbl_Date.setText(s);  
jLbl_Time.setText(ar[3]);

have to happen on the EDT (Event Dispatch Thread) since GUI components have to be manipulated from the EDT. You could put them on the EDT by wrapping them using SwingUtilities:

SwingUtilities.invokeLater(() -> {
    Date d=new Date();

    SimpleDateFormat sd=new SimpleDateFormat("yyyy - MM - dd");
    String s =  sd.format(d);
    String s1 = d.toString();
    String ar[]=s1.split(" ");

    jLbl_Date.setText(s);  
    jLbl_Time.setText(ar[3]);
});

However, there would be still a problem. Since your thread doesn't sleep between updating the labels, you would flood the EDT with update requests causing your GUI again to freeze. You could fix this by adding a Thread.sleep(1000); after updating the labels.

A more elegant approach is to use a swing timer instead of your thread:

Timer timer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Date d=new Date();

            SimpleDateFormat sd=new SimpleDateFormat("yyyy - MM - dd");
            String s =  sd.format(d);
            String s1 = d.toString();
            String ar[]=s1.split(" ");

            jLbl_Date.setText(s);  
            jLbl_Time.setText(ar[3]);
        }
});            
timer.setInitialDelay(0);
timer.start();

A swing timer takes care that the code withing the actionPerformed-method is executed on the EDT. It has the additional advanatage that it coaleceses the events if needed - another mechanic to prevent the events from flooding the EDT.

Calculator
  • 2,769
  • 1
  • 13
  • 18
0

You may have created a separate thread, but all the UI updation roots down to the AWT thread. Hence calling jLbl_date.setText() and jLbl_time.setText() methods very frequently by that thread are actually blocking the AWT thread directly.

Try adding a sleep(1000) after jLbl_Time.setText().

Subhranil
  • 851
  • 1
  • 8
  • 23
  • Also the `setText` calls should be `SwingUtilities.InvokeLater`ed. – Colin Feb 22 '17 at 15:02
  • Ya it should, correct me if I'm wrong, but AWT doesn't restrict multithreaded UI access like JavaFX. – Subhranil Feb 22 '17 at 15:06
  • @Subhranil, tnx for ur answer. I tried what you said now. but ita showing error. please can you modifying my code and comment here...? its so usefull to me. – siper camm Feb 22 '17 at 15:09
  • JLabel is Swing, and should not be modified by any thread other than the event dispatch thread. – Colin Feb 22 '17 at 15:09
  • Hello @Colin__s, you said answer i didn't get it. please can you explain with modifying my code...? its more helpful to me. I'm just a beginner in java... – siper camm Feb 22 '17 at 15:11
  • @sipercamm there's nothing called an "error". Paste the exception. – Subhranil Feb 22 '17 at 15:13
  • I know and I get that @Colin__s, I'm just wondering if it would raise any exception. – Subhranil Feb 22 '17 at 15:14
  • @Subhranil, here: Exception in thread "Thread-2" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: sleep at view.AdminStudent$1.run(AdminStudent.java:55) at java.lang.Thread.run(Thread.java:745) – siper camm Feb 22 '17 at 15:15
  • No, as far as I know it wouldn't throw an exception. – Colin Feb 22 '17 at 15:15
  • Meanwhile it is a problem of netbeans. http://stackoverflow.com/questions/2333285/java-lang-runtimeexception-uncompilable-source-code-what-can-cause-this – Subhranil Feb 22 '17 at 17:28
0

It looks like you have created a thread to run an infinite loop to update some date and time fields. This is not a very good way to achieve what you want to do.

A better solution would be to use a javax.swing.Timer with a shortish interval and update your UI from an attached action listener.

ActionListener timerListener = new ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        Date d=new Date();

        SimpleDateFormat sd=new SimpleDateFormat("yyyy - MM - dd");
        String s =  sd.format(d);
        String s1 = d.toString();
        String ar[]=s1.split(" ");

        jLbl_Date.setText(s);  
        jLbl_Time.setText(ar[3]);
    }
};

Timer t = new javax.swing.timer(1000, timerListener).start();

Something like the above should do the trick. This saves you the hassle of crossing thread boundries to update the UI, and will reduce cpu load quite substantially from your previous solution.

Colin
  • 3,394
  • 1
  • 21
  • 29
  • I added ur code. but got erro and this jframe is not run as well... its say "does not have main method" here attached way your code in my code: – siper camm Feb 22 '17 at 15:26
  • public AdminStudent() { initComponents(); ActionListener timerListener = new ActionListener { public void actionPerformed(ActionEvent e) { Date d=new Date(); SimpleDateFormat sd=new SimpleDateFormat("yyyy - MM - dd"); String s = sd.format(d); String s1 = d.toString(); String ar[]=s1.split(" "); jLbl_Date.setText(s); jLbl_Time.setText(ar[3]); } } Timer t = new javax.swing.timer(1000, timerListener).start(); } – siper camm Feb 22 '17 at 15:27
  • I missed a `;` off the end of the action listener – Colin Feb 22 '17 at 15:29