Results 1 to 1 of 1
  1. #1
    Avey is offline Member
    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Default How can I get ListSelectionEvent to only return once?

    Each time a ListSelectionEvent occurs, it counts both the down click of the mouse button and it's release. So I get output that looks like this...

    Does anyone have any suggestions as to how to handle which item on my list I press and returning something I can use to handle different cases according to which item on the list is pressed?

    Java Code:
    TVS
    3
    TVS
    3
    Pharmacy
    2
    Pharmacy
    2
    I am trying to create a psuedo-grocery cart app for a class but can't get the event handling right. Here's the main class/frame/handler

    Java Code:
    package Apps;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusListener;
    
    import javax.swing.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    
    public class CartScreen extends JFrame implements ActionListener, ListSelectionListener{  // I am a customized screen
    	
    	private static final int FRAME_WIDTH	= 300;
    	private static final int FRAME_HEIGHT	= 500;
    	private static final int FRAME_X_ORIGIN	= 150;
    	private static final int FRAME_Y_ORIGIN	= 250;
    
    	private static final int BUTTON_WIDTH	= 80;
    	private static final int BUTTON_HEIGHT	= 30;
    	
    	public static JList list;
    	public static JPanel listPanel;
    	public static JButton purchaseButton;
    	public static JButton cartButton;
    	
    	JButton okButton;
    	String[] listOptions = {"Fruit","Shoe", "Pharmacy", "TVS"};
    	
    	public CartScreen()
    	{
    		setSize(FRAME_WIDTH,FRAME_HEIGHT); 	 // window size
    		setTitle(" Shopping Cart ");
    		setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		Container content = getContentPane();   // get my canvas
    		
    		list = new JList(listOptions);
    		listPanel = new JPanel(new BorderLayout());
    		listPanel.add(new JScrollPane(list));
    		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		list.addListSelectionListener(this);
    		
    		content.add(listPanel, BorderLayout.CENTER);
    		content.setBackground(Color.LIGHT_GRAY); //set background whatevs
    		content.setLayout(new FlowLayout());
    		
    		setVisible(true);
    		
    	}
    	
    	public void actionPerformed(ActionEvent event) {
    		
    		System.out.println("action event");
    	}
    	
    	public void valueChanged(ListSelectionEvent e) {	
    		
    		System.out.println(list.getSelectedValue());
    		System.out.println(list.getSelectedIndex());
    		
    	}
    
    }
    Thanks!
    Last edited by Avey; 04-05-2013 at 05:45 AM.

Similar Threads

  1. return
    By Sanna.1 in forum New To Java
    Replies: 1
    Last Post: 02-17-2012, 01:38 PM
  2. Using int/int, 7/5 would return 1
    By zoe in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 11:25 AM
  3. about 'return'.
    By helloworld in forum New To Java
    Replies: 9
    Last Post: 11-28-2008, 04:08 AM
  4. if..else..return
    By mqdias in forum New To Java
    Replies: 1
    Last Post: 08-10-2007, 04:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •