How to Update JFrame Components?

123 views Asked by At

Well, i have this code.

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

import java.awt.Font;

import javax.swing.JSeparator;
import javax.swing.JTextPane;
import javax.swing.JButton;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class Menu {

private JFrame frmFelps;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Menu window = new Menu();
                window.frmFelps.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Menu() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    Funcoes acoes = new Funcoes();
    frmFelps = new JFrame();
    frmFelps.setResizable(false);
    frmFelps.setTitle("Felps");
    frmFelps.setBounds(100, 100, 824, 522);
    frmFelps.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmFelps.getContentPane().setLayout(null);

    JProgressBar progressoSede = new JProgressBar();
    progressoSede.setStringPainted(true);
    progressoSede.setValue(acoes.getSede());
    progressoSede.setBounds(71, 25, 100, 25);
    frmFelps.getContentPane().add(progressoSede);

    JProgressBar progressoAmor = new JProgressBar();
    progressoAmor.setValue(acoes.getAmor());
    progressoAmor.setStringPainted(true);
    progressoAmor.setBounds(71, 56, 100, 25);
    frmFelps.getContentPane().add(progressoAmor);

    JProgressBar progressoFeliz = new JProgressBar();
    progressoFeliz.setValue(acoes.getFeliz());
    progressoFeliz.setStringPainted(true);
    progressoFeliz.setBounds(71, 85, 100, 25);
    frmFelps.getContentPane().add(progressoFeliz);

    JLabel lblSede = new JLabel("Sede:");
    lblSede.setToolTipText("Porcentagem de AQUA que voc\u00EA bebeu e gravou nos ultimos tempos.");
    lblSede.setFont(new Font("Arial", Font.PLAIN, 16));
    lblSede.setBounds(24, 25, 46, 23);
    frmFelps.getContentPane().add(lblSede);

    JLabel lblAmor = new JLabel("Amor:");
    lblAmor.setToolTipText("O Amor n\u00E3o precisa ser descrito com palavras.");
    lblAmor.setFont(new Font("Arial", Font.PLAIN, 16));
    lblAmor.setBounds(24, 56, 46, 23);
    frmFelps.getContentPane().add(lblAmor);

    JLabel lblFeliz = new JLabel("Feliz:");
    lblFeliz.setToolTipText("Um Felps Feliz \u00E9 um Felps que n\u00E3o esta Triste.");
    lblFeliz.setFont(new Font("Arial", Font.PLAIN, 16));
    lblFeliz.setBounds(24, 85, 46, 23);
    frmFelps.getContentPane().add(lblFeliz);

    JLabel lblCamera = new JLabel("Camera:");
    lblCamera.setToolTipText("Quanto melhor a Camera mais Video com FaceCam o Felps Faz");
    lblCamera.setBounds(24, 168, 74, 14);
    frmFelps.getContentPane().add(lblCamera);

    JLabel lblPc = new JLabel("PC:");
    lblPc.setToolTipText("Quanto melhor seu PC mais o Cellbits vai sentir inveja de voc\u00EA.");
    lblPc.setBounds(24, 199, 74, 14);
    frmFelps.getContentPane().add(lblPc);

    JLabel lblFone = new JLabel("Fone:");
    lblFone.setToolTipText("Hey Galera Felps falando e bem vindos ao meu canal.");
    lblFone.setBounds(24, 230, 74, 14);
    frmFelps.getContentPane().add(lblFone);

    JLabel lblInscritos = new JLabel("Inscritos:");
    lblInscritos.setToolTipText("Quanto mais melhor. (P.S.: Eles s\u00F3 v\u00E3o te amar se voc\u00EA amar eles)");
    lblInscritos.setBounds(50, 290, 83, 14);
    frmFelps.getContentPane().add(lblInscritos);

    JLabel lblDinheiros = new JLabel("Dinheiros:");
    lblDinheiros.setToolTipText("Quantos dinheiros existem dentro do seu bolso Felpastico.");
    lblDinheiros.setBounds(10, 432, 60, 14);
    frmFelps.getContentPane().add(lblDinheiros);

    JTextPane txtpnCamera = new JTextPane();
    txtpnCamera.setEditable(false);
    txtpnCamera.setText(acoes.getCamera());
    txtpnCamera.setBounds(108, 162, 104, 20);
    frmFelps.getContentPane().add(txtpnCamera);

    JTextPane txtpnPc = new JTextPane();
    txtpnPc.setText(acoes.getPc());
    txtpnPc.setEditable(false);
    txtpnPc.setBounds(108, 193, 104, 20);
    frmFelps.getContentPane().add(txtpnPc);

    JTextPane txtpnFone = new JTextPane();
    txtpnFone.setText(acoes.getFone());
    txtpnFone.setEditable(false);
    txtpnFone.setBounds(108, 224, 104, 20);
    frmFelps.getContentPane().add(txtpnFone);

    JTextPane txtpnInscrito = new JTextPane();
    txtpnInscrito.setEditable(false);
    txtpnInscrito.setText(acoes.getInscritos());
    txtpnInscrito.setBounds(24, 315, 130, 20);
    frmFelps.getContentPane().add(txtpnInscrito);

    JTextPane txtpnDinheiro = new JTextPane();
    txtpnDinheiro.setText(acoes.getDinheiro());
    txtpnDinheiro.setEditable(false);
    txtpnDinheiro.setBounds(80, 426, 91, 20);
    frmFelps.getContentPane().add(txtpnDinheiro);

    JButton btnYoutube = new JButton("YouTube");
    btnYoutube.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            //TODO Fazer Algo
        }
    });

    JSeparator separator = new JSeparator();
    separator.setOrientation(SwingConstants.VERTICAL);
    separator.setBounds(228, 0, 13, 493);
    frmFelps.getContentPane().add(separator);
    btnYoutube.setBounds(534, 172, 146, 25);
    frmFelps.getContentPane().add(btnYoutube);

    JButton btnCasa = new JButton("Casa");
    btnCasa.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            //TODO Fazer Algo
            acoes.setInscritos(10);
            //txtpnInscrito.setText(acoes.getInscritos());
            atualizador();
            //txtpnInscrito.setText(acoes.getInscritos());
        }
    });
    btnCasa.setBounds(268, 171, 146, 25);
    frmFelps.getContentPane().add(btnCasa);

    JButton btnCidade = new JButton("Cidade");
    btnCidade.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            //TODO Fazer Algo
        }
    });
    btnCidade.setBounds(268, 264, 146, 25);
    frmFelps.getContentPane().add(btnCidade);

    JButton btnBica = new JButton("Bica");
    btnBica.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            //TODO Fazer Algo
        }
    });
    btnBica.setBounds(534, 265, 146, 25);
    frmFelps.getContentPane().add(btnBica);

    JLabel lblFundo = new JLabel("New label");
    lblFundo.setIcon(new ImageIcon("img\\mapa.png"));
    lblFundo.setBounds(251, 20, 528, 435);
    frmFelps.getContentPane().add(lblFundo);
}

private void atualizador(){
    //TODO
}
}

I want to change the text on txtpnInscrito every time that atualizador() runs But i don't know how to do this. If i change the text directly in the button btnCasa it changes, but i want to make a refresher by time and utilize the same function to refresh when doing actions on buttons.

Thanks in advance. And sorry for the horrible english.

1

There are 1 answers

0
MadProgrammer On BEST ANSWER

You need to make txtpnInscrito field of the class so that it's accessible in all your methods within your class

Have a look at Understanding Class Members for more details