• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Back Forward in java application

 
shan sonata
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i'm doing some java programmings and need help from you.
Here's the question:
I'm doing Back Forward task by using Back and Forward Buttons.
It's in not in applet like someone posted before. These're variables:

JPanel ContentPrevious, ContentCurrent, ContentNext;
JButton BackButton, ForwardButton;
boolean BackActivated = true;
boolean ForwardActivated = false;
Action action = new Action();
//////////////////////////////
//
//somewhere call action
//
//////////////////////////////////

public class Action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (BackActivated)
{
if (e.getSource()==BackButton)
{
ContentNext = new JPanel();
ContentNext = ContentCurrent; //problem: ContentNext doesn't take ContentCurrent value
ContentCurrent.setVisible(false);
Content.remove(ContentCurrent);
ContentCurrent = new JPanel();
ContentCurrent = ContentPrevious;//problem: ContentCurrent doesn't take ContentPrevious value
Content.add(ContentCurrent);
forwardActivated = true;
BackActivated = false;
}
}

else if (forwardActivated)
{
if (e.getSource()==ForwardButton)
{
ContentPrevious = new JPanel();
ContentPrevious = ContentCurrent;//same problem

ContentCurrent.setVisible(false);
Content.remove(ContentCurrent);
ContentCurrent = new JPanel();
ContentCurrent = ContentNext;//same problem
Content.add(ContentCurrent);
forwardActivated = false;
BackActivated = true;

}
}
}
}

Hope anyone can help me.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope anyone can help you what? Are you getting errors? You need to tell us what problems you are having so that we know what you need help with.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm also not sure what you're after, but copy/compile/run this to see the JPanels changing.
 
shan sonata
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for not telling you clearer. The problems are in comments in the code i posted. It didn't work properly.
.....
ContentNext = ContentCurrent; //problem: ContentNext doesn't take ContentCurrent value
.....

ContentNext didn't take the value of ContentCurrent like in C++?!
Michael, i tried ur code and it works properly. But there's only 3 panel.
My problem is: i have lots of panel and each time i show a panel in the frame, it becomes ContentCurrent.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still not sure I'm reading this correctly - you have multiple JPanels and you want to show the next one, or the previous one, depending on whether a forward or back button is clicked. If so, perhaps one way might be to have an array of JPanels and a counter, then if forward button is selected, add 1 to the counter then add JPanel[ctr] etc.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't this be a perfect use for CardLayout?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic