Search code examples
javaswingawtjpanelgridbaglayout

Swing Method error


I'm having a problem. I'm reading data from a socket, and when I read some meaningful data to my app I call a method to repaint the screen with the swing objects I want. This method works all right excepts that sometimes it does not appear. Nothing on the screen on the very first time the method is called (I already ran the debug and it passes all through the method). The stupid part is that, if I minimize and maximize the app it works fine again. Besides, if the method is executed again it works fine to. I'll put the code here just for documentation:

public void GraphicONENodeJoined (Nodes NodesList)
 {
     SensorsMapGraf [] sensorsMap;
     boolean FLAG;
     int ymin=0, ymax=30;
     FLAG = TabExistingNode_NEW_nodeRefresh (NodesList);
     if (FLAG)
     {
         ValorTeste++;
         if (ValorTeste > 1)
         {
             String [] s = new String[ValorTeste];
             JButton [] jbb = new JButton[ValorTeste];
             JCheckBox [] ch = new JCheckBox[ValorTeste];
             for (int i=0 ; i < ValorTeste-1 ; i++)
             {
                 s[i] =  nods[i];
                 jbb[i] = jb[i];
                 ch[i] = CheckBoxNodes[i];      
             }
             jb = new JButton[ValorTeste];
             CheckBoxNodes = new JCheckBox[ValorTeste];
             nods =  new String[ValorTeste];

             for (int i=0 ; i < ValorTeste ; i++)
             {
                 nods[i] = s[i];
                 jb[i] = jbb[i];

                 CheckBoxNodes[i] = ch[i];
             }       
         }

            nods[ValorTeste-1]+= NodesList.GETSourceNodeID();
            //PARA FAZER O UPDATE DAS TABS E DOS GRÁFICOS
            JPanel meio = new JPanel();         //JPanel que contem os graficos de cada sensor

            String s = new String();
            s += NodesList.GETSourceNodeID();

            JPanel meio_cima = new JPanel();    //JPanel que contem  o botão view (permitirá escolher quais os sensores a ver)
            meio_cima.setAlignmentX(LEFT_ALIGNMENT);
            meio_cima.setPreferredSize(new Dimension(dim.width, 25));
            meio.setBounds (pan1.getBounds());
            GridBagLayout g = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();

            meio.setLayout(g);
            meio.setOpaque(true);
            //botão view (permitirá escolher quais os sensores a ver)
            jb[nods.length-1] = new JButton("View");
            jb[nods.length-1].setPreferredSize(new Dimension(100,20));
            jb[ValorTeste -1].addActionListener(this);
            meio_cima.add(jb[ValorTeste-1]);
            //Panel qu albergará o panel meio e o panel meio_cima
            JPanel principal = new JPanel();
            //inicia o array de sensores que cada Tab tem de mostrar
            sensorsMap = new SensorsMapGraf[NodesList.GETnumSensoresActivos()];

            int indice = 0;
            for (int j=0 ; j < 12 ; j++)
            { 
                //nicely to view in the graphics
                if ((j == 0) || (j == 1) || (j == 2) || (j == 3) || (j == 6) || (j == 7))
                {
                    ymin = 0;
                    ymax = 3;
                }
                if (j == 8)
                {
                    ymin = 0;
                    ymax = 100;
                }
                if (j == 9)
                {
                    ymin = 0;
                    ymax = 100;
                }
                if (j == 10)
                {
                    ymin = 0;
                    ymax = 4;
                }
                if (j == 11)
                {
                    ymin = 0;
                    ymax = 130;
                }
                if (j == 4 )
                {
                    ymin = 0;
                    ymax = 6000;
                }
                if (j == 5)
                {
                    ymin = 0;
                    ymax =700;
                }

                if (NodesList.GETSensores(j).GETligado()) // se estiver ligado
                {
                    //panel que albergará o grafico 
                    JPanel p = new JPanel();
                    p.setBounds(meio.getBounds());

                    if (NodesList.GETnumSensoresActivos() <= 4)
                    {
                        if (NodesList.GETnumSensoresActivos() <= 4  && NodesList.GETnumSensoresActivos() > 1)
                            sensorsMap[indice] = new SensorsMapGraf(true,NodesList.GETSensores(j).GETnome(),NodesList.GETSensores(j).GETunidade(),meio.getBounds(),(int)meio.getWidth()/2-20,(int)meio.getHeight()/2-20,ymin,ymax); // cria o sensor
                        else
                            sensorsMap[indice] = new SensorsMapGraf(true,NodesList.GETSensores(j).GETnome(),NodesList.GETSensores(j).GETunidade(),meio.getBounds(),(int)meio.getWidth()/NodesList.GETnumSensoresActivos(),(int)meio.getHeight()/NodesList.GETnumSensoresActivos()-35,ymin,ymax); // cria o sensor
                    }

                    if (NodesList.GETnumSensoresActivos() > 4 && NodesList.GETnumSensoresActivos() <= 8)
                        sensorsMap[indice] = new SensorsMapGraf(true,NodesList.GETSensores(j).GETnome(),NodesList.GETSensores(j).GETunidade(),p.getBounds(),(int)meio.getWidth()/4-15,(int)meio.getHeight()/2 - 45,ymin,ymax); // cria o sensor

                    if (NodesList.GETnumSensoresActivos() > 8 && NodesList.GETnumSensoresActivos() <= 12)
                        sensorsMap[indice] = new SensorsMapGraf(true,NodesList.GETSensores(j).GETnome(),NodesList.GETSensores(j).GETunidade(),p.getBounds(),(int)meio.getWidth()/4-20,(int)meio.getHeight()/3-35,ymin,ymax); // cria o sensor

                        if (indice == 0)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 0;
                            c.gridy = 0;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 1)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 1;
                            c.gridy = 0;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 2)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            if (NodesList.GETnumSensoresActivos() <= 4)
                            {
                                c.gridx = 0;
                                c.gridy = 1;
                            }
                            else
                            {
                                c.gridx = 2;
                                c.gridy = 0;
                            }
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 3)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            if (NodesList.GETnumSensoresActivos() > 4)
                            {
                                c.gridx = 3;
                                c.gridy = 0;
                            }
                            else
                            {
                                c.gridx = 1;
                                c.gridy = 1;
                            }
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 4)
                        {

                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 0;
                            c.gridy = 1;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 5)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 1;
                            c.gridy = 1;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 6)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 2;
                            c.gridy = 1;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 7)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 3;
                            c.gridy = 1;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 8)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 0;
                            c.gridy = 2;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 9)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 1;
                            c.gridy = 2;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 10)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 2;
                            c.gridy = 2;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                        if (indice == 11)
                        {
                            c.fill = GridBagConstraints.HORIZONTAL;
                            c.gridx = 3;
                            c.gridy = 2;
                            meio.add(sensorsMap[indice].GetGraph().GETDemoPanel(),c);  //adidionar o panel p ao panel meio
                        }
                    indice++;
                }
            }

            //adicionar o meio_cima e o meio panel principal
            principal.add(meio_cima);
            principal.add(meio);

            Tabs.addTab(s, principal); // adicionar tab
            String ss = new String();
            ss += NodesList.GETSourceNodeID();

            GraphicsMap.add(new CentralMap(ValorTeste-1, ValorTeste-1, sensorsMap,ss,NodesList.GETLogicalNodeID()));  // adição de uma Tab
            GraphicsMap.get(ValorTeste-1).SetnumSensorsToShow(NodesList.GETnumSensoresActivos());

            posx = 20;
            posy = 20;

            CheckBoxNodes[ValorTeste-1] = new JCheckBox(s);
            CheckBoxNodes[ValorTeste-1].setBackground(null);
            CheckBoxNodes[ValorTeste-1].setLayout(new GridLayout(ValorTeste, 1, 0, 30));
            CheckBoxNodes[ValorTeste-1].setBounds(posx, posy, aux1-60, 30);
            CheckBoxNodes[ValorTeste-1].setSelected(false);
            CheckBoxNodes[ValorTeste-1].setOpaque(true);
            CheckBoxNodes[ValorTeste-1].setVisible(true);

            //Evento a tratar
            CheckBoxNodes[ValorTeste-1].addItemListener (this);

            pan3.add (CheckBoxNodes[ValorTeste-1]);
            pan1.add (Tabs);
     }
  }

I already tried to repaint pan2 but it didn't do anything.

thanks in advance


Solution

  • You're posting a lot of code completely unrelated to the problem, and confusing code at that, but it sounds as if you may have a Swing concurrency issue. Be sure to do the reading in of data on a background thread and the writing to the Swing components on the Swing event thread or EDT. For better help though, post code relevant to the problem. An sscce would work best.

    Also, if adding or removing components from a GUI, be sure to call revalidate() on the container and then repaint().