• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding elements to a JList dynamically  RSS feed

 
Giri Thava
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am adding elements to a JList dynamically. When I click a button,
the element is nicely adding to the end of the JList. The JList elememts
should adhere to MouseEvent or ListSelectionEvent (I chose MouseEvent).
When I launch the application for the first time the JList adheres to
MouseEvents, but after adding elements dynamically, the JList doesn't
adheres to MouseEvents.

The code that adds elements dynamically to the JList
bsave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
defaultlistModel.addElement(date);
}
});

The code that adheres to MouseEvent
myJlist.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
if(me.getClickCount() == 1) {
String s = (String)myJlist.getSelectedValue();
ta.setText(" ");
String date = (String)myJlist.getSelectedValue();
}
}
});

How can I solve this problem?
Advance thanks.
 
Brian Cole
Author
Ranch Hand
Posts: 959
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Giritharan Thavalingam:
When I launch the application for the first time the JList adheres to
MouseEvents, but after adding elements dynamically, the JList doesn't
adheres to MouseEvents.


Your code looks ok to me, and I can't see how adding elements
to the list model could be affecting the mouse listener stuff.

It looks like all your mouse listener does is clear a text
component. Are you sure that's not happening when the user
clicks on the JList (even after elements have been added)?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!