-1

I am having an array of JLabels with custom icons and 3 buttons.First time, a single button is displayed(Start) when click the panel repaints and the labels are displayed: https://i.stack.imgur.com/BHqa2.jpg . When i click the 2nd one(Urmatorul) i want the content of the pane to be erased and then replace the labels, but this is how it looks : https://i.stack.imgur.com/Uc5GH.jpg .The last button is an exit button. Also, i think is about the GridBagConstraints..

static JLabel[] label = new JLabel[words[random].length()];  // in the main class

//in the JPanel class

    for(int i = 0; i < label.length; i++)
            label[i] = new JLabel(box);

        add(sButton);

        //This is the first button ( that clears the jpanel for the first time and adds the 2 buttons + jlabels

        sButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                remove(sButton);
                c.gridx = 3;
                add(text,c);
                c.gridx = 4;
                add(field, c);
                c.gridx = 0;
                for(int i = 0; i < label.length; i++){

                    c.gridy = 1;
                    c.gridx++;
                    c.insets = new Insets(400,0,0,0);
                    add(label[i],c);
                                    }
                c.insets = new Insets(0,0,0,0);
                c.gridy ++;
                c.gridx = 0;    
                c.gridwidth = 4;
                c.weighty = 0.09;
                c.anchor = GridBagConstraints.SOUTH;
                add(eButton, c);
                c.gridx = 3;
                add(nButton, c);
                revalidate();
                repaint();


            }

        });

        eButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });

        // The second button, which i am having trouble with ( i just copied the code from the first button listener)

        nButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

            label = new JLabel[words[random].length()];
            for(int i = 0; i < label.length; i++)
                label[i] = new JLabel(box);
            removeAll();
            revalidate();
            repaint();
                    c.gridx = 3;
                    add(text,c);
                    c.gridx = 4;
                    add(field, c);
                    c.gridx = 0;
                    for(int i = 0; i < label.length; i++){

                        c.gridy = 1;
                        c.gridx++;
                        c.insets = new Insets(400,0,0,0);
                        add(label[i],c);
                                        }
                    c.insets = new Insets(0,0,0,0);
                    c.gridy ++;
                    c.gridx = 0;    
                    c.gridwidth = 4;
                    c.weighty = 0.09;
                    c.anchor = GridBagConstraints.SOUTH;
                    add(eButton, c);
                    c.gridx = 3;
                    add(nButton, c);
                    revalidate();
                    repaint();
             }
        });
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
AXL
  • 17
  • 1
  • 6
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Feb 04 '15 at 12:06

1 Answers1

0

repaint(); and revalidate(); should work, but try add and remove new visible component to visible JPanel and tell us what happens.

Anptk
  • 1,125
  • 2
  • 17
  • 28
UserLuke
  • 177
  • 13
  • what you mean by add and remove new visible components ? to add something diferent ? like a jbutton ? – AXL Feb 04 '15 at 10:05