Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Adding elements to a JList dynamically  RSS feed

 
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.
 
Author
Ranch Hand
Posts: 964
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!