Update a JLabel's text without altering everything else

Viewed 143

Title says it all, I need to update a JLabel's text, and sure, that works wonders with the basic methods, but my ''''game'''' goes from this:

no text update yet

to this

text update

I need to stop this from happening, I need to update the text of the label which says "a" on the second picture every second, my best bet was overriding paint method and drawing a string with this code:

public void paint(Graphics g){

        super.paint(g);


        g.setColor(Color.BLACK);

        g.drawString("Arca: " + administrador.getCiudad().getArca() 
                + "                                    " 
                + " Puntos de Belleza: " + administrador.getCiudad().getPuntosBelleza() + 
                "                                    " + 
                " Habitantes: " + 
                administrador.getCiudad().getCantidadHabitantes() + " / " 
                + administrador.getCiudad().getCantidadHabitantesDisponibles(), 400, 45);

            repaint();
}

but the string flickered nonstop, and so now I'm trying to update a Label's text inside a panel, but then the same thing that is showed in the second picture happens.

My question here is: how can I update the Label's text without it painting everything else?

Please note that using the JLabel (instead of drawstring) I am not using the code posted above, I'm not using any really, just adding it to the panel.

If you'd like a SSCCE concerning this all, you might be able to make use of this:

public class GUIJuego extends JFrame{

private JLabel labelConstrucciones = new JLabel("Construcciones");
private JLabel labelEstados = new JLabel(" ");

public GUIJuego(){

    JPanel panelConstruccion = new JPanel(new GridLayout(9,1));
    JPanel panelDatosCiudad = new JPanel(new GridLayout(1,2));
    JPanel panelMapa = new JPanel(new GridLayout(25,25));
    JPanel panelEstado = new JPanel(new FlowLayout()); 

    administrador = new Administrador(new Ciudad(), 0, new Dificultad(this.getMultiplicadorDificultad()), new Constructora());

    administrador.administrar(this);

    panelDatosCiudad.add(panelEstado);
    panelEstado.add(labelEstados);

    Timer timer = new Timer(10, new ActionListener(){
        public void actionPerformed(ActionEvent e){
            labelEstados.setText("Arca: " + administrador.getCiudad().getArca() 
                    + "                                    " 
                    + " Puntos de Belleza: " + administrador.getCiudad().getPuntosBelleza() + 
                    "                                    " + 
                    " Habitantes: " + 
                    administrador.getCiudad().getCantidadHabitantes() + " / " 
                    + administrador.getCiudad().getCantidadHabitantesDisponibles());
        }

    });

    timer.start();
    }
}

// Timer which updates the JLabel's values along with its class

public class Administrador {

private Ciudad ciudad;
private int contadorExplosion;
private Dificultad dificultad;
private Constructora constructora;
private TimerTask taskJuego;

public void administrar(GUIJuego juego){

    Timer timer = new Timer("Juego");

    taskJuego = new TimerTask(){
        public void run() {

            if((getCiudad().getArca() < 100000 && getCiudad().getArca() > 0) && getCiudad().getPuntosBelleza() < 1000 && getCiudad().getCantidadHabitantes() < 1000){

                getEgresosIngresos();
                tryExplotar();

            } else if(getCiudad().getArca() >= 100000 || getCiudad().getPuntosBelleza() >= 1000 || getCiudad().getCantidadHabitantes() >= 1000){

                this.cancel();
                JOptionPane.showMessageDialog(null, "¡Ganaste!");

            } else if(getCiudad().getArca() <= -1000) {

                this.cancel();
                JOptionPane.showMessageDialog(null, "¡Perdiste!");

            }
        }
    };

    timer.scheduleAtFixedRate(taskJuego, 0, 1000);


}
}

I hope this information is useful.

2 Answers
Related