944,159 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4881
  • Java RSS
Aug 11th, 2006
0

Table Listeners

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scoobie is offline Offline
20 posts
since Jan 2005
Aug 11th, 2006
0

Re: Table Listeners

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.
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005
Sep 1st, 2006
0

Re: Table Listeners

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scoobie is offline Offline
20 posts
since Jan 2005
Sep 5th, 2006
0

Re: Table Listeners

Can't you just add a MouseListener to the table then use .getSelectedRow() and retrieve the value of that row from your TableModel?
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how to move an jlabel icon from one jlabel to another jlabel
Next Thread in Java Forum Timeline: Hello world for mobile applications





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC