带有日期选择器字段的SWT表

时间:2013-09-17 19:36:39

标签: java date swt editor

这是一个显而易见的问题但由于某些原因我找不到有效的解决方案。我有一个包含3列的表,前两个是日期,第三个是我使用Spinner,因为我希望用户只输入数字。我需要所有字段都可以编辑,但是

  • 该字段只有在焦点处于焦点时才可编辑,当点击
  • 同一行中的其他字段不应该是可编辑的
  • 当该字段失去焦点时,它应该返回“默认”模式

这些都是人们在可编辑表格中所期望的正常事物。出于某种原因,我无法在SWT中完成这项工作。现在我所拥有的是:

  1. 我添加了一个新行,所有字段都是“默认”
  2. 我点击该行,所有字段都变为可编辑(日期字段变为组合框等)。
  3. 在失去焦点的行之后,它仍然可以编辑
  4. 这是我现在的代码,它基于我在互联网上找到的其他代码:

    // this will happen when you click on the table
    table.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            // Clean up any previous editor control
            final TableEditor editor1 = new TableEditor(table);               
            // The editor must have the same size as the cell and must
            // not be any smaller than 50 pixels.
            editor1.horizontalAlignment = SWT.LEFT;
            editor1.grabHorizontal = true;
            editor1.minimumWidth = 50;
            Control oldEditor = editor1.getEditor();
            if (oldEditor != null)
                oldEditor.dispose();                
    
            // Identify the selected row 
            TableItem item = (TableItem)e.item;
            if (item == null) return;
    
            // this is the editor for the first column
            DateTime startDateTxt = new DateTime(table, SWT.BORDER | SWT.DROP_DOWN | SWT.LONG);
            startDateTxt.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                    DateTime text = (DateTime)editor1.getEditor();
                    editor1.getItem().setText(0, new LocalDate(text.getYear(), text.getMonth(), text.getDay()).toString());
                }
            });
            editor1.setEditor(startDateTxt, item, 0);
    
            // and now comes the editor for the 2nd column
            //~~~~
            // Clean up any previous editor control
            final TableEditor editor2 = new TableEditor(table);               
            // The editor must have the same size as the cell and must
            // not be any smaller than 50 pixels.
            editor2.horizontalAlignment = SWT.LEFT;
            editor2.grabHorizontal = true;
            editor2.minimumWidth = 50;
            oldEditor = editor2.getEditor();
            if (oldEditor != null)
                oldEditor.dispose();                
    
            // this is the editor for the second column
            DateTime endDateTxt = new DateTime(table, SWT.BORDER | SWT.DROP_DOWN | SWT.LONG);
            endDateTxt.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                    DateTime text = (DateTime)editor2.getEditor();
                    editor2.getItem().setText(1, new LocalDate(text.getYear(), text.getMonth(), text.getDay()).toString());
                }
            });
            editor2.setEditor(endDateTxt, item, 1);
    
            //~~~~
            // Clean up any previous editor control
            final TableEditor editor3 = new TableEditor(table);               
            // The editor must have the same size as the cell and must
            // not be any smaller than 50 pixels.
            editor3.horizontalAlignment = SWT.LEFT;
            editor3.grabHorizontal = true;
            editor3.minimumWidth = 50;
            oldEditor = editor3.getEditor();
            if (oldEditor != null)
                oldEditor.dispose();                
    
            // this is the editor for the third column
            Spinner percentTxt = createNewSpinner(table, SWT.NONE);
            percentTxt.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent me) {
                    Spinner text = (Spinner)editor3.getEditor();
                    editor3.getItem().setText(2, text.getText());
                }
            });  
            editor3.setEditor(percentTxt, item, 2);   
            //~~~~
    
        }
    });
    

    正如您所看到的,当用户点击表格时,我会获得当前行,并在该行中添加3个编辑器。我想做的是获取所选列。我还没有找到如何做到这一点。如果我得到所选列,那么我将能够为所选单元格创建一个编辑器,而不是整行。

    在失去焦点之后,我还没有弄清楚如何处理编辑器。如果用户点击表外,则显然已经发生了。但是,如果用户只是点击另一个单元格怎么办?然后我将不得不处理旧编辑器并创建一个新编辑器。

1 个答案:

答案 0 :(得分:0)

要获取所选列,您可以为每列调用TableItem.getBounds(columnIndex),并查看所选内容是否在返回的矩形内。