Closed Thread Icon

Topic awaiting preservation: Problem with ArrayList (Java) (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=11915" title="Pages that link to Topic awaiting preservation: Problem with ArrayList (Java) (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Problem with ArrayList (Java) <span class="small">(Page 1 of 1)</span>\

 
WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 10-20-2001 07:49

The code below illustrates the main functions I am using to control the history of a web browser. newURL works on all url's entered into an address bar, and links taken, within web pages.
Back and Forward are the back and Forward button functions.

IMO, this code should work. The problem is that the remove function is not working at all... it just isn't removing the elements. And I don't know why... Could I get some assistance on this?

code:
public void newURL(String url)
{
if (currentPage.equals(null) &#0124; &#0124; currentPage.equals(""))
{
//Do nothing
}
else
backHistory.add(0,currentPage);
currentPage = url;
forwardHistory.clear();
}

public String forward()
{
backHistory.add(0,currentPage);
currentPage = forwardHistory.get(0);
forwardHistory.remove(0);
return (String)currentPage;
}

public String back()
{
forwardHistory.add(0,currentPage);
currentPage = backHistory.get(0);
backHistory.remove(0);
return (String)currentPage;
}



InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-20-2001 12:40

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 10-20-2001 19:29

No, do not mind showing the entire code. It is not a hard bit of code to work with.
What I did was I had a toString function which would display right on the GUI what
was going on with the variables. And what was happening, was they were not being
removed, as called by the functions.

<BLOCKQUOTE><FONT face="Verdana, Arial">code:</font><HR><pre>
import java.util.*;

/** Browser history class. This class functions
* linearly, to store and get all names.
*/

class BrowserHistory
{

private ArrayList backHistory, forwardHistory;
private Object currentPage = "";

public BrowserHistory()
{
backHistory = new ArrayList();
forwardHistory = new ArrayList();
}

public void newURL(String url)
{
if (currentPage.equals(null)

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 10-22-2001 02:18

Bump for assistance.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-22-2001 04:33

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 10-22-2001 06:34

No, I can wait and I will keep you posted.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-22-2001 23:14

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 10-23-2001 02:59

Ok. Thanks for that info and for putting the time into helping me out!

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-23-2001 10:31

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-23-2001 14:11

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 10-23-2001 17:29

I have imported java.util, and I am able to get things printed from the ArrayList, however the remove does not work... this is where the problem is... Everything is working fine, except the remove method... it is not being called or something...

I just don't get it...

I am adding elements to the beginning in all my functions and removing the 0th element...

I just don't understand why it is not working...

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-23-2001 18:13

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-23-2001 18:21

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 10-23-2001 19:20

Well I am going to post the calling code... Who knows, maybe just so you could get peace of mind... The other called classes... too...

browser.java

code:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

/** Very simplistic "Web browser" using Swing. Supply a URL on the
* command line to see it initially, and to set the destination
* of the "home" button.
* 1998 Marty Hall, [url=http://www.apl.jhu.edu/~hall/java/]http://www.apl.jhu.edu/~hall/java/[/url]
*/

public class Browser extends JFrame implements HyperlinkListener,
ActionListener {
public static void main(String[] args) {
if (args.length == 0)
new Browser("http://www.cs.sunysb.edu/~cse333/Section2/");
else
new Browser(args[0]);
}

private JIconButton homeButton, backButton, forwardButton;
private JTextField urlField;
private JEditorPane htmlPane;
private String initialURL;
private JLabel testLabel;

/** Create a new instance of the browser history.
*/

private BrowserHistory historyControl = new BrowserHistory();

public Browser(String initialURL) {
super("Daniel Curran Project 5");
this.initialURL = initialURL;
addWindowListener(new ExitListener());
WindowUtilities.setNativeLookAndFeel();

JPanel topPanel = new JPanel();
topPanel.setBackground(Color.lightGray);

/** Add the back and forwad buttons
* to the top panel.
*/
homeButton = new JIconButton("home.gif");
homeButton.addActionListener(this);

backButton = new JIconButton("back.gif");
backButton.setEnabled(false);
backButton.addActionListener(this);

forwardButton = new JIconButton("forward.gif");
forwardButton.setEnabled(false);
forwardButton.addActionListener(this);

JLabel urlLabel = new JLabel("URL:");
urlField = new JTextField(30);
urlField.setText(initialURL);
urlField.addActionListener(this);

//TEST

testLabel = new JLabel("Sizes (forward, backward): ");
topPanel.add(testLabel);

//TEST

/** Place the new forward and back
* button on the topPanel.
*/

topPanel.add(backButton);
topPanel.add(forwardButton);

topPanel.add(homeButton);
topPanel.add(urlLabel);
topPanel.add(urlField);

getContentPane().add(topPanel, BorderLayout.NORTH);

try {
htmlPane = new JEditorPane(initialURL);

/** Code to set the current page to the initial URL.
*/

historyControl.newURL(initialURL);
if (historyControl.backIsEmpty())
backButton.setEnabled(false);
else
backButton.setEnabled(true);
if (historyControl.forwardIsEmpty())
forwardButton.setEnabled(false);
else
forwardButton.setEnabled(true);

//TEST
testLabel.setText("" + historyControl.toString() + "");
//TEST

htmlPane.setEditable(false);
htmlPane.addHyperlinkListener(this);
JScrollPane scrollPane = new JScrollPane(htmlPane);
getContentPane().add(scrollPane, BorderLayout.CENTER);
} catch(IOException ioe) {
warnUser("Can't build HTML pane for " + initialURL
+ ": " + ioe);
}

Dimension screenSize = getToolkit().getScreenSize();
int width = screenSize.width * 8 / 10;
int height = screenSize.height * 8 / 10;
setBounds(width/8, height/8, width, height);
setVisible(true);
}

public void actionPerformed(ActionEvent event) {
String url = "";
if (event.getSource() == urlField){
url = urlField.getText();

historyControl.newURL(url);
if (historyControl.backIsEmpty())
backButton.setEnabled(false);
else
backButton.setEnabled(true);
if (historyControl.forwardIsEmpty())
forwardButton.setEnabled(false);
else
forwardButton.setEnabled(true);
//TEST
testLabel.setText("" + historyControl.toString() + "");
//TEST
}
else if(event.getSource() == forwardButton)
{
url = historyControl.forward();
if (historyControl.backIsEmpty())
backButton.setEnabled(false);
else
backButton.setEnabled(true);

if (historyControl.forwardIsEmpty())
forwardButton.setEnabled(false);
else
forwardButton.setEnabled(true);
//TEST
testLabel.setText("" + historyControl.toString() + "");
//TEST

}
else if(event.getSource() == backButton)
{
url = historyControl.back();

if (historyControl.backIsEmpty())
backButton.setEnabled(false);
else
backButton.setEnabled(true);

if (historyControl.forwardIsEmpty())
forwardButton.setEnabled(false);
else
forwardButton.setEnabled(true);

//TEST
testLabel.setText("" + historyControl.toString() + "");
//TEST

}
else // Clicked "home" button instead of entering URL
url = initialURL;

historyControl.newURL(url);
if (historyControl.backIsEmpty())
backButton.setEnabled(false);
else
backButton.setEnabled(true);
if (historyControl.forwardIsEmpty())
forwardButton.setEnabled(false);
else
forwardButton.setEnabled(true);

//TEST
testLabel.setText("" + historyControl.toString() + "");
//TEST

try {
htmlPane.setPage(new URL(url));
urlField.setText(url);
} catch(IOException ioe) {
warnUser("Can't follow link to " + url + ": " + ioe);
}
}

public void hyperlinkUpdate(HyperlinkEvent event) {
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
htmlPane.setPage(event.getURL());

historyControl.newURL(event.getURL().toExternalForm());
if (historyControl.backIsEmpty())
backButton.setEnabled(false);
else
backButton.setEnabled(true);
if (historyControl.forwardIsEmpty())
forwardButton.setEnabled(false);
else
forwardButton.setEnabled(true);

//TEST
testLabel.setText("" + historyControl.toString() + "");
//TEST

urlField.setText(event.getURL().toExternalForm());
} catch(IOException ioe) {
warnUser("Can't follow link to "
+ event.getURL().toExternalForm() + ": " + ioe);
}
}
}

private void warnUser(String message) {
JOptionPane.showMessageDialog(this, message, "Error",
JOptionPane.ERROR_MESSAGE);
}
}



exitlistner.java

code:
import java.awt.*;
import java.awt.event.*;

/** A listener that you attach to the top-level Frame or JFrame of
* your application, so quitting the frame exits the application.
* 1998-99 Marty Hall, [url=http://www.apl.jhu.edu/~hall/java/]http://www.apl.jhu.edu/~hall/java/[/url]
*/

public class ExitListener extends WindowAdapter {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
}



JIconButton.java

code:
import javax.swing.*;

/** A regular JButton created with an ImageIcon and with borders
* and content areas turned off.
* 1998 Marty Hall, [url=http://www.apl.jhu.edu/~hall/java/]http://www.apl.jhu.edu/~hall/java/[/url]
*/

public class JIconButton extends JButton {
public JIconButton(String file) {
super(new ImageIcon(file));
setContentAreaFilled(false);
setBorderPainted(false);
setFocusPainted(false);
}
}



WindowUtilities.java

code:
import javax.swing.*;
import java.awt.*;

/** A few utilities that simplify using windows in Swing.
* 1998-99 Marty Hall, [url=http://www.apl.jhu.edu/~hall/java/]http://www.apl.jhu.edu/~hall/java/[/url]
*/

public class WindowUtilities {

/** Tell system to use native look and feel, as in previous
* releases. Metal (Java) LAF is the default otherwise.
*/

public static void setNativeLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
System.out.println("Error setting native LAF: " + e);
}
}

public static void setJavaLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e) {
System.out.println("Error setting Java LAF: " + e);
}
}

public static void setMotifLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
} catch(Exception e) {
System.out.println("Error setting Motif LAF: " + e);
}
}

/** A simplified way to see a JPanel or other Container.
* Pops up a JFrame with specified Container as the content pane.
*/

public static JFrame openInJFrame(Container content,
int width,
int height,
String title,
Color bgColor) {
JFrame frame = new JFrame(title);
frame.setBackground(bgColor);
content.setBackground(bgColor);
frame.setSize(width, height);
frame.setContentPane(content);
frame.addWindowListener(new ExitListener());
frame.setVisible(true);
return(frame);
}

/** Uses Color.white as the background color. */

public static JFrame openInJFrame(Container content,
int width,
int height,
String title) {
return(openInJFrame(content, width, height, title, Color.white));
}

/** Uses Color.white as the background color, and the
* name of the Container's class as the JFrame title.
*/

public static JFrame openInJFrame(Container content,
int width,
int height) {
return(openInJFrame(content, width, height,
content.getClass().getName(),
Color.white));
}
}



InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-24-2001 09:34

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 10-24-2001 19:05

I didn't write the browser.

I am just doing the graphics and adding functionality.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 10-24-2001 22:14

My professor told me that a similar problem came to his attention yesterday... "Methods not working in JDK"

He said do a search on google about it. So I will do that.

I probabally will have to make a basic collection class to handle this stuff... It is a bit of a pain to re-write what has been written...

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-25-2001 01:04

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 10-25-2001 04:49

No, I don't mind redoing code.

However, I do mind redoing code when I have something much more useful that I could be redoing.

This code is pretty trivial, thus it is not much of an excercise to redo it, no to even create it in the beginning, so it is not much of a learning experience. I have much work to do for my job right now, that I am really getting behind in, as well as a project in mips assembly which is causing major headaches.

So, in the end I don't mean to rant. But I am, I have more important matters to attend to, and the least important of all is becoming the hassel.

« BackwardsOnwards »

Show Forum Drop Down Menu