JTable-复合编辑器焦点 [英] JTable - compound editor focus

查看:77
本文介绍了JTable-复合编辑器焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由几个组件组成的自定义编辑器.像这样:

class MyCellEditor extends AbstractCellEditor implements TableCellEditor {
    JTextArea textArea;
    JButton button;
    JPanel panel;

    MyCellEditor() {
        textArea = new JTextArea();
        button = new JButton();
        panel = new JPanel(new BorderLayout());
        panel.add(textArea, BorderLayout.CENTER);
        panel.add(button, BorderLayout.EAST);
    }

    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {
        textArea.setText((String) value);
        return panel;
    }

    public Object getCellEditorValue() {
        return textArea.getText();
    }
}

我希望内部的textArea在开始编辑时能抓住焦点.当我单击该单元格时,它的效果很好,但是当我使用键盘导航该表并开始在此单元格中键入内容时,效果不佳.

我该如何解决?

解决方案

前段时间,我遇到了同样的问题,花了我很长时间才找到解决方案.在focuslistener和东西上进行了很多尝试,但是直到我发现此 Santhosh Kumar的有用文章.

它写得很好,应该可以解决您的问题.

I have a custom editor composed of several components. Something like:

class MyCellEditor extends AbstractCellEditor implements TableCellEditor {
    JTextArea textArea;
    JButton button;
    JPanel panel;

    MyCellEditor() {
        textArea = new JTextArea();
        button = new JButton();
        panel = new JPanel(new BorderLayout());
        panel.add(textArea, BorderLayout.CENTER);
        panel.add(button, BorderLayout.EAST);
    }

    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {
        textArea.setText((String) value);
        return panel;
    }

    public Object getCellEditorValue() {
        return textArea.getText();
    }
}

I want the inner textArea to grab focus when editing starts. It works just fine when I click the cell, but not when I navigate the table with keyboard and start typing in this cell.

How can I fix this?

解决方案

I had the same problem some time ago and took me ages to find a solution. Tried a lot with focuslistener and stuff, but nothing really seemed to work the way I wanted it to until I found this useful article by Santhosh Kumar.

Its well written and should fix your problem.

这篇关于JTable-复合编辑器焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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