crelix
crelix

Reputation: 35

Java JLabel won't update

Edited Version, I hope this is what are you looking for. I tried to keep only necesery things in this version. I don't know what else to write, but i can't upload this edit becouse code is to big and text so small, so i write random stuf..

mainClass :

package Clicker;

import javax.swing.JFrame;

public class mainClass {

     public static void main(String[] args) {


         ClickerGame CG = new ClickerGame();

            JFrame frame = new JFrame("CarCollectionarV1");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            CG.addComponentToPane(frame.getContentPane());

            frame.pack();
            frame.setVisible(true);
            frame.setResizable(true);
            frame.setSize(1000, 700);

     }
}

ClickerGame Class :

package Clicker;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ClickerGame implements ActionListener {

       JButton jbtnMoney = new JButton("Click");
       JButton jbtnBoxes = new JButton("Click");

       final static String Click = "Click";

       //Make TabbedPane
        public void addComponentToPane(Container pane){

            jbtnMoney.addActionListener(this);
            jbtnBoxes.addActionListener(this);

        LabelsHolder Labels = new LabelsHolder(); // Imports JLabels

        //Visual        
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setTabPlacement(JTabbedPane.TOP);

        //MainPanel
        JPanel MainPanel = new JPanel();

        JPanel MainPanelSub = new JPanel();
        MainPanelSub.setLayout(new BoxLayout(MainPanelSub, BoxLayout.PAGE_AXIS));

        JPanel MainPanelSub2 = new JPanel();
        MainPanelSub2.setLayout(new BoxLayout(MainPanelSub2, BoxLayout.PAGE_AXIS));


        MainPanelSub.add(Labels.MoneyLabel);
        MainPanelSub.add(Labels.MoneyClicksLabel);
        MainPanelSub.add(jbtnMoney);
        MainPanelSub2.add(Labels.BoxesLabel);
        MainPanelSub2.add(Labels.BoxesClicksLabel);
        MainPanelSub2.add(jbtnBoxes);
        MainPanel.add(MainPanelSub);

        tabbedPane.addTab(Click, MainPanel);
        pane.add(tabbedPane, BorderLayout.CENTER);

        }

        MainHolder CarMain = new MainHolder();
        LabelsHolder Labels = new LabelsHolder();
         //Button Click

        public void actionPerformed(ActionEvent ae) {

        //Money Button Click
        if(ae.getSource() == jbtnMoney) {           
            CarMain.main[0] += 1;   
            CarMain.main[1] += 1;
            Labels.MoneyLabel.setText("Test");
        }
        }
}       

LabelsHolder class :

package Clicker;

import javax.swing.JLabel;

class LabelsHolder {

    MainHolder CarMain = new MainHolder();


    public JLabel MoneyLabel = new JLabel();  
    public JLabel MoneyClicksLabel = new JLabel();
    public JLabel BoxesLabel = new JLabel();
    public JLabel BoxesClicksLabel = new JLabel();

    public LabelsHolder(){

        MoneyLabel.setHorizontalTextPosition(JLabel.CENTER);
        MoneyLabel.setVerticalTextPosition(JLabel.BOTTOM);
        MoneyLabel.setText("Money: " + CarMain.main[0]);

        MoneyClicksLabel.setHorizontalTextPosition(JLabel.CENTER);
        MoneyClicksLabel.setVerticalTextPosition(JLabel.BOTTOM);
        MoneyClicksLabel.setText("Money Clicks: " + CarMain.main[1]);

        BoxesLabel.setHorizontalTextPosition(JLabel.CENTER);
        BoxesLabel.setVerticalTextPosition(JLabel.BOTTOM);
        BoxesLabel.setText("Boxes: " + CarMain.main[2]);

        BoxesClicksLabel.setHorizontalTextPosition(JLabel.CENTER);
        BoxesClicksLabel.setVerticalTextPosition(JLabel.BOTTOM);
        BoxesClicksLabel.setText("Boxes Clicks: " + CarMain.main[3]);

    }
}   

MainHolder Class :

package Clicker;

public class MainHolder {

public Integer main[] = new Integer[4];

    public MainHolder(){

        MakeMain();     
    }

    public void MainMaker(int k,int p){

        main[p] = k;

    }

    public void MakeMain(){



        int i=0;
        MainMaker(0,i); // Money
        i++;
        MainMaker(0,i); // Money Clicks
        i++;
        MainMaker(0,i); // Boxes
        i++;
        MainMaker(0,i); // Boxes Clicks
        i++;
    }
}

Upvotes: 1

Views: 271

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

You're creating two LabelsHolder instances:

class ClickerGame implements ActionListener {

        // ....
        LabelsHolder Labels = new LabelsHolder(); // **** here

        // Visual
        // .....
    }

    // and here!!!@
    MainHolder CarMain = new MainHolder();
    LabelsHolder Labels = new LabelsHolder();

Doing this means that your ActionListener is changing the state of a non-visualized JLabel. The solution: Create and use only one. So get rid of the instance declared inside of the constructor and instead only use the instance field.

Other suggestions:

  • You will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.
  • You should avoid using public fields and allowing other classes direct access to these fields. Much better would be to use private fields and public methods that limit the exposure of your object states.

Upvotes: 1

Related Questions