Results 1 to 8 of 8
Thread: Adding to JList with Netbeans
- 06-03-2008, 02:22 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 34
- Rep Power
- 0
Adding to JList with Netbeans
Hello,
I am having a really difficult time trying to add a jTextField name and last name to the jList with netbeans, I know that for exit, I can create an action by right mouse clicking and creating an event. This is simple enough, however I have an add button that I am using and I have no idea how to code the add button with netbeans. Can someone please explain to me in detail how to do this.
I looked for tutorials on using netbeans to use the add button to add to a jlist and I found nothing. if someone could give me the instructions on how to do this, or show me the page for netbeans that shows the steps in netbeans to do this I would really appreciate it. Thank you.
- 06-03-2008, 03:35 AM #2
Not clear,
You want to add the contents of the textField to JList?
NetBeans doesn't support that feature(The programmer should do it)... it only initializes the contents when the ClassLoader loads that class.I looked for tutorials on using netbeans to use the add button to add to a jlist and I found nothing.
Not dynamic...
try to use DefaultListModel, have experiments on it....
THere is a JList constructor that has a parameter ListModel...
You can pass that DefaultListModel to that parameter....
You may play with that.....freedom exists in the world of ideas
- 06-03-2008, 06:09 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 34
- Rep Power
- 0
Adding to JList with Netbeans
Ok, I don't really understand where this defaultlistmodel goes, and
THere is a JList constructor that has a parameter ListModel...
I read about this but I keep forgetting what constructors are and a parameter?
do I do something like this
public class defaultlist {
public void defaultListModel() {
listmodel.setjlist1.(jTextField1)
listmodel.setjlist1.(jTextField2)
}
}
and then in the add button actionperformed event
do this
new defaultListModel();
- 06-03-2008, 08:41 AM #4
No....
Something like,
and observe what happens to jlist...Java Code:class MyMainClass{ DefaultListModel dlm = new DefaultListModel(); JList jlist = new JList( dlm ); //invoke this when add button fired private static final void addList(){ dlm.addElement((Object)jTextField1); // add name dlm.addElement((Object)jTextField2); // add lastname } }Last edited by sukatoa; 06-03-2008 at 08:49 AM.
freedom exists in the world of ideas
- 06-03-2008, 08:01 PM #5
Member
- Join Date
- Apr 2008
- Posts
- 34
- Rep Power
- 0
Ok I put this code in the jbutton section and I kept receiving an error that said "Illegal start of expression" I don't know enough to see anything wrong with this. Netbeans didn't say anything else.
and this code I put at the very top of the code, their is more like the rest of the GUI, but I didn't paste it because its too long.Java Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { //invoke this when add button fired private static final void addList(){ dlm.addElement((Object)jTextField1); // add name dlm.addElement((Object)jTextField2); // add lastname } }
Java Code:package editor; import javax.swing.JList; import javax.swing.*; /** * * @author Philip */ class MyMainClass{ DefaultListModel dlm = new DefaultListModel(); JList jlist = new JList( dlm ); } public class NewJFrame extends javax.swing.JFrame { /** Creates new form NewJFrame */ public NewJFrame() { initComponents(); }
- 06-03-2008, 08:10 PM #6
How then could i do, such a wicked thing, and sin against the netbeans... :(
after the button clicked, the methodshould be invoked....jButton1ActionPerformed
And? i didn't say put that sample method to that actionPerformed...
put that addList beside those existing methods in your main classJava Code:eg. private void jButton1ActionPerformed(netbeans style) { addList(); }
What i mean to MyMainClass is the class you've coded where the main method exists....
I assumed you textFields are also in that main class...Last edited by sukatoa; 06-03-2008 at 08:13 PM.
freedom exists in the world of ideas
- 06-03-2008, 08:21 PM #7
Member
- Join Date
- Apr 2008
- Posts
- 34
- Rep Power
- 0
Thank you for your help. Let me show you what I did.
I declared private DefaultListModel lm = newDefaultListModel(); with the rest of my declared buttons and textfields etc...
Then I went to the jlist properties section and clicked on the model section and typed in lm after selecting custom code. and then just added the elements to the add button after creating an event actionperformed by right clicking on the add button, selecting events, then actionperformed and typing in the following code.
lm.addElement(jTextField1.getText()); etc... for the rest of the textfields
I tryed doing this yestereday but I was trying to select component and then I was trying to bind. I go it now though, so if any other newbies like myself need help with something similar hopefully they will find my post, since I was looking for hours for a post like this.
Thank you again for your effort.
- 03-28-2012, 07:39 PM #8
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
JList and JPanels
By JetsYanks in forum New To JavaReplies: 8Last Post: 12-25-2009, 02:11 PM -
searching within a JList
By newtojava7 in forum New To JavaReplies: 1Last Post: 03-10-2008, 12:12 AM -
Adding Business Methods to an EJB in NetBeans
By JavaForums in forum NetBeansReplies: 0Last Post: 07-30-2007, 11:13 PM -
Help with JList
By Albert in forum NetBeansReplies: 1Last Post: 07-13-2007, 03:42 PM -
add a jlist column
By Alan in forum JCreatorReplies: 1Last Post: 05-28-2007, 04:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks