2

I am new to programming (I'm 11 and hoping for java coding to be my career, but its just a hobby right now :)) and I just made a countdown program, here is the class:

package me.NoahCagle.JAVA;

import javax.swing.JFrame;

public class Main extends JFrame implements Runnable {
private static final long serialVersionUID = 1L;
public static int width = 600;
public static int height = 500;
public static String title = "Countdown!";
public static boolean running = false;
public int number = 11;
public Thread thread;
Dimension size = new Dimension(width, height);

public Main() {
    super(title);
    setSize(size);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
}

public static void main(String[] args) {
    Main m = new Main();
    m.start();
}

public void start() {
    if (running) {
        return;
    }

    running = true;
    Thread thread = new Thread(this);
    thread.start();

}

@SuppressWarnings("static-access")
public void run() {
    while (running) {
        number--;
        if (number == -1) {
            System.out.println("Done!");
            System.exit(0);
        }
        try {
        thread.sleep(1000);
        }catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
        System.out.println("" + number);
    }
}

public void stop() {
    if (!running) {
        return;
    }

    running = false;
    try {
        thread.join();
    }catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
        }
    }
}

That may not have been necessary, but whatever. Well like I was saying, if you read the code, you will notice that it prints the value to the console. Well, if I could get that to display on a JLabel, while updating at the same time. I have tried just doing setText("" + number) thinking that because I have a thread going, it would repaint. But that didn't happen. It was just stuck at 11. Can someone please help me? Thanks

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Noah Cagle
  • 146
  • 1
  • 4
  • 15
  • There is an excellent [answer elsewhere on SO](http://stackoverflow.com/a/2477202/1523342). – mthmulders Jul 25 '13 at 07:40
  • When I was 11 years old I didn't know how to type on keyboards. – Maroun Jul 25 '13 at 07:53
  • I can see some flaws in your code, though two quick things, the variable `running` should be declared with the `keyword volatile`, as its value is not updated by the code in the scrutiny of the while loop, the value is coming from some place else. Secondly, IMHO, I think package names should be all in small letters. – nIcE cOw Jul 25 '13 at 09:05

2 Answers2

4

First, you may want to take a read through Concurrency in Swing. There are some very important constraints when it comes to dealing with multiple threads and Swing.

For your problem, you really should be using a javax.swing.Timer, and with examples...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-1

As 11yrs old you have done good job here. But where did you add any panel to the frame on which u want to show the number? Once you do it and put some label to add the number you will need to call the repaint method. Also to use threads with swings, there are many libraries you can use like Timer.

Happy Coding!

Sandiip Patil
  • 456
  • 1
  • 4
  • 21
  • this is about googling and then to use 1st. of code examples, and wrong, nothing to do with age – mKorbel Jul 25 '13 at 08:37
  • Even googling and putting it right place and removing compile time errors needs effort. I applauded the kid for his efforts. I dont understand how it makes my answer go for negative. – Sandiip Patil Jul 25 '13 at 08:54
  • 1
    Upvoting the answer for one reason, no reasons specified for the down vote :-) Though, I still think that the answer can be made a bit better by simply providing one working example, of what needs to be done. – nIcE cOw Jul 25 '13 at 09:08
  • @nIcEcOw Thanks. I answered based on what I understood and thought needs some work in the question itself. But still have no idea why it went for downvote, cause I said its good code for 11yrs old??!! – Sandiip Patil Jul 25 '13 at 09:12
  • @mKorbel will you like to elaborate with example for your comment, if you think you can write better drama? – Sandiip Patil Jul 25 '13 at 09:14
  • 1
    @SandiipPatil : questioning someone's ability is not he essence of the site. Though if you will check his answers, already posted, you will find plenty of them, that will add something new to the knowledge everyday. – nIcE cOw Jul 25 '13 at 09:23
  • @nIcEcOw I am actually amazed by his abilities, as you can see there is one more very true comment below the question that at the age of 11 we dint know how to type on keyboard. And I actually meant the same. But I am taken otherwise. Sad :(. And I asked question about label and panel which he mentioned in code that wants to updated. If check carefully I suggested him the write one. mKorbel: No comments for your intelligence. – Sandiip Patil Jul 25 '13 at 09:25
  • see some peacefull_face to upvoted, reasons are different, I'm describle my view about, @nIcE cOw +++ – mKorbel Jul 25 '13 at 09:26
  • -1 (mainly, the noise around the real advice isn't very helpful, but that alone wouldn't have made it) because your answer is technically incorrect a) no need to call repaint after updating a property of the label b) it's swing (not swings) c) Timer is not a library but a cclass of core swing - yeah, I'm pedantic but imprecise answers (unfortunately far too frequent here) are not helpful for anybody ... – kleopatra Jul 25 '13 at 09:36
  • @nIcEcOw now that you know the reasons for both downvotes, you could remove your upvote ;-) – kleopatra Jul 25 '13 at 09:38
  • 2
    @kleopatra : Taking my vote back will be like, putting a question mark on the intent of the poster. As I first interpreted the answer, the answer did lack the real stuff, though the reason for down vote was not there, for the poster to understand what information, the post `lacks/ is wrong`. For me everyone deserves that ONE CHANCE to improve :-) – nIcE cOw Jul 25 '13 at 10:31
  • 1
    as @nIcEcOw mentioned: getting the reason for a downvote explained in detail, is a _chance to improve_ your answer :-) – kleopatra Jul 25 '13 at 11:13
  • @SandiipPatil : I hope you will update this answer soon, as already stated, this answer does lack things, to provide concrete help to someone, in any way. I am still waiting for that to happen :-) – nIcE cOw Jul 25 '13 at 12:00