James B
James B

Reputation: 231

Java setText JLabel from another class

Hello I have two classes one which contains just my GUI and the other with my methods. I need to update the playerMoneyLbl label in my BlackjackGUI class, from one of my Blackjack class's methods. I thought I will need to first convert the int to string before it can be displayed as a label but my code does not work. I am grateful for any help. Here is my code:

BlackjackGUI Class

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class BlackjackGUI{

    private JFrame frame;
    private JPanel panel, panelTop, panelBottom, panelCenter, panelHitBtn, panelStandBtn;
    private JButton newGameBtn, dealBtn, hitBtn, standBtn;
    JLabel placeBetLbl, playerMoneyLbl, playerCardLbl, dealerCardLbl, playerCardTotalLbl, dealerCardTotalLbl;
    private JTextField betInputBox;
    private Blackjack Blackjack = new Blackjack();


    Color bg = new Color(0, 100, 0);

    public BlackjackGUI(){
        createForm();

        fillTop();
        fillCenter();
        fillBottom();

        frame.add(panel);
        frame.setVisible(true);
    }

    public void createForm() {
        frame = new JFrame("Blackjack");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1000,900);

        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBackground(bg);

        panelTop = new JPanel();
        panelTop.setBackground(bg);
        panel.add(panelTop, BorderLayout.NORTH);

        panelBottom = new JPanel();
        panelBottom.setBackground(bg);
        panel.add(panelBottom, BorderLayout.SOUTH);

        panelCenter = new JPanel();
        panelCenter.setLayout(new GridLayout(3, 2));
        panelCenter.setBackground(bg);
        panel.add(panelCenter, BorderLayout.CENTER);

    }

    public void fillTop() {

        newGameBtn = new JButton("New Game");
        newGameBtn.setFont(new Font("Gill Sans MT", Font.PLAIN, 16));
        panelTop.add(newGameBtn);
        newGameBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);

            }
        });


    }

    public void fillCenter() {

                ImageIcon defaultDealerCard1 = new ImageIcon("Images/Cards/BACK.png");
                dealerCardLbl = new JLabel();
                dealerCardLbl.setIcon(defaultDealerCard1);
                dealerCardLbl.setHorizontalAlignment(dealerCardLbl.CENTER);
                panelCenter.add(dealerCardLbl);

                dealerCardTotalLbl = new JLabel("5");
                dealerCardTotalLbl.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
                dealerCardTotalLbl.setForeground(Color.WHITE);
                dealerCardTotalLbl.setHorizontalAlignment(dealerCardTotalLbl.CENTER);
                panelCenter.add(dealerCardTotalLbl);

                ImageIcon defaultPlayerCard1 = new ImageIcon("Images/Cards/BACK.png");
                playerCardLbl = new JLabel();
                playerCardLbl.setIcon(defaultPlayerCard1);
                playerCardLbl.setHorizontalAlignment(playerCardLbl.CENTER);
                panelCenter.add(playerCardLbl);

                playerCardTotalLbl = new JLabel("16 - BUST! You lose :(");
                playerCardTotalLbl.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
                playerCardTotalLbl.setForeground(Color.WHITE);
                playerCardTotalLbl.setHorizontalAlignment(playerCardTotalLbl.CENTER);
                panelCenter.add(playerCardTotalLbl);

                panelHitBtn = new JPanel();
                panelHitBtn.setLayout(new GridLayout(3, 3));
                panelHitBtn.setBackground(bg);
                panelCenter.add(panelHitBtn);

                panelStandBtn = new JPanel();
                panelStandBtn.setLayout(new GridLayout(3, 3));
                panelStandBtn.setBackground(bg);
                panelCenter.add(panelStandBtn);

                hitBtn = new JButton("Hit");
                hitBtn.setMaximumSize( new Dimension (50,50));
                hitBtn.setHorizontalAlignment(hitBtn.CENTER);
                hitBtn.setVerticalAlignment(hitBtn.CENTER);
                panelHitBtn.add(hitBtn, BorderLayout.CENTER);
                hitBtn.setVisible(false);

                hitBtn = new JButton("Hit");
                hitBtn.setMaximumSize( new Dimension (50,50));
                hitBtn.setHorizontalAlignment(hitBtn.CENTER);
                hitBtn.setVerticalAlignment(hitBtn.CENTER);
                panelHitBtn.add(hitBtn, BorderLayout.CENTER);
                hitBtn.setVisible(false);

                hitBtn = new JButton("Hit");
                hitBtn.setMaximumSize( new Dimension (50,50));
                hitBtn.setHorizontalAlignment(hitBtn.CENTER);
                hitBtn.setVerticalAlignment(hitBtn.CENTER);
                panelHitBtn.add(hitBtn, BorderLayout.CENTER);
                hitBtn.setVisible(false);

                hitBtn = new JButton("Hit");
                hitBtn.setMaximumSize( new Dimension (50,50));
                hitBtn.setHorizontalAlignment(hitBtn.CENTER);
                hitBtn.setVerticalAlignment(hitBtn.CENTER);
                panelHitBtn.add(hitBtn, BorderLayout.CENTER);
                hitBtn.setVisible(false);

                hitBtn = new JButton("HIT"); //KEEP THIS ONE
                hitBtn.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
                hitBtn.setHorizontalAlignment(hitBtn.CENTER);
                hitBtn.setVerticalAlignment(hitBtn.CENTER);
                panelHitBtn.add(hitBtn, BorderLayout.CENTER);

                hitBtn = new JButton("Hit");
                hitBtn.setMaximumSize( new Dimension (50,50));
                hitBtn.setHorizontalAlignment(hitBtn.CENTER);
                hitBtn.setVerticalAlignment(hitBtn.CENTER);
                panelHitBtn.add(hitBtn, BorderLayout.CENTER);
                hitBtn.setVisible(false);

                hitBtn = new JButton("Hit");
                hitBtn.setMaximumSize( new Dimension (50,50));
                hitBtn.setHorizontalAlignment(hitBtn.CENTER);
                hitBtn.setVerticalAlignment(hitBtn.CENTER);
                panelHitBtn.add(hitBtn, BorderLayout.CENTER);
                hitBtn.setVisible(false);

                hitBtn = new JButton("Hit");
                hitBtn.setMaximumSize( new Dimension (50,50));
                hitBtn.setHorizontalAlignment(hitBtn.CENTER);
                hitBtn.setVerticalAlignment(hitBtn.CENTER);
                panelHitBtn.add(hitBtn, BorderLayout.CENTER);
                hitBtn.setVisible(false);

                hitBtn = new JButton("Hit");
                hitBtn.setMaximumSize( new Dimension (50,50));
                hitBtn.setHorizontalAlignment(hitBtn.CENTER);
                hitBtn.setVerticalAlignment(hitBtn.CENTER);
                panelHitBtn.add(hitBtn, BorderLayout.CENTER);
                hitBtn.setVisible(false);

                standBtn = new JButton("Stand");
                panelStandBtn.add(standBtn);
                standBtn.setVisible(false);

                standBtn = new JButton("Stand");
                panelStandBtn.add(standBtn);
                standBtn.setVisible(false);

                standBtn = new JButton("Stand");
                panelStandBtn.add(standBtn);
                standBtn.setVisible(false);

                standBtn = new JButton("Stand");
                panelStandBtn.add(standBtn);
                standBtn.setVisible(false);

                standBtn = new JButton("STAND");
                standBtn.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
                panelStandBtn.add(standBtn);

                standBtn = new JButton("Stand");
                panelStandBtn.add(standBtn);
                standBtn.setVisible(false);

                standBtn = new JButton("Stand");
                panelStandBtn.add(standBtn);
                standBtn.setVisible(false);

                standBtn = new JButton("Stand");
                panelStandBtn.add(standBtn);
                standBtn.setVisible(false);

                standBtn = new JButton("Stand");
                panelStandBtn.add(standBtn);
                standBtn.setVisible(false);     

    }

    public void fillBottom() {

        betInputBox = new JTextField("25");
        betInputBox.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
        betInputBox.setHorizontalAlignment(SwingConstants.RIGHT);
        betInputBox.setPreferredSize(new Dimension(175,50));
        betInputBox.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                betInputBox.setText("");
            }
        });
        panelBottom.add(betInputBox, BorderLayout.WEST);

        dealBtn = new JButton("DEAL");
        dealBtn.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
        panelBottom.add(dealBtn);
        newGameBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);

            }
        });

        placeBetLbl = new JLabel("Place your bets!");
        placeBetLbl.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
        placeBetLbl.setForeground(Color.YELLOW);
        panelBottom.add(placeBetLbl);

        playerMoneyLbl = new JLabel();
        playerMoneyLbl.setFont(new Font("Gill Sans MT", Font.PLAIN, 35));
        playerMoneyLbl.setForeground(Color.WHITE);
        panelBottom.add(playerMoneyLbl, BorderLayout.EAST);

    }

    public static void main(String[] args) {
    new BlackjackGUI();

    }

}

Blackjack Class

public class Blackjack {

    int playerMoney = 2222;
    String playerMoneyString;

    public void process(){
        playerMoneyString = Integer.toString(playerMoney);
        BlackjackGUI.playerMoneyLbl.setText

    }

    public void deal() {
        //deal code goes here.
    }



}

Upvotes: 0

Views: 179

Answers (2)

Tomasz Nocoń
Tomasz Nocoń

Reputation: 408

I think a better approach would be to create a getter method inside the Blackjack class and update the label in the BlackjackGUI class (since it's a GUI class it should be the one that does GUI stuff, like updating labels).

Also You are trying to access playerMoneyLbl variable as if it was static (BlackjackGUI.playerMoneyLbl), but it's not.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347194

You have any number of options, some good, some bad

You could...

Pass a reference of the JLabel to the other class. This is kind of bad:

  • As it exposes the UI component to the class
  • Couples the code together making it difficult to manage or change in the future
  • Exposes the UI component to the other class which would allow it to make unchecked to changes to it or other components
  • Really is beyond the scope of the respobsibility for the second class. What does it care about what you want to happen when something changes?

You could...

Use an Observer Pattern, allowing interested parties to register themselves to the second class, so that when some event occured, the second class could notify the interested parties and then they could take what ever action that they wanted to based on their own individual needs

This:

  • Decouples the code, as no one piece is reliant on the other, making it easier to change and modify the code in the future
  • Defines and separates the area's of responsibility for the classes

You might also like to have a look at Model-View-Controller which might give you some more ideas about how to structure your code

Upvotes: 2

Related Questions