Hi,

i was wondering if you could help me. i am having a problem with a listener in SWT. i have a program where the user enters some text in a textfield and then clicks on a browse button where they select the directory they want the program to search for this string. the user then clicks a submit button. what happens here is that it will only return the files that contain that string and i have them outputting to an SWT table. the problem i am having is that i would like the user to be able to click on one of the files in the table and get it to open. however when i do this it opens all files in the table instead of just the one i picked. i think its because i have a mouse listener on the table instead of on the TableItem. however when i went to do it on the table item i could't figure out what the parameters where meant to be.

Can you please please help me. i am really stuck and have spent two days trying to get it to work with no success. it is part of my final year project.

thanks in advanced

scoobie.

here is the code i am using:

private void addFile(final String searchPattern, final File file, boolean subdirs, boolean first) 
{
   System.out.println("In add file"); 
    
   if (file.isFile()) 
   {
	   if (containsPattern(file.toString(), searchPattern)) 
	    {
	  	   System.out.println(file);
	          // Need to add to model in event thread
	          EventQueue.invokeLater(new Runnable() 
	     		    {
		        	public void run() 
		        	{}
		          });
		          
		        Results(searchPattern, file);
		       }
	     } //end if
				     
		else if (file.isDirectory()) 
		     {   		    	
		    		 if (first || subdirs) 
			    	 	{ // need to let first pass through
					     File files[] = file.listFiles();
					     for (int i = 0; i < files.length; i++) 
					     {
					        addFile(searchPattern, files[i], subdirs, false);
					     } //end for loop
			    	      }	    	
		    }
	}//end addFile
	
		
	public void Results(final String searchPattern, final File file)
	{
		final String files = file.toString();
		TableItem tableItem = new TableItem(rsTable, SWT.NONE);
		tableItem.setText(files);
	       
	    rsTable.addMouseListener(new MouseListener()
		{
	    	public void mouseDoubleClick(MouseEvent arg0)
			{
	    		System.out.println("ShowHighlighted");
	    		// Don't block the event thread
				new Thread() 
				{
					public void run()
					{
						final JDialog dialog = new JDialog();
						Container contentPane = dialog.getContentPane();
					try 
					{
					FileReader reader = new FileReader(files);
					JTextPane textPane = new JTextPane();
					textPane.setEditable(false);
					JScrollPane scrollPane = new JScrollPane(textPane);
					contentPane.add(scrollPane);
					 // Read in file
					textPane.read(reader, null);
					// Make highlight attribute
					final SimpleAttributeSet set = new SimpleAttributeSet();
					set.addAttribute(
						          StyleConstants.CharacterConstants.Bold,
						          Boolean.TRUE);
					 // Get Document for text pane
				      final DefaultStyledDocument document =
						          (DefaultStyledDocument)textPane.getDocument();
				 // Handle newlines across platforms
						        document.putProperty(
						          DefaultEditorKit.EndOfLineStringProperty, "\n");
				 // highlight areas
				String text = textPane.getText();
				Pattern thePattern = Pattern.compile(searchPattern);
				final Matcher matcher = thePattern.matcher(text);
			        EventQueue.invokeLater(new Runnable() 
					{
					          public void run() 
					         {
					          while (matcher.find()) 
					          {
					            int start = matcher.start();
					            int length = matcher.end() - start;
					document.setCharacterAttributes(start, length, set, false);
					         }// end while
						 // show results
						 dialog.setSize(300, 300);
						 dialog.show();
				 }//end inside run
		        } //end invokeQ
	        );
	      } //end try
						      
			 catch (IOException ex) 
				 {
				     System.err.println("Unable to show highlights" + ex);			      }//end catch
			 }//end outside run
		  } //end thread
	  .start();						
}
public void mouseDown(MouseEvent arg0) 
{
}

public void mouseUp(MouseEvent arg0) 
{				
}
});
		

 filePath.pack(); // column
	    
 rsTable.pack();	//table   
 
	 	
} //end results method

Recommended Answers

All 3 Replies

You might want to look into a SectionListener instead of a MouseListener.

Just a guess though. I've never worked with the SWT.

Hi,

I tried that, i can't seem to get it to work.

I am trying to get a mouse listener so that when i double click on the file in the table it will open the file.

thanks,

scoobie

Can't you just add a MouseListener to the table then use .getSelectedRow() and retrieve the value of that row from your TableModel?

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.