Problem getting focus when use JPanel as JTable cell editor

ぐ巨炮叔叔 提交于 2019-12-13 14:24:10

问题


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.

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

    }
}

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 ?


回答1:


what does pressing RETURN do that allows it to work?

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.

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

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.



来源:https://stackoverflow.com/questions/3986074/problem-getting-focus-when-use-jpanel-as-jtable-cell-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!