-2

I am making a travel planner desktop app. And I have come so far so its only the listing left. I wanna use an Jlist with a defaultListModel or something. But how Do I list all like this???

The result I wanna get is like this:

line : Spårvagn 6
direction : Kortedala via Sahlgrenska
Starting Location : Brunnsparken, Göteborg
time : 17:27
date : 2013-04-03
track : E 
Destination : Svingeln, Göteborg
time : 17:56
date : 2013-04-03
track : B 
line : Spårvagn 1
direction : Östra Sjukhuset
Starting Location : Brunnsparken, Göteborg
time : 17:30
date : 2013-04-03
track : D 
Destination : Svingeln, Göteborg
time : 17:36
date : 2013-04-03
track : B 
line : Spårvagn 3
direction : Kålltorp
Starting Location : Brunnsparken, Göteborg
time : 17:35
date : 2013-04-03
track : D 
Destination : Svingeln, Göteborg
time : 17:41
date : 2013-04-03
track : B 
line : Spårvagn 6
direction : Kortedala via Sahlgrenska
Starting Location : Brunnsparken, Göteborg
time : 17:36
date : 2013-04-03
track : E 
Destination : Svingeln, Göteborg
time : 18:05
date : 2013-04-03
track : B 
line : Spårvagn 1
direction : Östra Sjukhuset
Starting Location : Brunnsparken, Göteborg
time : 17:40
date : 2013-04-03
track : D 
Destination : Svingeln, Göteborg
time : 17:46
date : 2013-04-03
track : B 

My code :

JSONObject destination = (JSONObject) jsonObj.get("Destination");
            String dname = (String) destination.get("name");
            String dtime = (String) destination.get("time");
            String ddate = (String) destination.get("date");
            String dtrack =(String) destination.get("track");

            //set the info about the destination
            TripService.getTrip().setDate(date);
            TripService.getTrip().setName(dname);
            TripService.getTrip().setTime(dtime);
            TripService.getTrip().setTrack(dtrack);

            //Sets line and direction for all trips
/*          
            model.addElement("line : " + line);
            model.addElement("direction : " + direction);

            //set the info about the starting location

            model.addElement("Starting Location : " + name);
            model.addElement("Time : " + time);
            model.addElement("Date : " + date);
            model.addElement("Track : " + track);
*/  

But it return an [] in my console. How to accomplish this

All this is listed only in my console with this output. But I am not able to transfer it into my application. Whats wrong and how to do this?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Josef
  • 527
  • 2
  • 5
  • 22

2 Answers2

1

The problem is not adding the data to the ListModel. All you do is create an object that contains all the information you want displayed and add the object to the model.

The problem is displaying all the information. For that you will need a custom renderer. See the section from the Swing tutorial on Writing a Custom Cell Renderer.

camickr
  • 321,443
  • 19
  • 166
  • 288
0
DefaultListModel model = new DefaultListModel();
   JList list = new JList(model);

now add the data in model using model.addElement()

Biswajit
  • 2,434
  • 2
  • 28
  • 35