// implemented for ActionListener event handling public void actionPerformed(ActionEvent e) { String actionCmd = e.getActionCommand(); Stack locStack = parentBrowserFrame.locationStack; Stack fwdStack = parentBrowserFrame.forwardStack; if (actionCmd.equals(homeCmd)) // event from home button { fwdStack.removeAllElements(); parentBrowserFrame.setBrowserLocation(mainBrowserURL); } else if (actionCmd.equals(backCmd)) // event from back button { if (!locStack.isEmpty()) { String myLocale = (String) (locStack.pop()); // push current location on forward stack fwdStack.push(location); getForwardButton().setEnabled(true); // do *not* cache the last location in the stack parentBrowserFrame.setBrowserLocation(myLocale, false); } } else if (actionCmd.equals(forwardCmd)) // event from forward button { if (!fwdStack.isEmpty()) { // remove location from forward stack String newLoc = (String) (fwdStack.pop()); // DO add the current location to the back stack parentBrowserFrame.setBrowserLocation(newLoc); } } else if (actionCmd.equals(comboCmd)) // event from URL combo box! { if (e.getSource() instanceof JComboBox) // just to be sure { JComboBox thisBox = (JComboBox) e.getSource(); String newLoc = thisBox.getSelectedItem().toString(); if (newLoc != null && !newLoc.equals("")) // ignore empty selections { if (thisBox.getSelectedIndex() == -1) { thisBox.insertItemAt(newLoc, 0); } fwdStack.removeAllElements(); parentBrowserFrame.setBrowserLocation(newLoc); } } } // disable the back button if we find the location stack is empty if (locStack.isEmpty()) { getBackButton().setEnabled(false); } // disable forward button if forward stack is empty if (fwdStack.isEmpty()) { getForwardButton().setEnabled(false); } }
protected void addMyControls() { // add browser-style control buttons JButton home = new JButton(new ImageIcon("data/Home24.gif")); JButton back = new JButton(new ImageIcon("data/Back24.gif")); JButton fwd = new JButton(new ImageIcon("data/Forward24.gif")); home.setToolTipText("Home"); home.addActionListener(this); home.setActionCommand(homeCmd); back.setToolTipText("Back"); back.addActionListener(this); back.setActionCommand(backCmd); back.setEnabled(false); // initially disabled fwd.setToolTipText("Forward"); fwd.addActionListener(this); fwd.setActionCommand(forwardCmd); fwd.setEnabled(false); // initially disabled add(home); add(back); add(fwd); add(new JToolBar.Separator()); // set built-in index variables homeIndex = getComponentIndex(home); backIndex = getComponentIndex(back); forwardIndex = getComponentIndex(fwd); JComboBox comboBox = new JComboBox(); comboBox.setEditable(true); comboBox.addActionListener(this); comboBox.setActionCommand(comboCmd); comboBox.setMaximumRowCount(3); // don't let it get too long comboBox.insertItemAt(mainBrowserURL, 0); // don't start it out empty add(comboBox); comboBoxIndex = getComponentIndex(comboBox); }