Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Back and forth using stacks

Status
Not open for further replies.

chris921

Technical User
Mar 9, 2004
82
GB
Code:
ActionListener listenerB = new ActionListener() {
        public void actionPerformed(ActionEvent ev)
		{
		    try
		    {
		        fStack.push("[URL unfurl="true"]http://"[/URL] + address.getText().toLowerCase());
		        html.setPage((String) bStack.pop());
		        }
		        catch( Exception ex ) { new JOptionPane("Illegal URL, try again");}
        }
	};
				ActionListener listenerF = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        bStack.push("[URL unfurl="true"]http://"[/URL] + address.getText().toLowerCase());
                        html.setPage((String) fStack.pop());
                    } catch( Exception ex ) { new JOptionPane("Illegal URL, try again");}
                }
			};

		back.addActionListener(listenerB);
		forward.addActionListener(listenerF);

Code:
    final Stack fStack = new Stack();
    final Stack bStack = new Stack();

Can anyone see why this wont work? I wash for them to go back and forth like a webbrowser?
 
Yeah, but at the start there's nothing on either stack, so neither of the setPage methods will work since they do a pop on an (intially) empty stack to get their page.
 
I think you should indent a bit the code, it's difficult to understand this way.

Furthermore, if you tell us why is not working with an example, would be easier to answer.

Cheers,

Dian
 
Browser buttons (and undos have similar logic)

To start:
both stacks empty, buttons disabled

New site is gone to:
clear the forward stack (if not empty), push the current site on the history stack, enable the back (if it's not already), load the new page

Back:
ensure history stack not empty
push current site to forward stack (enable forward if not already enabled), pop site (load it) from history stack, if history stack is now empty disable the back button.

Forward:
ensure forward stack not empty
push current site to history stack (enable back if not enabled), pop site (load it) from the forward stack, if the forward stack is now empty disable the forward button.

The other way to do this is a doublly linked list with a current iterator... this can be more will be more efficent if done right.

[plug=shameless]
[/plug]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top