Jtable - Caret没有显示在单元格StartEdit + requestFocus()上

时间:2017-04-23 01:20:50

标签: java swing jtable

我设置了自定义TableCellEditor / AbstractCellEditor,以便在双击单元格或按空格键时,它进入编辑模式。

class TextAreaCellEditor extends AbstractCellEditor implements TableCellEditor {

        JComponent component = new JTextArea();

        @Override
        public boolean isCellEditable(EventObject e) {
            GenInput inp = new GenInput();
            if (super.isCellEditable(e)) {
                if (e instanceof MouseEvent) {
                    MouseEvent me = (MouseEvent) e;
                    return me.getClickCount() >= 2;
                }
                if (e instanceof KeyEvent) {
                    KeyEvent ke = (KeyEvent) e;
                    return ke.getKeyCode() == inp.spacebar; //'inp' is my own class that acts as a reference for keycodes
                }
            }
            return false;
        }
       ...

这两个都有效,并激活StartEdit,这意味着下一步执行以下代码:

@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
        int rowIndex, int vColIndex) {

    ((JTextArea) component).setText("");
    ((JTextArea) component).setWrapStyleWord(true);
    ((JTextArea) component).setLineWrap(true);
    ((JTextArea) component).requestFocus();
    ((JTextArea) component).addKeyListener(new KeyListener() {
    ...

工作部分:无论我是否包含"((JTextArea)组件).requestFocus();" ,当我双击它时,完全进入编辑模式,即。插入符号开始闪烁,当我按下' up'或者' down'插入符号在多行文本框中上下移动,而不是向上或向下跳到下一行。

问题:然而,当我按下空格键时,它会进入某种“部分”状态。编辑模式;结果是,插入符号没有闪烁,虽然我可以在框中输入单词,但如果我按下' /' down'它将跳转到上一行/下一行。

当我开始时,我该怎么做才能显示插入符号(即进入完全编辑模式'而不是“部分编辑模式”#39;我已经描述过)通过空格键编辑?

编辑:"((JTextArea)组件).getCaret()。setVisible(true);"使插入符号显示,但没有改变它处于部分编辑模式的事实,所以我仍然无法按下/按下而不会失去焦点。

2 个答案:

答案 0 :(得分:0)

以下是解决方案:

table.setSurrendersFocusOnKeystroke(真);

上下文:

@Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
                int rowIndex, int vColIndex) {

            table.setSurrendersFocusOnKeystroke(true);
            ((JTextArea) component).setText("");
            ((JTextArea) component).setWrapStyleWord(true);
            ((JTextArea) component).setLineWrap(true);
            ((JTextArea) component).setFocusable(true);

答案 1 :(得分:0)

适合我的工作

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                DefaultTableModel model = new DefaultTableModel(10, 1);
                JTable table = new JTable(model);
                table.setDefaultEditor(Object.class, new TextAreaCellEditor());
                table.setRowHeight(20);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    class TextAreaCellEditor extends AbstractCellEditor implements TableCellEditor {

        JTextArea component = new JTextArea();

        @Override
        public boolean isCellEditable(EventObject e) {
            if (super.isCellEditable(e)) {
                if (e instanceof MouseEvent) {
                    MouseEvent me = (MouseEvent) e;
                    return me.getClickCount() >= 2;
                }
                if (e instanceof KeyEvent) {
                    KeyEvent ke = (KeyEvent) e;
                    return ke.getKeyCode() == KeyEvent.VK_SPACE;
                }
            }
            return false;
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
                                                                                                 int rowIndex, int vColIndex) {

            ((JTextArea) component).setText("");
            ((JTextArea) component).setWrapStyleWord(true);
            ((JTextArea) component).setLineWrap(true);
            return  component;
        }

        @Override
        public Object getCellEditorValue() {
            return component.getText();
        }
    }   
}

也许代码中的其他内容正在弄乱它。考虑提供一个runnable example来证明您的问题。这不是代码转储,而是您正在做的事情的一个示例,它突出了您遇到的问题。这将减少混淆和更好的响应