下面是我用于使用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)
来实现这一点。
但我正在尝试制作表格,以便可以选择每一行但单元格不应该是可编辑的。是否有任何直接的方法来实现这一目标?
注意:我尝试覆盖this post中指定的函数isCellEditable
。但是当运行屏幕时只显示空表。
答案 0 :(得分: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; }
});
为了实现这一目标,您需要制作变量columnNames
和dataValues
final
。
答案 1 :(得分: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();
}
});
}
}
答案 2 :(得分:0)
编写自定义表模型(您必须在其中嵌入数据值并覆盖getValueAt方法以返回这些模型),然后覆盖其isCellEditable()以返回false。