JTable - 复合编辑器焦点

发布于 2024-09-12 09:55:23 字数 835 浏览 1 评论 0原文

我有一个由多个组件组成的自定义编辑器。类似于:

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 在编辑开始时抓住焦点。当我单击单元格时,它工作得很好,但当我使用键盘导航表格并开始在此单元格中键入时,它就不行了。

我该如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹倦 2024-09-19 09:55:23

我前段时间遇到了同样的问题,花了很长时间才找到解决方案。使用 focuslistener 和其他东西尝试了很多,但似乎没有什么真正按照我想要的方式工作,直到我发现这个 Santhosh Kumar 的有用文章

写得很好,应该可以解决你的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文