• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble with a custom class in a JApplet

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey there. I am new to the forum. I have a java class and this question pertains to a portion of an assignment I have been given, and I am severely stuck because I do not fully grasp the concept of how data flows between classes and methods and all that stuff. I have been reading and reading for hours various sources online and in java books. So know that I am not just asking for a quick answer, or for anyone to do my work for me. Also there seems to be a significant difference between what our professor is teaching us and how books and online sources describe how to program in Java, which makes it really hard for me to keep track of what I am trying to figure out . My class seems like its mainly a Java Applet/GUI class and that's what we've mostly covered and we didnt even go over making classes/methods. I do understand the various concepts of the swing components and things, but like I said, I am having trouble with more of the programming logic stuff regarding custom classes and their methods specifically.

I will paste the code for my applet and could someone perhaps show me how I might need to rearrange my code so that it can work, or maybe I just need to add a simple statement in there that will allow the data to flow properly..

When I attempt to compile the code, I get this error:

cannot find symbol: method addItem(java.lang.String)
location: class javax.swing.JList
itemList.addItem(items[i].description);

NOTE: Line 53 is where I have a loop statement that I am trying to get to go through the 4 items and pull out their descriptions and then add them to the JList.

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know of a JList.addItem(String) method. Even the Javadocs for JList give an example of adding a bunch of Strings--maybe that'll help.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
addItem() would belong to awt.List, which you do not want to mix in with your Swing components

some options:
1) use a DefaultListModel, and its addElement(), then set the list's model
2) a JList constructor takes an Object[], the trick is to override invItem()'s toString(),
to return the description. The description will then appear in the JList
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave,
Welcome to the Ranch.

We have a dedicated forum for UI related questions where your post would suit better. I will move this thread over there for you.
In the meanwhile please do check out http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
 
Grow your own food... or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic