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)
[java] Newbie question about addElement
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 :
by the way I am using linux :)
any idea why this is happening ?
Thanks
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/
http://java.sun.com/j2se/1.5.0/docs/api/
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.
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.
Thank you.
I want to add data in runtime.
Instead of Jlist what component would you recommend? Thanks
I want to add data in runtime.
Instead of Jlist what component would you recommend? Thanks
DefaultListModel model = new DefaultListModel();JList myList = new JList(model);model.addElement( "Something 1" );model.addElement( "Something 2" );
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
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
Quote:Original post by bargastehYou 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.
What I mean that how can I add a text for example from a text field while the program is running.
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 ? :(
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 ? :(
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.
Thank you for your guide.
I tried this but didnt work:
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); }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement