aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JList help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JList help." Watch "JList help." New topic
Author

JList help.

Andrew Geroge Alexander
Ranch Hand

Joined: Feb 14, 2012
Posts: 31
I have created a JList through IDE. The list have been declared as,

private javax.swing.JList jList1;

now i want to add data to the list, i have a string array in which i have the values, but when i try adding by using the following,

for(int i=0;i<=5;i++)
jList1.add(i,tmpArrya[i]);


no suitable method found for add(int,java.lang.String)
method java.awt.Container.add(java.awt.Component,java.lang.Object,int) is not applicable
(actual and formal argument lists differ in length)
method java.awt.Container.add(java.awt.Component,java.lang.Object) is not applicable
(actual argument int cannot be converted to java.awt.Component by method invocation conversion)
method java.awt.Container.add(java.awt.Component,int) is not applicable
(actual argument int cannot be converted to java.awt.Component by method invocation conversion)
method java.awt.Container.add(java.lang.String,java.awt.Component) is not applicable
(actual argument int cannot be converted to java.lang.String by method invocation conversion)
method java.awt.Container.add(java.awt.Component) is not applicable
(actual and formal argument lists differ in length)
method java.awt.Component.add(java.awt.PopupMenu) is not applicable
(actual and formal argument lists differ in length)

I am not sure of what half of it means either. How will I be able to add the values to the list dynamically??
Any help will be greatly appreciated.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
The message is saying that the JList class does not have an add method that takes an int and a String as parameters.
You need to use a different add method (or maybe a different method altogether) to do whatever it is you are trying to do


Joanne
Andrew Geroge Alexander
Ranch Hand

Joined: Feb 14, 2012
Posts: 31
How will I be able to add the values to the list dynamically??
Any help will be greatly appreciated.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

A JList has a ListModel that contains the actual data. You need to use a ListModel implementation that allows you to dynamically add to it. Check out DefaultListModel.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Andrew Geroge Alexander
Ranch Hand

Joined: Feb 14, 2012
Posts: 31
Hi Rob, thanks for the reply. I am kinda hitting a brick wall.
could you provide a simple example?
I have tried the foillowing but it does not seem to display on the jList.



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

In your first example you used jList1, now you're using jList2. Are you checking the right JList?
Andrew Geroge Alexander
Ranch Hand

Joined: Feb 14, 2012
Posts: 31
Am sorry.. Yes, I am checking the right fields. When i started the question i used jList1. Then i deleted it from my design, again added a new list which came up as jList2. So the variables are fine.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4640
    
    5

I have created a JList through IDE.

Yes, I am checking the right fields. When i started the question i used jList1. Then i deleted it from my design, again added a new list which came up as jList2.

Dump the visual designer. It's easier to learn to write Swing code yourself.

Visual designers, with their near-unreadable generated code, are not beginners' tools. The learning curve can be steep, and the effort is only worthwhile if you go make a career of developing GUIs.


luck, db
There are no new questions, but there may be new answers.
Andrew Geroge Alexander
Ranch Hand

Joined: Feb 14, 2012
Posts: 31
Hello Darryl, I hear what you are saying, but building here gives me a bit of advantage. Helps me know know my syntax errors, proper imports. Please could you help me out here??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
I think at this point, this discussion belong in a different forum: to Swing we go!
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

Paul Ruban Raj Eugin Francis wrote:

This code should work. Has the jList2 been added to the panel/frame? Also, make sure you have this code before you make the frame visible.

Better, post a SSCCE that demonstrates the problem.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Andrew Geroge Alexander
Ranch Hand

Joined: Feb 14, 2012
Posts: 31
Thank you. I got it resolved.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JList help.