gpt4 book ai didi

java - 如何在禁用编辑的情况下选择 JTable 单元格中的文本?

转载 作者:搜寻专家 更新时间:2023-11-01 02:45:48 26 4
gpt4 key购买 nike

想象一下,我正在用 Java 构建一个 IRC 客户端,我希望聊天 View 中的富文本显示 IRC 颜色和彩色缺口。我想用 JTable 构建它。我可以这样做,但文本是不可选择的。使表格可编辑没有意义。

我也调查过:

  • TextArea - 没有富文本格式
  • JEdi​​tPane - 不能追加,只能替换性能不佳的地方
  • JList - 无法选择文本

  • 所以我得到了一个工作表,我只需要文本是可选择的而不是可编辑的。我也只想要文本内容,并且在复制文本选择时不会将任何 HTML 复制到剪贴板中。

    我尝试了 setRowSelectionAllowed() 的各种迭代, setColumnSelectionEnabled()setCellSelectionEnabled()setSelectionMode表模型为 isCellEditable() 返回 false .没有任何内容使文本可选。

    编辑:根据答案 1 我错了文本编辑器 Pane 所以我正在尝试这些解决方案。

    最佳答案

    我不知道你为什么不想使用 JTextPaneJEditorPane .您可以通过其文档插入文本。示例在这里 --> How to use Editor Panes and Text Panes .

    但是为了你的目的,你可以做这样的事情。我覆盖 changeSelection单击时选择所有文本,单元格可编辑但其单元格编辑器不可编辑。

    public class JTableTest {


    private final DefaultCellEditor cellEditor;
    private final JTextField textfield;
    private JPanel panel;
    private MyTableModel tableModel = new MyTableModel();
    private JTable table = new JTable() {

    @Override
    public TableCellEditor getCellEditor(int row, int column) {
    return JTableTest.this.cellEditor;
    }

    @Override
    public void changeSelection(
    final int row, final int column, final boolean toggle, final boolean extend) {
    super.changeSelection(row, column, toggle, extend);
    SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
    if ((getCellEditor(row, column) != null && !editCellAt(row, column))) {
    JTextField textfield=(JTextField)JTableTest.this.cellEditor.getComponent();
    textfield.selectAll();
    }
    }
    });
    }
    };

    public JTableTest() {
    JScrollPane scroll = new JScrollPane(table);
    table.setModel(tableModel);
    panel = new JPanel(new BorderLayout());
    panel.add(scroll, BorderLayout.CENTER);
    textfield = new JTextField();
    textfield.setEditable(Boolean.FALSE);
    textfield.setBorder(null);
    cellEditor = new DefaultCellEditor(textfield);
    tableModel.insertValue(new ItemRow("nonEditable", "Editable"));
    }



    private class ItemRow {

    private String column1;
    private String column2;

    public ItemRow(String column1, String column2) {
    this.column1 = column1;
    this.column2 = column2;
    }

    public String getColumn1() {
    return column1;
    }

    public void setColumn1(String column1) {
    this.column1 = column1;
    }

    public String getColumn2() {
    return column2;
    }

    public void setColumn2(String column2) {
    this.column2 = column2;
    }
    }

    private class MyTableModel extends AbstractTableModel {

    public static final int COLUMN1_INDEX = 0;
    public static final int COLUMN2_INDEX = 1;
    private final List<ItemRow> data = new ArrayList<>();

    private final String[] columnsNames = {
    "Column1",
    "Column2",};

    private final Class<?>[] columnsTypes = {
    String.class,
    String.class
    };


    public MyTableModel() {
    super();
    }

    @Override
    public Object getValueAt(int inRow, int inCol) {
    ItemRow row = data.get(inRow);
    Object outReturn = null;

    switch (inCol) {
    case COLUMN1_INDEX:
    outReturn = row.getColumn1();
    break;
    case COLUMN2_INDEX:
    outReturn = row.getColumn2();
    break;
    default:
    throw new RuntimeException("invalid column");
    }

    return outReturn;
    }

    @Override
    public void setValueAt(Object inValue, int inRow, int inCol) {
    System.out.println("Gets called ");
    if (inRow < 0 || inCol < 0 || inRow >= data.size()) {
    return;
    }

    ItemRow row = data.get(inRow);
    switch (inCol) {
    case COLUMN1_INDEX:
    row.setColumn1(inValue.toString());
    break;
    case COLUMN2_INDEX:
    row.setColumn2(inValue.toString());
    break;
    }
    fireTableCellUpdated(inRow, inCol);
    }

    @Override
    public int getRowCount() {
    return data.size();
    }

    @Override
    public int getColumnCount() {
    return columnsTypes.length;
    }

    @Override
    public String getColumnName(int inCol) {
    return this.columnsNames[inCol];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
    return this.columnsTypes[columnIndex];
    }

    /**
    *
    * @param row
    */
    public void insertValue(ItemRow row) {
    data.add(row);
    fireTableRowsInserted(data.size() - 1, data.size() - 1);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
    return true;
    }



    }


    private static void createAndShowGUI(final Container container, final String title) {
    //Create and set up the window.
    JFrame frame = new JFrame(title);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationByPlatform(Boolean.TRUE);
    frame.add(container);
    //Display the window.
    frame.pack();
    frame.setVisible(true);
    }

    public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
    createAndShowGUI(new JTableTest().panel, "Test");
    }

    });
    }

    }

    关于java - 如何在禁用编辑的情况下选择 JTable 单元格中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22440733/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com