将JPanel用作JTable单元格编辑器时无法获得焦点 [英] Problem getting focus when use JPanel as JTable cell editor

查看:159
本文介绍了将JPanel用作JTable单元格编辑器时无法获得焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单元格编辑器,其中包含一个可以双击以打开编辑对话框的小按钮,然后是一个可以用于内联编辑值的文本字段(需要弹出框才能编辑其他值,只有第一个显示在JTable中.)

I have a cell editor that contains a little button that can be double clicked on to bring up an edit dialog, and then a textfield that can be used to edit the value inline (the popup is required to allow editing of additional values, only the first is shown in the JTable).

当用户单击该字段时,一切都很好,但是如果他们将其切换到该字段中,则文本字段将不会获得焦点,除非使用鼠标单击该字段,否则他们将无法编辑该字段.

When user clicks on field everything is okay, but if they tab into the field they textfield doesn't receive focus and they cannot edit the field unless they click on it with the mouse.

我尝试摆弄jpanel的各种聚焦方法,但这没什么区别,有人知道我在做什么错吗?

I tried fiddling with the various focus methods of jpanel but it made no difference, anybody know what Im doing wrong ?

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);
    }
}

试图在面板上添加焦点侦听器,似乎没有任何作用

Tried adding focuslistener to panel, didnt seem to make any difference

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可以做什么它可以工作吗?

So after tabbing into field, I type a key and it sorts of look likes focus is gained but you cannot enter anything into the field whereas if I type RETURN then I can start editing the field, what does pressing RETURN do that allows it to work ?

推荐答案

按RETURN可以使它正常工作吗?

what does pressing RETURN do that allows it to work?

如方便的> 键绑定所示应用程序,大多数L& F中默认的ENTER键绑定是notify-field-accept.目前尚不清楚为什么ActionListener 开始 使用stopCellEditing().我希望它在更新数据模型后调用fireEditingStopped() ,如该

As shown in the handy Key Bindings application, the default ENTER key binding in most L&Fs is notify-field-accept. It's not clear why your ActionListener begins with stopCellEditing(). I would have expected it to invoke fireEditingStopped() after updating the data model, as suggested in this example.

可悲的是,我不熟悉 Jaikoz .您可能会看 概念:编辑器和渲染器 和后续部分,以获取更多指导.

Sadly, I'm unfamiliar with Jaikoz. You might look at Concepts: Editors and Renderers and the subsequent sections for additional guidance.

附录:如您的注释中所述,DefaultCellEditor中的JTextField允许默认输入所选字段.从您的示例尚不清楚该默认值如何被取消.如果没有显示该问题的 sscce ,您可以将代码与相关的

Addendum: As noted in your comment, a JTextField in a DefaultCellEditor allows typing in the selected field by default. It's not clear from your example how that default is being nullified. Absent an sscce that demonstrates the problem, you might compare your code with this related example that exhibits the default behavior using a subclass of JTextField.

这篇关于将JPanel用作JTable单元格编辑器时无法获得焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆