Solved

Open a File from a Table Column Button Click

Posted on 2013-01-08
4
190 Views
Last Modified: 2013-01-14
The Table Viewer is created and displayed in a Dialog Class.  The columns and button creation is in a TableViewer class.  

I have added a column in my  table that is populated with buttons
Table with Buttons in Column

I want the corresponding file open when the user clicks the button in the same row.

Column / Button Code

case 6:
                   Map<Object, Button> buttons = new HashMap<Object, Button>();   
                   TableItem item = (TableItem) cell.getItem();
                   Button button;
                  
                   if(buttons.containsKey(cell.getElement())) {
                      button = buttons.get(cell.getElement());
                   }
                   else
                   {
                     button = new Button((Composite) cell.getViewerRow().getControl(),SWT.PUSH);
                     //button.setText("Adobe");
                     button.setImage(loadImage("/images/AdobeIcon.png"));
                     
                     buttons.put(cell.getElement(), button);
                   }
                   TableEditor editor = new TableEditor(item.getParent());
                   editor.grabHorizontal  = true;
                   editor.grabVertical = true;
                   editor.setEditor(button , item, cell.getColumnIndex());
                   button.addListener(SWT.Selection, new SelectionListener(item, button, cell.getElement()));
                   editor.layout();
                  }

Open in new window



Selection Listener Class Code

class SelectionListener implements Listener {
      TableItem item;
      Button editButton;
      Object element;
     

      //////////////////////////////////////////////////////////////////////////
      //                         Constructor                                  //
      //////////////////////////////////////////////////////////////////////////
      public SelectionListener(TableItem item, Button editButton, Object element) {
          this.item = item;
          this.editButton = editButton;
          this.element = element;
       }

      //////////////////////////////////////////////////////////////////////////
      //                         handleEvent                                  //
      //////////////////////////////////////////////////////////////////////////
      @Override
      public void handleEvent(Event event) {
         AplotSaveDataModel.SaveData selected = (( AplotSaveDataModel.SaveData)element);
         int index = AplotSaveDataModel.getInstance().getIndexOf(selected);
         final File viewerFile = new File(AplotSaveDataModel.getInstance().getSelectedPDFFileName(index));
         try {
            Desktop.getDesktop().open(viewerFile);
         }
         catch (IOException e) {
           e.printStackTrace();
        }
      }

Open in new window



   
AplotSaveDataModel.SaveData selected = (( AplotSaveDataModel.SaveData)element);

Open in new window

This value does change when a differnt row is clicked

   
int index = AplotSaveDataModel.getInstance().getIndexOf(selected);

Open in new window

Allways Returns 0

This is the getIndexOf method in AplotSaveDataModel
 
     
public int getIndexOf(SaveData selected) {
       return data.indexOf(selected); //data is a arraylist
     }

Open in new window


I think that the above method may be the issue.  

What is happening - It does not matter which button is clicked.  The file in row 0 is always opened, because the index it always return 0

I am wondering if data.indexOf(selected) returns 0 is element is found and -1 if not.

I would be glad to change the code above, if there is a easier way to preform what is needed.  I just want when the button is clicked the file opens that is in the same row has the button.

Thanks in advance for any help you can give me.
0
Comment
Question by:jkteater
  • 3
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 38756098
I don't know which parts of the app are SWT, but the correct way to do what you want in Swing is to return a JButton as the CellEditor for the cell in question. You can build in an Action that will open the file
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38774606
:)
0
 

Author Comment

by:jkteater
ID: 38774806
CEHJ,  I always appreciate your time and suggestions.  You have been a huge help to me and I hope you will continue to be as I continue to learn. :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38774853
and I hope you will continue to be as I continue to learn. :)
Most likely ;)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 75
maven example error 3 52
Java DateChooser? 3 29
split string containing \r\n in Java 46 38
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now