I'm trying to populate a JList
from an Arraylist<Member>
from inside an ActionListener
method.
public class guiDemo extends JFrame implements ListSelectionListener {
...
private JList list = new JList();
private JScrollPane sp = new JScrollPane();
...
ActionListener list10 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
win5.setVisible(true);
p1.setVisible(false);
String familyName = null;
int id = 0;
try {
familyName = (txt4.getText());
id = Integer.parseInt(txt3.getText());
} catch (NumberFormatException ne) {
System.out.println("error");
id = 0;
}
try{
ArrayList<Member> result = getMembersForList(id, familyName);
list = new JList(result.toArray());
sp = new JScrollPane(list);
} catch (SQLException se) {
txt.setText("SQLException!");
}
catch ( ClassNotFoundException ce) {
txt.setText("ClassNotFoundException!");
}
}
};
result is the ArrayList<Member>
that I'm trying to populate the JList
with, as to create an interface where a user can choose between the objects in the ArrayList
.
Furthermore:
...
b14.addActionListener(list10);
...
win5.setPreferredSize(new Dimension(400, 400));
win5.setLayout(new GridLayout(10,1));
win5.setMinimumSize(new Dimension(400, 400));
win5.add(b13);win5.add(l9);win5.add(txt3);win5.add(l10);
win5.add(txt4);win5.add(b14);win5.add(l11);
win5.add(sp);
list.setVisibleRowCount(15);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(this);
...
As you can see the JList
is to be located below two textfields(txtX)
, labels(lX)
and buttons (bX)
.
public void valueChanged(ListSelectionEvent e){
}
I believe the problem stems from the fact that I declare the JList
and JScrollPane
outside the actionlistener
and then cannot populate the JList
. The valueChanged
method is not completely done (I'd have to solve this problem first) but I don't believe this problem stems from the fact that it is empty.
Populating the JList
from outside the ActionListener
will not work since the ArrayList
has yet to be filled. The idea is that you search for either an id or a familyName or both and get a number of choices based on matches from an SQLite db. The contents of the result ArrayList
are objects based on the matches from the DB.
I believe this is probably some real rookie mistake, but I would appreciate any help I can get.
The message I get:
First, the -Xlint
flag is providing warnings about a lack of generics.
This means that the compiler can not guarantee that the data the JList
is expecting is actually the data you are providing. That is, the way you have it set up, JList
is expecting elements of type Object, but you are providing Memeber
.
This is not an issue, so long as you've ensured that your renderers can actually handle this. It's just the compiler telling you, that it has no way to be sure that this is correct behaviour or not...This might cause issues at run time.
It is preferred where possible, tp provide support for generics, but it's not always possible.
private JList<Member> list ...;
//...
ArrayList<Member> result = getMembersForList(id, familyName);
list = new JList(result.toArray(new Member[result.size()]));
This will allow you to trap possible errors at compile time rather than hitting an error at run time...
Second, let me make some guesses...
You create an instance of JList
and JScrollPane
...
private JList list = new JList();
private JScrollPane sp = new JScrollPane();
You then add these to the UI...
sp.setViewportView(list);
add(sp); // for example...
Then in your actionPerformed
method, you create new instances of these objects...
list = new JList(result.toArray());
sp = new JScrollPane(list);
But never add them to the UI and some how magically expect that they will know how to link to the previous instances and update the UI...?
That's not how it works. The instances you have created in the actionPerformed
method have no relationship to the objects you created previously. Instead, you should be changing the JList
's model, for example...
ActionListener list10 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
//...
try{
//ArrayList<Member> result = getMembersForList(id, familyName);
//list = new JList(result.toArray());
//sp = new JScrollPane(list);
DefaultListModel model = new DefaultListModel();
for (Member member : getMembersForList(id, familyName)) {
model.addElement(member);
}
list.setModel(model);
//...
}
};
I think this is just because JList is a generic type and you try to use it as raw type.
http://docs.oracle.com/javase/7/docs/api/javax/swing/JList.html
Note that these are just warnings, not errors.
If you say JList<Member>
, these warnings will probably go away.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.