Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JList help.

 
Andrew Geroge Alexander
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andrew Geroge Alexander
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How will I be able to add the values to the list dynamically??
Any help will be greatly appreciated.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Geroge Alexander
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your first example you used jList1, now you're using jList2. Are you checking the right JList?
 
Andrew Geroge Alexander
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Geroge Alexander
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think at this point, this discussion belong in a different forum: to Swing we go!
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Geroge Alexander
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I got it resolved.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic