1

下面是我用来使用 JTable 生成表数据的代码

public class Scroller extends JFrame {

    public Scroller() throws HeadlessException {

            String columnNames[] = { "Location", "Lived In People" };

            // Create some data
            String dataValues[][] =
            {
                { "100 Hamilton", "" },
                { "", "Balan" },
                { "", "Kris" },
                { "Parkwood place", "" },
                { "", "Kris" }
            };

        JTable table = new JTable(dataValues, columnNames);
        table.setPreferredSize(new Dimension(250, 600));

        final JScrollPane scroll = new JScrollPane(table);
        scroll.getVerticalScrollBar().setUnitIncrement(50);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        add(scroll, BorderLayout.CENTER);
        setSize(300, 300);
        setVisible(true);

    }

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

            public void run() {
                new Scroller().setVisible(true);
            }
        });
    }

}

输出如下所示

在此处输入图像描述

现在,我可以点击每一行。但是当我双击任何单元格时,它可以让我编辑单元格中的数据。我发现我可以用它table.setEnabled(false)来避免这种情况发生。

但我正在尝试制作表格,以便可以选择每一行,但单元格不应该是可编辑的。有没有直接的方法来实现这一目标?

注意:我试图覆盖这篇文章isCellEditable中指定的函数。但运行时屏幕只显示空表。

4

3 回答 3

2

您正在使用 TableModel,它通过始终返回 true 来实现 isCellEditable。您可以通过编写一个始终返回 false 的表模型来实现您想要的行为。例子:

public class MyTableModel extends DefaultTableModel {

 public MyTableModel(Vector data, Vector columnNames) {
    setDataVector(data, columnNames);
 }

 @Override
 public boolean isCellEditable(int row, int column) {
         return false;
     }
}

并将这一行添加到您的代码中:

table.setModel(new MyTableModel(data, columnNames));

选择

如果您想坚持使用双数组,请尝试:

JTable table = new JTable(new AbstractTableModel() {
            public String getColumnName(int column) { return columnNames[column].toString(); }
            public int getRowCount() { return dataValues.length; }
            public int getColumnCount() { return columnNames.length; }
            public Object getValueAt(int row, int col) { return dataValues[row][col]; }
            public boolean isCellEditable(int row, int column) { return false; }
        });

为此,您需要将变量columnNamesdataValues final.

于 2013-05-07T12:43:56.120 回答
1

回答 5-6 个潜在点中的第一个

i cannot see any data in the table. Its loaded empty

代码示例(所有内容都声明为局部变量)

import java.awt.GridLayout;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class SimpleTableDemo extends JPanel {

    private static final long serialVersionUID = 1L;
    private JTable table;
    private String[] columnNames = {"First Name",
        "Last Name", "Sport", "# of Years", "Vegetarian", "Date"};
    private Object[][] data = {
        {"Kathy", "Smith", "Snowboarding", new Integer(5), (false), new Date()},
        {"John", "Doe", "Rowing", new Integer(3), (true), new Date()},
        {"Sue", "Black", "Knitting", new Integer(2), (false), null},
        {"Jane", "White", "Speed reading", new Integer(20), (true), new Date()},
        {"Joe", "Brown", "Pool", new Integer(10), (false), new Date()},};
    private TableModel 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 col) {
            switch (col) {
                case 1:
                    return true;
                case 3:
                    return true;
                default:
                    return false;
            }
        }
    };

    public SimpleTableDemo() {
        super(new GridLayout(1, 0));
        table = new JTable(model);
        table.setRowHeight(20);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.setFillsViewportHeight(true);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("SimpleTableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SimpleTableDemo newContentPane = new SimpleTableDemo();
        frame.add(newContentPane);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
于 2013-05-07T12:54:59.240 回答
0

编写一个自定义表模型(您必须在其中嵌入数据值并覆盖 getValueAt 方法以返回这些值),然后覆盖其 isCellEditable() 以返回 false。

于 2013-05-07T12:46:55.883 回答