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

时间:2011-10-04 12:19:00

标签: java swing jtable

我有一个表来显示值,在这个表中用户可以选择一行,但不能更改其内容。我的代码是:

    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);

但是当我点击它们时,现在不会选择表行。我怎么能解决这个问题?

2 个答案:

答案 0 :(得分:6)

选择行的能力与编辑给定行中的单元格无关。

问题是你的SubractTableRenderer负责进行突出显示。您需要在渲染器中使用类似以下内容的代码:

@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;
}

答案 1 :(得分:3)

JTable实现

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

例如

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();
            }
        });
    }
}