final String cardListStrings[] = {"First Card", "Next Card", "Previous Card", "Last Card"};
		cardList = new JList(cardListStrings);
		cardList.setVisibleRowCount(4);
		cardList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		cardList.addListSelectionListener(
				new ListSelectionListener()
				{
					public void valueChanged(ListSelectionEvent e)
					{
						System.out.println("Inside List Event Handler");
						if (cardList.getSelectedIndex()== 0)
						{
							cardManager.first(deck);
							System.out.println("First Card");
						}
						if (cardList.getSelectedIndex()== 1)
						{
							cardManager.next(deck);
							System.out.println("Next Card");
						}
						if (cardList.getSelectedIndex() == 2)
						{	
							cardManager.previous(deck);
							System.out.println("Previous Card");
						}
						if (cardList.getSelectedIndex()== 3)
						{
							cardManager.last(deck);
							System.out.println("Last Card");
						}
					}
				}
			);

This piece of code fires twice for every selection in the JList Menu. How do I get it to fire once for each mouse click.

Thanks

Recommended Answers

All 5 Replies

you need to find a more appropriate listener (i.e. NOT ListSelectionListener).

edit: you could also try look at ListSelectionEvent, and filter out the event you don't want.

Yes, you can try this in order to obtain only 1 print:

private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {
        // TODO add your handling code here:
        if (!evt.getValueIsAdjusting())
            System.out.println("Clicked on "+ jList1.getSelectedValue());
    }

you could also use a siple count, that will start off at zero and when the event if fired it will add one to the counter, the next time the event is fired it will check the counter and act appropriately i.e(psuedo):

eventfired(Event e) {
if (counter==1) {
System.out.println(e);//would print the event
counter=0;//reset counter for next time
} else {//this is when the event is fired the first time i.e user clicked the list and is selecting
counter++;
}
}
Member Avatar for hfx642

3 years old, people!

3 years old, people!

i guess this is why they ask so nicely MARK THREAD AS SOLVED.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.