Table Listeners

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2005
Posts: 20
Reputation: scoobie is an unknown quantity at this point 
Solved Threads: 0
scoobie scoobie is offline Offline
Newbie Poster

Table Listeners

 
0
  #1
Aug 11th, 2006
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:

  1. private void addFile(final String searchPattern, final File file, boolean subdirs, boolean first)
  2. {
  3. System.out.println("In add file");
  4.  
  5. if (file.isFile())
  6. {
  7. if (containsPattern(file.toString(), searchPattern))
  8. {
  9. System.out.println(file);
  10. // Need to add to model in event thread
  11. EventQueue.invokeLater(new Runnable()
  12. {
  13. public void run()
  14. {}
  15. });
  16.  
  17. Results(searchPattern, file);
  18. }
  19. } //end if
  20.  
  21. else if (file.isDirectory())
  22. {
  23. if (first || subdirs)
  24. { // need to let first pass through
  25. File files[] = file.listFiles();
  26. for (int i = 0; i < files.length; i++)
  27. {
  28. addFile(searchPattern, files[i], subdirs, false);
  29. } //end for loop
  30. }
  31. }
  32. }//end addFile
  33.  
  34.  
  35. public void Results(final String searchPattern, final File file)
  36. {
  37. final String files = file.toString();
  38. TableItem tableItem = new TableItem(rsTable, SWT.NONE);
  39. tableItem.setText(files);
  40.  
  41. rsTable.addMouseListener(new MouseListener()
  42. {
  43. public void mouseDoubleClick(MouseEvent arg0)
  44. {
  45. System.out.println("ShowHighlighted");
  46. // Don't block the event thread
  47. new Thread()
  48. {
  49. public void run()
  50. {
  51. final JDialog dialog = new JDialog();
  52. Container contentPane = dialog.getContentPane();
  53. try
  54. {
  55. FileReader reader = new FileReader(files);
  56. JTextPane textPane = new JTextPane();
  57. textPane.setEditable(false);
  58. JScrollPane scrollPane = new JScrollPane(textPane);
  59. contentPane.add(scrollPane);
  60. // Read in file
  61. textPane.read(reader, null);
  62. // Make highlight attribute
  63. final SimpleAttributeSet set = new SimpleAttributeSet();
  64. set.addAttribute(
  65. StyleConstants.CharacterConstants.Bold,
  66. Boolean.TRUE);
  67. // Get Document for text pane
  68. final DefaultStyledDocument document =
  69. (DefaultStyledDocument)textPane.getDocument();
  70. // Handle newlines across platforms
  71. document.putProperty(
  72. DefaultEditorKit.EndOfLineStringProperty, "\n");
  73. // highlight areas
  74. String text = textPane.getText();
  75. Pattern thePattern = Pattern.compile(searchPattern);
  76. final Matcher matcher = thePattern.matcher(text);
  77. EventQueue.invokeLater(new Runnable()
  78. {
  79. public void run()
  80. {
  81. while (matcher.find())
  82. {
  83. int start = matcher.start();
  84. int length = matcher.end() - start;
  85. document.setCharacterAttributes(start, length, set, false);
  86. }// end while
  87. // show results
  88. dialog.setSize(300, 300);
  89. dialog.show();
  90. }//end inside run
  91. } //end invokeQ
  92. );
  93. } //end try
  94.  
  95. catch (IOException ex)
  96. {
  97. System.err.println("Unable to show highlights" + ex); }//end catch
  98. }//end outside run
  99. } //end thread
  100. .start();
  101. }
  102. public void mouseDown(MouseEvent arg0)
  103. {
  104. }
  105.  
  106. public void mouseUp(MouseEvent arg0)
  107. {
  108. }
  109. });
  110.  
  111.  
  112. filePath.pack(); // column
  113.  
  114. rsTable.pack(); //table
  115.  
  116.  
  117. } //end results method
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: Table Listeners

 
0
  #2
Aug 11th, 2006
You might want to look into a SectionListener instead of a MouseListener.

Just a guess though. I've never worked with the SWT.
Last edited by hooknc; Aug 11th, 2006 at 12:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 20
Reputation: scoobie is an unknown quantity at this point 
Solved Threads: 0
scoobie scoobie is offline Offline
Newbie Poster

Re: Table Listeners

 
0
  #3
Sep 1st, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 802
Reputation: Phaelax is on a distinguished road 
Solved Threads: 40
Phaelax Phaelax is offline Offline
Practically a Posting Shark

Re: Table Listeners

 
0
  #4
Sep 5th, 2006
Can't you just add a MouseListener to the table then use .getSelectedRow() and retrieve the value of that row from your TableModel?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2990 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC