views:

49

answers:

1

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  A: 

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.

trashgod
Ignore the ActionListener thats used when double click on the button but is irrelavant to this question. I suppose regarding the RETURN I want to know how to make the pressing of any key make the textfield ready for editing like the return key, which is what any key does for a standard JTextfield cell editor. Familarity with jaikoz is not required ;)
For reference, I've added a link to a related example above.
trashgod
Okay so in creating a sscce I made some progress, using table.setSurrendersFocusOnKeystroke(true) helps, and I realised that my code was already mapping the RETURN key to something different then the default char, but Im now hitting another problem, as its different Ive created a new question http://stackoverflow.com/questions/3995619/losing-first-character-in-jtable-panel-based-cell-editor
Sorry this answer wasn't helpful.
trashgod
It was useful, it forced me to try harder and get a solution ;)