Jump to content
  • Advertisement
Sign in to follow this  
bargasteh

[java] Newbie question about addElement

This topic is 4578 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I am a C++ programmer who started to program Java recently. I am using Netbean and it really looks cool to me. I placed a textarea, a button and Jlist. Now I want to do something really simple. I want to add the text in text area to Jlist. I did this but does work: String a1; a1 = jTextField1.getText(); jList1.addElement(a1); for some reason compiler doesnt know addElement. Error is :
init:
deps-jar:
Compiling 1 source file to /home/raha/JavaApplication1/build/classes
/home/raha/JavaApplication1/src/javaapplication1/NewJFrame.java:202: cannot find symbol
symbol  : method addElement(java.lang.String)
location: class javax.swing.JList
jList1.addElement(a1);
1 error
BUILD FAILED (total time: 0 seconds)

by the way I am using linux :) any idea why this is happening ? Thanks

Share this post


Link to post
Share on other sites
Advertisement
The function .add() is how java does things. For more info on what all the other java objects' member functions are, see here:

http://java.sun.com/j2se/1.5.0/docs/api/

Share this post


Link to post
Share on other sites
you should also install the Sun JDK instead of relying on gcj.
gcj is extremely old and incomplete.

JList btw doesn't have any method to add data at runtime directly.
You either initialise it with an array of Vector for a static list or a ListModel for dynamic data (in which case you will need to derive a ListModel type yourself or use one of the predefined ones like DefaultListModel in order to add and remove data at runtime).

The latter is the preferred way of working in non-trivial applications, as the ListModel can also be used to do custom rendering of output and helps separate the user interface from the data.

See the API documentation for full details on these classes.

Share this post


Link to post
Share on other sites

DefaultListModel model = new DefaultListModel();
JList myList = new JList(model);

model.addElement( "Something 1" );
model.addElement( "Something 2" );

Share this post


Link to post
Share on other sites
No no.
What I mean that how can I add a text for example from a text field while the program is running.

Is it possible to do this using Vector ? if yes how is it possible ?

Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by bargasteh
What I mean that how can I add a text for example from a text field while the program is running.
You mean like when you hit a button or somethin'? Lookup what's called "action listeners" and you should be able to do what you want to do from there.

Share this post


Link to post
Share on other sites
I am using NetBean, NetBean generat the GUI code, so for example when I put a button then I can goto action property and then choose for example clickbutton (or mouse over, etc).
My problem is not handling the event. My problem is that this doesnt work:

jList1.addElement(a1);

but this works:

jTextPane1.setText(a1);

or even

jTextField2.setText(a1);


once I click the buttom the string a1 set itself in jTextPane1 or Jtextfield2 without any problem. but for some reason that I dont know NetBean Doesnt accept addElement or even add for Jlist.

Why ? :(

Share this post


Link to post
Share on other sites
Quote:
Original post by bargasteh
My problem is that this doesnt work: jList1.addElement(a1);

Yeah. Look at Son of Cain's post. addElement is a member function of DefaultListModel, not JList. So, you want to add your text to the model of the JList. The reason for this is because the items displayed in a JList depend upon the information in its model, so if you add info to the model, you're adding info to the JList. Just be sure to set the model accordingly as also shown in Son of Cain's post.
Quote:
Original post by bargasteh
NetBean Doesnt accept addElement or even add for Jlist.

You can't use .add() with a JList. And as said, addElement() is not a member of JList at all. Did you look at that api reference I posted earlier? It tells you all the member functions of each of Java's objects.

Share this post


Link to post
Share on other sites
Thank you for your guide.

I tried this but didnt work:


private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String a1;
Vector list = new Vector ();
a1 = jTextField1.getText();

DefaultListModel jList1 = new DefaultListModel();
JList myList = new JList(jList1);

jList1.addElement( a1 );
jTextField2.setText(a1);
jTextPane1.setText(a1);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!