[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

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);
        }
    }
0
mtcmedia
Asked:
mtcmedia
  • 8
  • 4
  • 2
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
zzynxSoftware engineerCommented:
Thanks :)
0
 
zzynxSoftware engineerCommented:
Well, your subject title seemed to be quite correct ;°)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now