Hello DaniWeb community! I come to you in need of some guidance on a quite frustrating problem.

In a nutshell, I am building a java system in which users can keep track of weather forecasts at a list of registered locations, which they can add or remove from their "watchlist".

I have built the database infrastructure and a login system which all works fine, but I am struggling with how to handle the next task, which is representing a user's watchlist in some visible medium. I have currentley stored a user's "watchlist" in another MS Access database with the user's ID and a String of the name of the location they are watching.

My plan is then to convert this into tangible Location objects which can then have their weather statistics changed. I have devised a method that creates these Location objects on the fly by creating them as the method queries the ResultSet of the watchlist database. These Location objects are then stored in an ArrayList.

NOW, here is my problem. I am trying to basically fill the ArrayList with the appropriate locations in one class, then "unpack" the list in a GUI class, and add the contained Location objects to some visible list (perhaps a JList). I have been trying to use XMLEncoder/Decoder to accomplish this by unpacking the ArrayList into another local ArrayList in the GUI class, but this hasn't worked. I keep getting "java.lang.InstantiationException"s.

Can anybody give me some help on a process like this? I am just trying to export the correct set of Location objects to a GUI class where they can there be displayed and manipulated accordingly.

Many thanks in advance for any help. I feel as though my project has hit a standstill - I just can't figure out how to solve this task...

Define a GUI class that has a constructor which takes an ArrayList<Location> as its argument.

public class YourGUI{
public YourGUI(ArrayList<Location> list){
//add it to a JList here, add that JList to a JPanel & JFrame, show the JFrame, etc
}
}

Your project statement seems quite complicated so I find it very likely that you already know the information I just gave you. Is there any particular reason why you need to "unpack" the ArrayList? If you have an ArrayList of Location Objects... and the Location Objects contain readable information (such as Strings) then it should be ok.

Oh, and I forgot to mention: after you finished setting up this GUI class, from within your "main" class (where you have the ArrayList you were discussing) you would create a new YourGUI(arrayList) and that would be it.

You can easily instantiate a Jlist object with your ArrayList Object by passing the Arraylist to a JList Constructor:

Jlist myList = new Jlist(MyArrayList);

By the way, I've created a sample class that does it:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;


public class MainApp extends JFrame {
	
	private JList weatherList;
	private JScrollPane listScroller;
	
	
	private void initializeCOmponents(ArrayList myArrayList){
		
		weatherList = new JList( myArrayList.toArray() );
		weatherList.setLayoutOrientation(JList.VERTICAL);
		weatherList.setVisibleRowCount(-1);
		
		listScroller = new JScrollPane(weatherList);
		listScroller.setPreferredSize(new Dimension(150, 80));
		listScroller.setAlignmentX(LEFT_ALIGNMENT);


	}
	
	
	
	private void configureLayout(){

		this.setLayout(new FlowLayout());
		
		getContentPane().add(listScroller);
		
		this.setSize(300, 600);
		
		this.setVisible(true);
		pack();
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
	}

	public MainApp(ArrayList<String> weatherList){
		initializeCOmponents(weatherList);
		configureLayout();
	}
	
	
	public static void main(String[] args) {

		ArrayList<String> weatherList =  new ArrayList<String>();
		
		weatherList.add("weatherItem_001");
		weatherList.add("weatherItem_002");
		weatherList.add("weatherItem_003");
		weatherList.add("weatherItem_004");
		weatherList.add("weatherItem_005");
		weatherList.add("weatherItem_006");
		weatherList.add("weatherItem_007");
		weatherList.add("weatherItem_008");
		weatherList.add("weatherItem_009");
		weatherList.add("weatherItem_010");
		weatherList.add("weatherItem_011");
		weatherList.add("weatherItem_012");
		weatherList.add("weatherItem_013");
		weatherList.add("weatherItem_014");
		weatherList.add("weatherItem_015");
		weatherList.add("weatherItem_016");
		
		new MainApp(weatherList);
	}
}

Note that this is just an example. If you plan really to use this for production, I suggest that you go deeper into the Swing java API.

You can use the SUN tutorials to do through this: http://java.sun.com/docs/books/tutorial/ui/features/components.html

Cheers,
Ricardo

Define a GUI class that has a constructor which takes an ArrayList<Location> as its argument.

public class YourGUI{
public YourGUI(ArrayList<Location> list){
//add it to a JList here, add that JList to a JPanel & JFrame, show the JFrame, etc
}
}

Your project statement seems quite complicated so I find it very likely that you already know the information I just gave you. Is there any particular reason why you need to "unpack" the ArrayList? If you have an ArrayList of Location Objects... and the Location Objects contain readable information (such as Strings) then it should be ok.

Oh, and I forgot to mention: after you finished setting up this GUI class, from within your "main" class (where you have the ArrayList you were discussing) you would create a new YourGUI(arrayList) and that would be it.

Thanks for your replies guys.

I suppose my main issue is getting the contents of my arraylist (or the list itself) from my main implementation class to the GUI class that I plan to make use of it.

public class YourMainClass{

public static void main(String[] args){
// Up here you created your arraylist and put Location objects in it
new YourGUI(yourArrayList);
}
}

That is all you need to do. Let the YourGUI class handle putting the ArrayList inside the JList and then showing the GUI. Here is a good tutorial on how to use JList

Hi guys.

Thanks to your advice, I now have the ArrayList I want inside my GUI class. However, whatever I try to add the Location objects (in the ArrayList) to my JList, I get a bunch of Java hashcode memory references:

Locations.Location@197a37c

And so forth...

This leads me to believe there may be something wrong with my Location objects, but surely this is as simple as just retrieving what is inside the ArrayList rather than a reference to it...

I have tried

model.addElement(physicalWatchList);
model.addElement(physicalWatchList.toString());
model.addElement(physicalWatchList.toArray);

among other things, but nothing has worked yet.

I think you might be trying to add your entire ArrayList into the JList, which would be a mistake. You can do something like this:

ArrayList<Location> physicalWatchList = new ArrayList<Location>();
// .. some code to add Locations to the physicalWatchList goes here
DefaultListModel model = new DefaultListModel();
for (Location loc: physicalWatchList) model.addElement(loc.toString());
JList list = new JList(model);

//Some time later when you want to add more elements
list.getModel().addElement(newLocation.toString());

Also, make sure your Location class actually overrides the toString method from the Object class otherwise it is not going to output what you might want it to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.