DieLyn
DieLyn

Reputation: 11

Changing JLabels between classes

My first post, so forgive any incorrect etiquette. I'm currently doing my year end project for school and I need a bit of help. I am making a GUI java app in Netbeans. I have two classes. One is a class that controls a timer, the other is a class that is a scoreboard screen. I need to update the scoreboard timerLabel with the time that is being counted down in the timerClass. Its quite messy as there is another timer label in the Timer class which does update. My problem is that I cannot get timerLabel in MatchScreen() to update. Here is my code :

Timer Class

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class TimerClass extends JFrame {

    Timer timer;
    JLabel promptLabel, timerLabel;
    int counter;
    JTextField tf;
    JButton button;
    MatchScreen call = null;

    public TimerClass() {
        call = new MatchScreen();
        setLayout(new GridLayout(4, 4, 7, 7));
        promptLabel = new JLabel(""
                + "Enter number of seconds for the timer", 
                SwingConstants.CENTER);
        add(promptLabel);
        tf = new JTextField(5);
        add(tf);
        button = new JButton("Start");
        add(button);
        timerLabel = new JLabel("waiting...", 
                SwingConstants.CENTER);
        add(timerLabel);

        event e = new event();
        button.addActionListener(e);
        System.out.println("Button pressed");
    }

    public class event implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            System.out.println("Action performed");
            int count = (int) (Double.parseDouble(tf.getText()));
            timerLabel.setText("Time left: " + count);
            call.setTimerLabel(count);
            System.out.println("Passed count to tc");
            TimeClass tc = new TimeClass(count);
            timer = new Timer(1000, tc);
            System.out.println("Timer.start");
            timer.start();
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        /*public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
        }*/
    }

    public class TimeClass implements ActionListener {

        int counter;

        public TimeClass(int counter) {
            this.counter = counter;
        }

        public void actionPerformed(ActionEvent e) {
            counter--;
            if (counter >= 1) {
                call.setTimerLabel(counter);
            } else {
                timerLabel.setText("END");
                timer.stop();
                Toolkit.getDefaultToolkit().beep();
            }
        }
    }

    public static void main(String args[]) {
        TimerClass gui = new TimerClass();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(250, 150);
        gui.setTitle("Time Setup");
        gui.setVisible(true);
    }
}

And now the ScoreBoard Screen

public class MatchScreen extends javax.swing.JFrame {

    int redScore = 0, blueScore = 0, blueCat1 = 0,
            blueCat2 = 0, redCat1 = 0, redCat2 = 0, winner = 0;

    public MatchScreen() {
        initComponents();
    }

    //Determine Winner of the match
    public int getWinner() {
        if (redScore > blueScore) {
            winner = 1;
        } else {
            winner = 2;
        }
        return winner;
    }

    public void setTimerLabel(int a) {
        int time = a;
        while (time >= 1) {
            timerLabel.setText("" + time);
        }
        if (time < 1) {
            timerLabel.setText("End");
        }
    }

    private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
        //Creates an object of the timerClass
        TimerClass gui = new TimerClass();
        gui.setSize(300, 175);
        gui.setTitle("Time Setup");
        gui.setVisible(true);
    }
}

Some code that I felt is irrelevant was left out from the MatchScreen().

Many thanks

Upvotes: 0

Views: 362

Answers (2)

DieLyn
DieLyn

Reputation: 11

Managed to solve the general problem. I put all the code into one class. Not ideal, but it works :/ Anyway, deadlines are looming.

Sincere thanks.

Upvotes: 1

Brandon Buck
Brandon Buck

Reputation: 7181

You have a while loop in the setTimerLabel method, which I don't think you intended to put there. Also, you take the parameter a and assign it to time and then never use a again, why not just rename your parameter to time and bypass that additional variable?

EDIT

Sorry, I forgot to explain what I'm seeing :P If you say call.setTimerLabel(10) then you hit that while loop (while(time >= 1) which is essentially running while(10 >= 1) which is an infinite loop. Your program is never leaving the method setTimerLabel the first time you call it with a value >= 1.

Upvotes: 0

Related Questions