need help understanding how to add to jlist
Hello all, I'm new to this forum and fairly new to java as well so dumb it down for me if you can. I'm having trouble figuring this out. I have 3 jlists and each one has a textfield and a jbutton so the user can enter text to be added as a new list item. I have read probably a dozen webpages or forum posts on this and all have pretty much the same code advice for doing this. The problem I'm having is that none have said or shown how the code actually links the button to the jlist1, jlist2, and jlist3 respectively. What am I missing?? Here is the code I have and not sure what to change or add.
Code :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String addName = (jTextField1.getText());
DefaultListModel names = new DefaultListModel();
names.addElement(addName);
JList nameList = new JList( names );
jTextField1.setText("");
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String addPlace = (jTextField2.getText());
DefaultListModel places = new DefaultListModel();
places.addElement(addPlace);
JList placeList = new JList( places );
jTextField2.setText("");
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String addDay = (jTextField3.getText());
DefaultListModel days = new DefaultListModel();
days.addElement(addDay);
JList dayList = new JList( days );
jTextField3.setText("");
}
Thanks
Re: need help understanding how to add to jlist
Quote:
how the code actually links the button to the jlist1,
Not sure what you mean by "links".
You add an action listener to a button. When the user presses the button, the code in the action listener method is called where you do what you want to do to respond to the button that was pressed.
If you want to change the contents of a component like a JList, you need a reference to the JList object that you can use to call one of its methods.
NOTE: I see you are defining variables inside of the mehtods. When the method exits, those definitions will go away. You need to define the variables outside of the methods at the class level where they will stay around for the life of the class object.