Simple JPanel Problem

Hello all,

I have back and next buttons to go through a set of slides (JPanels) in a GUI (JFrame), but only the next button works - even when both the getNextSlide() and getPreviousSlide() methods are nearly identical.
 
    private class JSlideListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.out.println(e.getActionCommand());
            GUI.getContentPane().removeAll();
            GUI.getContentPane().validate();
            if (e.getActionCommand() == "Next") {
                slideCount++;
                getNextSlide();
            } else if (e.getActionCommand() == "Back") {
                slideCount--;
                getPreviousSlide();
            }
        }
    }
 
This is my listener, and the weird thing is, if I go forward two slides from 0, 1, 2 etc until the end it works. If I use back, the same slide stays in place. However, If I go 0, 1, 2, and then back to 1, and then forward 2, 3, slide 3 then appears. Is it something to do with slides already been displayed not going to work? Here are the methods Im using:
 
    public void getNextSlide() {
        System.out.println("getting slide + " + slideCount);
        if (slideCount < numSlides) {
            GUI.getContentPane().add(nextSlideBut, BorderLayout.EAST);
            GUI.getContentPane().validate();
            GUI.getContentPane().add(prevSlideBut, BorderLayout.WEST);
            GUI.getContentPane().validate();
            GUI.getContentPane().add(SLIDES[slideCount], BorderLayout.CENTER);
            GUI.getContentPane().validate();
        } else {
            System.out.println("no more slides");
            System.exit(0);
        }
    }
 
    public void getPreviousSlide() {
        System.out.println("getting slide + " + slideCount);
        if (slideCount >= 0) {
            GUI.getContentPane().add(nextSlideBut, BorderLayout.EAST);
            GUI.getContentPane().validate();
            GUI.getContentPane().add(prevSlideBut, BorderLayout.WEST);
            GUI.getContentPane().validate();
            GUI.getContentPane().add(SLIDES[slideCount], BorderLayout.CENTER);
            GUI.getContentPane().validate();
        } else {
            System.out.println("no more slides");
            System.exit(0);
        }
    }
LVL 2
mtcmediaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
>>            if (e.getActionCommand() == "Next") {
should be:
  if (e.getActionCommand().equals("Next")) {

same to other if conditions
0
petmagdyCommented:
or better:

  if ("Next".equals( e.getActionCommand() ) )
0
mtcmediaAuthor Commented:
Thanks for that, thats not the problem though :(
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

zzynxSoftware engineerCommented:
Other side remark:

This
            GUI.getContentPane().add(nextSlideBut, BorderLayout.EAST);
            GUI.getContentPane().validate();
            GUI.getContentPane().add(prevSlideBut, BorderLayout.WEST);
            GUI.getContentPane().validate();
            GUI.getContentPane().add(SLIDES[slideCount], BorderLayout.CENTER);
            GUI.getContentPane().validate();
can be just
            GUI.getContentPane().add(nextSlideBut, BorderLayout.EAST);
            GUI.getContentPane().add(prevSlideBut, BorderLayout.WEST);
            GUI.getContentPane().add(SLIDES[slideCount], BorderLayout.CENTER);
            GUI.getContentPane().validate();
0
mtcmediaAuthor Commented:
Thanks :) Any idea what could be causing this  problem?
0
zzynxSoftware engineerCommented:
Could you try to replace the

>> GUI.getContentPane().validate();

by

GUI.getContentPane().revalidate();
GUI.getContentPane().repaint();
0
mtcmediaAuthor Commented:
Cannot resolve symbol method revalidate()
0
zzynxSoftware engineerCommented:
Think you'll need a cast:

((JComponent)GUI.getContentPane()).revalidate();
GUI.getContentPane().repaint();
0
zzynxSoftware engineerCommented:
sorry for that
0
mtcmediaAuthor Commented:
Thats it! What was the problem?
0
zzynxSoftware engineerCommented:
The new slide was there, but you didn't see it because it wasn't redrawn.
0
zzynxSoftware engineerCommented:
...and apparently a validate() doesn't force the redraw.
While I experienced that the combination I gave you does.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSoftware engineerCommented:
Thanks :)
0
zzynxSoftware engineerCommented:
Well, your subject title seemed to be quite correct ;°)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.