简体   繁体   English

当每个单元格不可编辑时,可以选择JTable行

[英]Make JTable rows selectable when every cell is non-editable

I have a table to show values, in this table user can select one row, but can not change its content. 我有一个表来显示值,在这个表中用户可以选择一行,但不能更改其内容。 My code is: 我的代码是:

    JTable tablaCurvas1 = new JTable();
    TableCellRenderer tableRender = new SubtractTableRenderer();
    tablaCurvas1.setDefaultRenderer(Object.class, tableRender);
    tablaCurvas1.setModel(new DefaultTableModel(rowData, columnNames){
        private static final long serialVersionUID = 1L;
    @Override
       public boolean isCellEditable(int row, int column) {
           return false; //So I make every cell non-editable
       }

    });

    JScrollPane scrollPane = new JScrollPane(tablaCurvas1);
    tablaCurvas1.setFillsViewportHeight(true);
    tablaCurvas1.setPreferredScrollableViewportSize(new Dimension(200,100));
    pane.add(scrollPane,c);

But now table rows do not get selected when I click over them. 但是当我点击它们时,现在不会选择表行。 How could I solve this? 我该怎么解决这个问题?

The ability to select a row has nothing to do with the ability to edit a cell in the given row. 选择行的能力与编辑给定行中的单元格的能力无关。

The problem is your SubractTableRenderer which is responsible for doing the highlighting. 问题是你的SubractTableRenderer负责突出显示。 You need code something like the following in the renderer: 您需要在渲染器中使用以下代码:

@Override
public Component getTableCellRendererComponent(
    JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
    super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

    // add custom rendering code

    if (isSelected)
        setBackground( table.getSelectionBackground() );
    else
        setBackground( table.getBackground() );

    return this;
}

JTable implements JTable实现

table.setRowSelectionAllowed(boolean);
table.setColumnSelectionAllowed(boolean);
table.setCellSelectionEnabled(boolean); 

for example 例如

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class MyTable {

    public MyTable() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(new JScrollPane(createTable()));
        JFrame frame = new JFrame("My Table");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.setSize(new Dimension(400, 130));
        frame.setVisible(true);
    }

    private JTable createTable() {
        Object[][] data = {{"Nazli", "Shahi", new Date()},
            {"Nima", "Sohrabi", new Date()},
            {"Farsheed", "Tari", new Date()},
            {"Anousheh", "Modaressi", new Date()}};
        String[] columnNames = {"First Name", "Last Name", "DOB"};
        DefaultTableModel model = new DefaultTableModel(data, columnNames) {

            private static final long serialVersionUID = 1L;

            @Override
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        JTable table = new JTable(model);
        table.setAutoCreateRowSorter(true);
        table.setRowSelectionAllowed(true);
        table.setColumnSelectionAllowed(true);
        table.setCellSelectionEnabled(true);
        return table;
    }

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

            @Override
            public void run() {
                MyTable myTable = new MyTable();
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM