#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    MouseListener doesn't work


    Hello!
    I'm trying to make things happen when the mouse is clicked or released. My program is based on a textfield with an autocomplete feature that will show in a JWindow. I don't get any kind of response from the MouseListener. I also have a documentlistener but that works fine.
    Here is my code:

    Code:
    public class InteractiveSearcher extends JFrame implements MouseListener,SearchClient {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	JTextField search_string;
    	SearchProvider search_provider = FemaleNamesSearchProvider.getInstance();
    	long last_search;
    	private JFrame frame;
    	blablabla search = new blablabla();
    	
    	
    	public InteractiveSearcher(){
    		frame = new JFrame("Search test");
    		search_provider.addObserver(this);
    		search_string = new JTextField();
    		JButton start_search = new JButton("Run search");
    		//MouseListener mListener = null;
    		//frame.addMouseListener(mListener);
    		//frame.addMouseListener(this);
    		
    		DocumentListener documentListener = new DocumentListener() {
    		      public void changedUpdate(DocumentEvent documentEvent) {
    		        searchIt(documentEvent);
    		      }
    		      public void insertUpdate(DocumentEvent documentEvent) {
    		        searchIt(documentEvent);
    		      }
    		      public void removeUpdate(DocumentEvent documentEvent) {
    		        searchIt(documentEvent);
    		      }
    		      private void searchIt(DocumentEvent documentEvent) {
    		        DocumentEvent.EventType type = documentEvent.getType();
    		        if (type.equals(DocumentEvent.EventType.CHANGE)) {
    		          do_search();
    		          Point location = search_string.getLocationOnScreen();
    		          int y = 25;
    		          //search.setBounds(location.x,y + location.y,150,250);
    		        }  else if (type.equals(DocumentEvent.EventType.INSERT)) {
    		          do_search();
    		          Point location = search_string.getLocationOnScreen();
    		          int y = 25;
    		          //search.setBounds(location.x,y + location.y,150,250);
    		        }  else if (type.equals(DocumentEvent.EventType.REMOVE)) {
    		          do_search();
    		          Point location = search_string.getLocationOnScreen();
    		          int y = 25;
    		          //search.setBounds(location.x,y + location.y,150,250);
    		        }
    		        System.out.print("hej");
    		        //Document source = documentEvent.getDocument();
    		        /*int length = source.getLength();
    		        System.out.println("Length: " + length);*/
    		      }
    		    };
    		    search_string.getDocument().addDocumentListener(documentListener);
    		  
    		//search_window.pack();
    		search.setBackground(new Color(0,0,0,255));
    		frame.getContentPane().setLayout(new GridLayout(1,2));
    		frame.getContentPane().add(search_string);
    		frame.getContentPane().add(start_search);
    		frame.pack();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		search.add(search.c);
    		search.toFront();
    		search.setAlwaysOnTop(true);
    		search.setVisible(true);
    		search.requestFocusInWindow();
    	}
    	
    	protected void do_search() {
    		//Call the search provider make sure that this call is not interrupted 
    		synchronized (search_provider) {
    			last_search = search_provider.newAsyncSearch(search_string.getText(), 50);
    		}	
    	}
    
    	@Override
    	public void searchResultUpdate(long search_id, final List<String> result) {
    		// This method is called when the search function (newAsyncSearch) is done
    		// make sure that this block of code is not interrupted by another search result 
    		synchronized (search_provider) {
    			// We only want to show the last search 
    			if (search_id == last_search){
    				SwingUtilities.invokeLater(new Runnable() {					
    					@Override
    					public void run() {
    						//JOptionPane.showMessageDialog(frame, result);
    						System.out.println(result);
    						search.updateResults(result, 1);
    					
    					}
    				});
    			}
    			
    		}
    	}
    	
    	
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    		System.out.println("IT WORKS");
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		System.out.println("IT WORKS");
    	}
    
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    		System.out.println("IT WORKS");
    	}
    
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    		System.out.println("IT WORKS");
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    		System.out.println("IT WORKS");
    	}
    	
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    			SwingUtilities.invokeLater(new Runnable() {
    				
    				@Override
    				public void run() {
    					new InteractiveSearcher();
    					
    				}
    			});
    	}
    	
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Please make a small complete program the compiles, executes and shows the problem.
    The posted code does not compile because of missing imports and missing class definitions.

IMN logo majestic logo threadwatch logo seochat tools logo