java - 需要保持一些列可编辑和不可编辑,并且只允许双击单元格进行单元格编辑

标签 java swing jtable tablecelleditor defaulttablemodel

在我的 JTable 实现中。我必须保持一些列可编辑,一些列不可编辑,所以我覆盖了 isCellEditable -

@Override
public boolean isCellEditable(int row, int col) {
    if (col == uneditableColumn) {
        return false;
    }
    return bEdit;
}

现在我的要求是只允许在双击时编辑单元格,即如果用户双击单元格则只有它进入可编辑模式。为此 - 我必须制作您自己的 CellEditor 并覆盖。

public boolean isCellEditable( EventObject e )

有人可以建议是否可以使用 -

public boolean isCellEditable(int row, int col) 

请帮忙 -

最佳答案

is to allow edit the cell only on Double click i.e. if user double clicks 
on cell then only it comes into editable mode.

你看过DefaultCellEditor#clickCountToStart

对于 CellEditor 或方法覆盖 isCellEditable (AbstractTableModel)

@Override
public boolean isCellEditable(EventObject anEvent) {
    if (anEvent instanceof MouseEvent) {
        return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart;
    }
    return true;
}

对于DefaultTableModel会不会是

import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.*;

public class EditorRendererClickCountToStart {

    public EditorRendererClickCountToStart() {
        TableModel model = new DefaultTableModel(new Object[][]{
                    {"A", "Item 0"}, {"B", "Item 1"}, {"C", "Item 2"},
                    {"D", "Item 3"}, {"E", "Item 4"}}, new String[]{"TextField", "Combo"});
        JTable table = new JTable(model);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        DefaultCellEditor editor = new DefaultCellEditor(new JComboBox(new Object[]{
                    "Item 0", "Item 1", "Item 2", "Item 3", "Item 4"}));
        editor.setClickCountToStart(2);
        table.getColumnModel().getColumn(1).setCellEditor(editor);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                System.out.println(info.getName());
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (UnsupportedLookAndFeelException e) {// handle exception
        } catch (ClassNotFoundException e) {// handle exception
        } catch (InstantiationException e) {// handle exception
        } catch (IllegalAccessException e) {// handle exception
        }
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new EditorRendererClickCountToStart();
            }
        });
    }
}

关于java - 需要保持一些列可编辑和不可编辑,并且只允许双击单元格进行单元格编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9972643/

相关文章:

java - 安卓 Facebook : Login in One activity and Logout in another activity

java - Swing 和 Java EE 服务器

java - 使用 SwingWorker 在 JTextPane 中突出显示语法

java - JTable 标题位于左侧,单元格位于右侧 [用图像解释]

java - JTable 单元格渲染器跳过 boolean 列

java - saxon-xpath-9.1.0.8.jar!/META-INF/services/javax.xml.xpath.XPathFactory :2: Illegal configuration-file syntax

java - 如何判断一个点是在另一个点的左边还是右边

java - 将android应用程序连接到服务器上的数据库?

java - Java的Swing框架是如何制作窗口/框架的?

java - 如何保留选定的行并从 jtable 中删除其他行?