سؤال

ما أود القيام به هو أن تكون قادرا على علامة التبويب بين العناصر في الجدول.

أنا حاليا أخلق طاولتي مثل هذا.

this.tableViewer = 
            new TableViewer(parent , SWT.FULL_SELECTION);

   tableViewer.setUseHashlookup(true);
        table = tableViewer.getTable();

        GridData gridData = new GridData(GridData.FILL_BOTH);
        gridData.grabExcessVerticalSpace = true;
        table.setLayoutData(gridData);

        table.setLinesVisible(true);
        table.setHeaderVisible(true);

        ...

   /** Create the Cell Editor Array - will hold all columns **/
        editors = new CellEditor[table.getColumnCount()];
        /** Cell Editor Row 1 **/


  /** Set the column properties **/
        tableViewer.setColumnProperties(columnNames);

        /** Assign the cell editors to the viewer **/
        tableViewer.setCellEditors(editors);

        /** Set the cell modifier for the viewer **/
        tableViewer.setCellModifier(new MyCellModifier(this));
        //Create the Table Viewer

        /** Table Viewer Content and Label Provider **/
        tableViewer.setContentProvider(new MyContentProvider(this));
        tableViewer.setLabelProvider(new MyLabelProvider());

لكنني لست متأكدا من كيفية إعداد الجدولة. يعمل كل شيء آخر بقدر تحرير الأعمدة، وإظهار البيانات، إلخ. عالق فقط في هذا الجزء الأخير.

إذا فاتني وثائق واضحة أو Javadocs - اعتذاري وحتى الإشارة إلى تلك ستكون رائعة.

هل كانت مفيدة؟

المحلول

أعتقد أن علامة التبويب الافتراضية لا تقفز من الخلية إلى خلية في جدول SWT. بدلا من ذلك يعبر إلى السيطرة التالية. لذلك ستحتاج أيضا إلى إخبارها بعدم اجتيازها عند الضغط على علامة التبويب

KeyListener keyListener = new KeyLisener()
{
    public void keyPressed(KeyEvent evt)
    {
        if (evt.keyCode == SWT.TAB)
        {
            // There are numerous setSelection methods.  I'll leave this to you. 
            tableViewer.getTable().setSelection(...)
        }
    }

    public void keyReleased(KeyEvent evt){}
}

TraverseListener traverseListener = new TraverseListener()
{
    public void keyTraversed(TraverseEvent evt)
    {
        if (evt.keyCode == SWT.TAB)
            evt.doit = false;
    }
}

tableViewer.getTable().addKeyListener(keyListener);
tableViewer.getTable().addTraverseListener(traverseListener);

أيضا، كما اقترح Derbiggi، يجب إضافة المستمعين إلى إضافة كائن الجدول، وليس الجدول ".

نصائح أخرى

على الرغم من الحل انهياتوس تم النشر منخفضا للغاية وربما يعمل (لم أختبره)، يمنحك JFACE إطارا لهذه المشكلة المحددة. انظر org.eclipse.jface.viewers.TableViewerFocusCellManager جنبا إلى جنب مع org.eclipse.jface.viewers.CellNavigationStrategy الفصول لحل هذه المشكلة.

لم أستطع الحصول على السلوك المطلوب مع TraverseListener (لن يعبر داخل الطاولة)، واجهت مشكلة في الحصول عليها مع FocusCellManager و CellNavigationStrategy. وبعد أخيرا، وجدت هذا الحل الذي تمكنني من علامة التبويب من العمود إلى العمود على صف واحد وتفعيل المحرر تلقائيا.

Viewer viewer =  ...

TableViewerFocusCellManager focusCellManager =
    new TableViewerFocusCellManager(
        viewer,
        new FocusCellHighlighter(viewer) {});

ColumnViewerEditorActivationStrategy editorActivationStrategy =
    new ColumnViewerEditorActivationStrategy(viewer) {

            @Override
            protected boolean isEditorActivationEvent(
                ColumnViewerEditorActivationEvent event) {
                    ViewerCell cell = (ViewerCell) event.getSource();
                   return cell.getColumnIndex() == 1 || cell.getColumnIndex() == 2;
            }

};

TableViewerEditor.create(viewer, focusCellManager, editorActivationStrategy,
    TableViewerEditor.TABBING_HORIZONTAL);

تحتاج إلى إضافة مفتاح KeySistener وتعيين التحديد أو التركيز على الخلية التالية:

tableViewer.getTable().addKeyListener(new KeyListener(){

   public void keyPressed(KeyEvent e) {

       System.out.println("Key Pressed");
       if (e.keycode == SWT.TAB)
       {
           System.out.println("Detected TAB key");
           // set table viewer selection
       }
   }

   public void keyReleased(KeyEvent e) {

       System.out.println("Key Released");
   }}
);

اضطررت أيضا إلى تنفيذ جداب بين العناصر في جدول. نستخدم الشبكة من سديم مثل الطاولة. أولا، اضطررت إلى قمع جداب التركيز منعه من الخروج من الطاولة.enter image description here

ثم أضفت مستمعا رئيسيا يتحرك التركيز / التحديد إلى الخلية التالية:

enter image description here

كما أنني جعلت خوارزمية الخاصة بي لنقل اختيار خلية واحدة إلى اليمين ومتى في نهاية الصف، انقله إلى بداية الصف التالي. عند الوصول إلى نهاية الجدول، يتحرك التحديد مرة أخرى إلى الخلية الأولى في الجدول.

هذا حل المشكلة بالنسبة لي.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top