Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble with a custom class in a JApplet

 
Dave Fletcher
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • 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
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!