Thread and repaint()

Hi I have a project to show some information, what I do is to put the specific information in some JPanel. This is a production project and I have 7 production lines, so for each line I use a panelCel and in that panelCel I create the JPanels with the orders, but I don't know why it doesn't show all the orders. I use a thread for each line to fill all the orders so in total I have 7 threads and works fine, but it doesn't actualize when my code finish until I move the mouse or press a button, why its happening this and how can I solve it? I have used the validate() and repaint() methods but it doesn't make a difference.

Here is an example of how I call my threads:

public void actualizarLineas(){
lineaProduccion linea1 = new lineaProduccion(panelCel1, ordenes1);
    lineaProduccion linea2 = new lineaProduccion(panelCel2, ordenes2);
    lineaProduccion linea3 = new lineaProduccion(panelCel3, ordenes3);
    lineaProduccion linea4 = new lineaProduccion(panelCel4, ordenes4);
    lineaProduccion linea5 = new lineaProduccion(panelCel5, ordenes5);
    lineaProduccion linea6 = new lineaProduccion(panelCel6, ordenes6);
    lineaProduccion linea7 = new lineaProduccion(panelCel7, ordenes7);

    linea1.start();
    linea2.start();
    linea3.start();
    linea4.start();
    linea5.start();
    linea6.start();
    linea7.start();}

ordenesX are the arrays that have the orders, and this is the class:

public class lineaProduccion extends Thread {
JPanel jpCelula;
    String []datos;

    public lineaProduccion(JPanel jpCelula, String[] datos) {
        this.jpCelula = jpCelula;
        this.datos = datos;
    }

    public JPanel getJpCelula() {
        return jpCelula;
    }

    public void setJpCelula(JPanel jpCelula) {
        this.jpCelula = jpCelula;
    }

    public String[] getDatos() {
        return datos;
    }

    public void setDatos(String[] datos) {
        this.datos = datos;
    }

    @Override
    public void run(){
        String auxOrdenAct = "0";
        String auxOrdenAnt  = "0";
        String celulaAnterior = "0";
        String celulaActual = "0";
        Color colorActual = null;
        Color colorAnterior = null;

        if(datos.length > 0){
            int posicionX = 10;

            for (String ordenActual : datos) {
                final JPanel orden = new JPanel();
                orden.setName(ordenActual);
                orden.setLocation(posicionX ,15);
                orden.setSize(65, 75);
                orden.setToolTipText(ordenActual);
                auxOrdenAnt = auxOrdenAct;
                auxOrdenAct = ordenActual;
                if(auxOrdenAnt.equals("0"))
                    auxOrdenAnt = ordenActual;
                celulaAnterior = celulaActual;
                celulaActual = jpCelula.getName();
                if(!celulaAnterior.equals(celulaActual))
                    celulaAnterior = celulaActual;
                String inf1 = transaccion.obtieneinf1(ordenActual);
                if(inf1.equals("-1"))
                    orden.setBackground(new java.awt.Color(0, 102,153));
                final JLabel etiqueta[] = new JLabel[3];
                for(int j=0; j<3; j++){
                    etiqueta[j]= new JLabel("etiqueta" +j);
                    etiqueta[j].setForeground(Color.white);
                }
                final String inf2 = transaccion.obtieneinf2(ordenActual);
                final String inf3 = transaccion.obtieneinf3(ordenActual);
                etiqueta[0].setText(ordenActual);
                etiqueta[1].setText("    "+ inf2 + "    ");
                if(!inf3.equals("-1"))
                    etiqueta[2].setText(inf3);

                orden.add(etiqueta[0]);
                orden.add(etiqueta[1]);
                orden.add(etiqueta[2]);
                posicionX = posicionX + 70;
                jpCelula.add(orden);
                Dimension d = new Dimension(posicionX , 70);
                jpCelula.setPreferredSize(d);
            }
        } else {
            System.out.println("No hay órdenes para esta línea");
        }
    }}

Basically what I do is to put the information in one JPanel and add it to my jPanel.

Upvotes: 0

Views: 70

Answers (2)

What I have done to solve this was doing a single thread that make all the stuff, so this single thread have the two methods of above but instead of calling more threads, it call only a method. I have tried to use the EDT but it haven't work for me, thanks to everyone who helped me!

Upvotes: 0

Jon
Jon

Reputation: 3502

You've got to be on the "Swing Thread", which is also called the EDT. Swing is designed for single threaded access using only this thread. If you make swing calls such as paint() or repaint() or foo.setText() or anything like that on a different thread you will get unpredictable results.

This question explains how to make your code run on this thread. Move all the Swing calls to this thread and it will work better.

Java Swing - running on EDT

Also, be sure not to do long blocking calls on the EDT, such as a database read. Doing that will cause the application to freeze up while the EDT is busy. Only use the EDT for Swing or AWT things.

Upvotes: 1

Related Questions