Jface KeyListener未被激活

时间:2014-04-08 13:54:22

标签: java eclipse jface

我试图在Jface的搜索框中添加一个过滤器,没有任何运气...... KeyListener中的keyReleased方法永远不会被激活。我想我忘了什么,因为这种方法有警告"附加":

  

新的KeyAdapter(){}类型的方法keyReleased(KeyEvent)从不在本地使用

这是我的View类中的代码:

package de.vogella.jface.tableviewer;


import java.awt.event.KeyEvent;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.ui.part.ViewPart;

import de.vogella.jface.tableviewer.edit.AgeEditingSupport;
import de.vogella.jface.tableviewer.edit.FirstNameEditingSupport;
import de.vogella.jface.tableviewer.edit.GenderEditingSupport;
import de.vogella.jface.tableviewer.edit.LastNameEditingSupport;
import de.vogella.jface.tableviewer.edit.MarriedEditingSupport;
import de.vogella.jface.tableviewer.filter.PersonFilter;
import de.vogella.jface.tableviewer.model.ModelProvider;
import de.vogella.jface.tableviewer.model.Person;
import de.vogella.jface.tableviewer.sorter.MyViewerComparator;

public class View extends ViewPart {

  public static final String ID = "de.vogella.jface.tableviewer.view";
  private MyViewerComparator comparator;
  private PersonFilter filter;


  private TableViewer viewer;
  // static fields to hold the images
  private static final Image CHECKED = Activator.getImageDescriptor(
          "icons/checked.gif").createImage();
  private static final Image UNCHECKED = Activator.getImageDescriptor(
          "icons/unchecked.gif").createImage();

  public void createPartControl(Composite parent) {

    GridLayout layout = new GridLayout(2, false);
    parent.setLayout(layout);
    Label searchLabel = new Label(parent, SWT.NONE);
    searchLabel.setText("Search: ");
    final Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH);
    searchText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
        | GridData.HORIZONTAL_ALIGN_FILL));
    createViewer(parent);
    // Set the sorter for the table
    comparator = new MyViewerComparator();
    viewer.setComparator(comparator);

    // New to support the search
    searchText.addKeyListener(new KeyAdapter() {

      public void keyReleased(KeyEvent ke) {

        filter.setSearchText(searchText.getText());
        viewer.refresh();
      }
    });

    filter = new PersonFilter();
    viewer.addFilter(filter);
  }

  private void createViewer(Composite parent) {

    viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
        | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
    createColumns(parent, viewer);
    final Table table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    viewer.setContentProvider(new ArrayContentProvider());
    // get the content for the viewer, setInput will call getElements in the
    // contentProvider
    viewer.setInput(ModelProvider.INSTANCE.getPersons());
    // make the selection available to other views
    getSite().setSelectionProvider(viewer);
    // set the sorter for the table

    // define layout for the viewer
    GridData gridData = new GridData();
    gridData.verticalAlignment = GridData.BEGINNING;
    gridData.horizontalSpan = 2;
    gridData.grabExcessHorizontalSpace = false;
    gridData.grabExcessVerticalSpace = false;
    gridData.horizontalAlignment = GridData.BEGINNING;
    viewer.getControl().setLayoutData(gridData);
  }

  public TableViewer getViewer() {

    return viewer;
  }

  // create the columns for the table
  private void createColumns(final Composite parent, final TableViewer viewer) {

    String[] titles = { "First name", "Last name", "Gender", "Married", "Age" };
    int[] bounds = { 112, 112, 112, 112, 112 };

    // first column is for the first name
    TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
    col.setLabelProvider(new ColumnLabelProvider() {

      @Override
      public String getText(Object element) {

        Person p = (Person) element;

        return p.getFirstName();
      }
    });

    col.setEditingSupport(new FirstNameEditingSupport(viewer)); 


    // second column is for the last name
    col = createTableViewerColumn(titles[1], bounds[1], 1);
    col.setLabelProvider(new ColumnLabelProvider() {

      @Override
      public String getText(Object element) {

        Person p = (Person) element;

        return p.getLastName();
      }
    });

    col.setEditingSupport(new LastNameEditingSupport(viewer));


    // now the gender
    col = createTableViewerColumn(titles[2], bounds[2], 2);
    col.setLabelProvider(new ColumnLabelProvider() {

      @Override
      public String getText(Object element) {

        Person p = (Person) element;

        return p.getGender();
      }
    });

    col.setEditingSupport(new GenderEditingSupport(viewer));


    // now the status married
    col = createTableViewerColumn(titles[3], bounds[3], 3);
    col.setLabelProvider(new CenterImageLabelProvider() {

      @Override
      public Image getImage(Object element) {

        if (((Person) element).isMarried()) {

          return CHECKED;
        }
        else {

          return UNCHECKED;
        }
      }
    });

    col.setEditingSupport(new MarriedEditingSupport(viewer));


    // now the age
    col = createTableViewerColumn(titles[4], bounds[4], 4);
    col.setLabelProvider(new ColumnLabelProvider() {

      @Override
      public String getText(Object element) {

          Person p = (Person) element;

          return p.getAge().toString();
      }
    });

    col.setEditingSupport(new AgeEditingSupport(viewer));

  }

  private TableViewerColumn createTableViewerColumn(String title, 
          int bound, final int colNumber) {

    final TableViewerColumn viewerColumn = new TableViewerColumn(viewer,
        SWT.CENTER);
    final TableColumn column = viewerColumn.getColumn();
    column.setText(title);
    column.setWidth(bound);
    column.setResizable(true);
    column.setMoveable(true);
    column.addSelectionListener(getSelectionAdapter(column, colNumber));

    return viewerColumn;
  }

private SelectionAdapter getSelectionAdapter(final TableColumn column,
          final int index) {

                SelectionAdapter selectionAdapter = new SelectionAdapter() {

                  @Override
                  public void widgetSelected(SelectionEvent e) {

                    comparator.setColumn(index);
                    int dir = comparator.getDirection();
                    viewer.getTable().setSortDirection(dir);
                    viewer.getTable().setSortColumn(column);
                    viewer.refresh();
                  }
                };

    return selectionAdapter;
  }

  public void setFocus() {

    viewer.getControl().setFocus();
  }
}

1 个答案:

答案 0 :(得分:1)

导入错误KeyEvent - 您需要org.eclipse.swt.events.KeyEvent java.awt.event.KeyEvent