簡體   English   中英

使用JPanel作為JTable單元格編輯器時出現問題

[英]Problem getting focus when use JPanel as JTable cell editor

我有一個單元格編輯器,其中包含一個可以雙擊以顯示編輯對話框的小按鈕,然后是一個可用於編輯內聯值的文本字段(需要彈出窗口才能編輯其他值,只有首先顯示在JTable中)。

當用戶點擊字段時,一切正常,但如果他們在字段中顯示,則文本字段不會獲得焦點,除非他們用鼠標點擊它,否則他們無法編輯字段。

我試圖擺弄jpanel的各種焦點方法,但它沒有任何區別,任何人都知道我做錯了什么?

package com.jthink.jaikoz.celleditor;

import com.jthink.jaikoz.celldata.Cell;
import com.jthink.jaikoz.guielement.Focus;
import com.jthink.jaikoz.table.CellLocation;
import com.jthink.jaikoz.table.DatasheetToggleButton;
import com.jthink.jaikoz.table.datasheet.Datasheet;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class SimpleMultiRowCellEditor
    extends DefaultCellEditor implements ActionListener
{

    final JPanel panel;
    private final DatasheetToggleButton rowCount;
    Cell value;

    public SimpleMultiRowCellEditor(final JTextField text)
    {
        super(text);
        this.setClickCountToStart(1);

        rowCount = new DatasheetToggleButton();
        rowCount.setVisible(true);
        rowCount.addActionListener(this);
        panel = new JPanel();
        panel.setOpaque(false);
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        panel.add(rowCount);
        panel.add(editorComponent);
        /*panel.setFocusable(true);
        panel.setFocusCycleRoot(true);
        ArrayList focusOrder = new ArrayList();
        focusOrder.add(editorComponent);
        focusOrder.add(rowCount);
        focusOrder.add(panel);
        panel.setFocusTraversalPolicy(new Focus(focusOrder));
        */
    }

    public Component getTableCellEditorComponent(
        final JTable table, final Object val, final boolean isSelected,
        final int row, final int column)
    {
        value = (Cell) ((Cell) val).clone();
        rowCount.setText(String.valueOf(value.getValues().size()));
        delegate.setValue(value.getValue());
        return panel;
    }

    public Object getCellEditorValue()
    {
        final String s = (String) delegate.getCellEditorValue();
        value.setValue(s);
        return value;
    }

    public void actionPerformed(final ActionEvent e)
    {
        this.stopCellEditing();
        final CellLocation cl =  Datasheet.getActiveEditSheet()
            .getTable().getSelectedCellLocations().get(0);
        UpdateMultiRowCellDialog.getInstanceOf().display(value,cl);
    }
}

嘗試將focuslistener添加到面板,似乎沒有任何區別

class PanelFocusListener implements FocusListener
{
    public void focusGained(FocusEvent e)
    {
        System.out.println("Gained Focus");
        editorComponent.requestFocusInWindow();
    }

    public void focusLost(FocusEvent e)
    {
        System.out.println("Lost Focus");

    }
}

因此,在標記到字段后,我鍵入一個鍵,它看起來像焦點是獲得但是你不能在字段中輸入任何東西,而如果我鍵入RETURN然后我可以開始編輯字段,按RETURN做什么允許它工作?

什么按RETURN可以讓它工作?

如方便的Key Bindings應用程序所示,大多數L&F中的默認ENTER鍵綁定是notify-field-accept 目前尚不清楚為什么你的ActionListenerstopCellEditing() 開頭 我希望它更新數據模型調用fireEditingStopped() ,如本例所示

可悲的是,我對Jaikoz不熟悉。 您可以查看概念:編輯器和渲染器以及后續部分以獲取其他指導。

附錄:如您的評論中所述, DefaultCellEditorJTextField允許在默認情況下鍵入所選字段。 從您的示例中不清楚該默認值是如何無效的。 如果沒有演示此問題的sscce ,您可以將代碼與使用JTextField的子類展示默認行為的相關示例進行比較。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM