Java-Newbie
Java-Newbie

Reputation: 43

Changing only one Panel in JFrame

I have one JFrame with two Buttons in East and West,Label in North,Label and TextField in South. and one Image in the Center. I want to call another Image after 30 seconds in the center. But every time I call the 2nd image the components in North,South,East and West are disappearing.

This is my code. //The Components in JFrame.

    firstPicblur = new JPanel(new BorderLayout());
    pictureblur01 = new ImageIcon("C:\\java pics\\papsi2.jpg");
    pictureblurA = new JLabel(pictureblur01);

    firstPicblur.add(pictureblurA,BorderLayout.CENTER);

    //FirstPicture blurred B
    firstPicblurB = new JPanel(new BorderLayout());
    pictureblur02 = new ImageIcon("C:\\java pics\\papsi1.jpg");
    pictureblurB = new JLabel(pictureblur02);

    firstPicblurB.add(pictureblurB,BorderLayout.CENTER);

    //Next0Prev buttons
    Next0Prev = new JPanel(new BorderLayout());
    Next = new JButton("NEXT");
    Prev = new JButton("PREV");

    //Next0Prev labels is constant at SOUTH
    firstPicblurA = new JPanel(new BorderLayout());
    clueNo1 = new JLabel("Picture Number 01");
    TypeHere = new JTextField("Guess Who");

    firstPicblurA.add(TypeHere, BorderLayout.CENTER);
    firstPicblurA.add(clueNo1, BorderLayout.SOUTH);

    Next0Prev.add(firstPicblur,BorderLayout.CENTER);
    Next0Prev.add(Next,BorderLayout.EAST);
    Next0Prev.add(Prev,BorderLayout.WEST);
    Next0Prev.add(firstPicblurA,BorderLayout.SOUTH);

//other components

and this is the actionEvent

        if(e.getSource() == continueButton){
        add(Next0Prev);
        Next0Prev.setVisible(true);
        Next0Prev.repaint();
        Next0Prev.revalidate();
        loadingEffectBtn.setVisible(false);
        Timer ta = new Timer(10000, new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                         add(firstPicblurB);
                         firstPicblurB.setVisible(true);
                         Next0Prev.setVisible(true);
                        }
                        });
                        ta.start();
                        ta.setRepeats(true);
    }

but when i do this. The 2nd picture is appearing in center but the N,E,W,S components are disappearing.

Upvotes: 0

Views: 123

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

This is a bit hard to tell, but what appears to be happening is...

You're creating Next0Prev and adding all your components to it...

Next0Prev = new JPanel(new BorderLayout());

//....

Next0Prev.add(firstPicblur,BorderLayout.CENTER);
Next0Prev.add(Next,BorderLayout.EAST);
Next0Prev.add(Prev,BorderLayout.WEST);
Next0Prev.add(firstPicblurA,BorderLayout.SOUTH);

Which I assume you are adding to the frame...

Then in your Timer's actionPerformed, you're adding firstPicblurB to the main container...

public void actionPerformed(ActionEvent e) {
    add(firstPicblurB);
    firstPicblurB.setVisible(true);
    Next0Prev.setVisible(true);
}

Now, assuming that the main container is either a JFrame or is using a BorderLayout, this will effectively hide Next0Prev as BorderLayout can only have a single component in any of it's 5 positions.

Instead, you should be adding firstPicblurB to Next0Prev...

A simpler solution would be (as has already being pointed out) to use a single JLabel and simple change it's icon

I would also highly recommend that you take the time to read through and apply Code Conventions for the Java Programming Language

Upvotes: 1

Related Questions