Java:如果行由 AbstractTableModel 插入,如何将行(数据)显式插入 JTable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9697546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: How to insert rows(data) into JTable explicitly if rows are inserted by AbstractTableModel
提问by Vinit Vikash
In my application there is a JTable and I want to insert row after creating table.
在我的应用程序中有一个 JTable,我想在创建表后插入行。
Following all codes are in the constructor of the frame.
以下所有代码都在框架的构造函数中。
Code:
代码:
private TableModel model = new AbstractTableModel(){
String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"};
private Object[][] data = {};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public boolean isCellEditable(int row, int col) {
retuen false;
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
};
table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(5, 218, 884, 194);
//now adding this to the frame where I want to show
frame.add(scrollPane);
now I want to insert rows or data into the table. How this is possible. Previously I used DefaultTableModelbut we cannot use isCellEditable and other method in DefaultTableModel so I change that with the code above. But in the above code I am not able to insert data (rows) explicitly, please help me.
现在我想在表中插入行或数据。这怎么可能。以前我使用过DefaultTableModel但我们不能在 DefaultTableModel 中使用 isCellEditable 和其他方法,所以我用上面的代码改变了它。但是在上面的代码中我无法显式插入数据(行),请帮助我。
采纳答案by chenyi1976
Like this, you can implement other method like deleteRow(int rowIndex) and insertRowToIndex(int rowIndex, List rowData).
像这样,您可以实现其他方法,如 deleteRow(int rowIndex) 和 insertRowToIndex(int rowIndex, List rowData)。
Rememer after you change the data, you have to fire the table event, like fireTableRowsInserted() etc
更改数据后,您必须触发 table 事件,例如 fireTableRowsInserted() 等
public static class MyTableModel extends AbstractTableModel
{
private List<String> columnNames = new ArrayList();
private List<List> data = new ArrayList();
{
columnNames.add("First Name");
columnNames.add("Last Name");
columnNames.add("Sport");
columnNames.add("# of Years");
columnNames.add("Vegetarian");
}
public void addRow(List rowData)
{
data.add(rowData);
fireTableRowsInserted(data.size() - 1, data.size() - 1);
}
public int getColumnCount()
{
return columnNames.size();
}
public int getRowCount()
{
return data.size();
}
public String getColumnName(int col)
{
try
{
return columnNames.get(col);
}
catch(Exception e)
{
return null;
}
}
public Object getValueAt(int row, int col)
{
return data.get(row).get(col);
}
public boolean isCellEditable(int row, int col)
{
return false;
}
public Class getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
};
public static void main(String[] args)
{
MyTableModel model = new MyTableModel();
model.addRow(Arrays.asList("yi", "chen", "sleep", 35, true));
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(5, 218, 884, 194);
//now adding this to the frame where I want to show
JFrame frame = new JFrame();
frame.add(scrollPane);
frame.setVisible(true);
}
回答by Peter
Just extending from DefaultTableModel
instead of extending from AbstractTableModel
should do the trick.
只是从而DefaultTableModel
不是从延伸AbstractTableModel
应该可以解决问题。
回答by Rahul Borkar
AbstractTableModel will not do what you want to do, For this purpose you need to use DefaultTableModel,
AbstractTableModel 不会做你想做的事,为此你需要使用 DefaultTableModel,
When you use DefaultTableModel you can set isCellEditable or any other method in following ways,
当您使用 DefaultTableModel 时,您可以通过以下方式设置 isCellEditable 或任何其他方法,
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
};
in above code data will be String[ ][ ]. And by writing above code you are setting cell editable as false.
在上面的代码中,数据将是 String[][]。通过编写上面的代码,您将单元格可编辑设置为 false。
Other than this you can also load data in swingworker or afterwords like you might have done.
除此之外,您还可以像之前所做的那样在 swingworker 或 afterwords 中加载数据。
回答by mKorbel
@Peter is right +1 use DefaultTableModel
rather than AbstractTableModel
, because your AbstractTableModel
missed method addRow();
(and another methods too)
@Peter 是正确的 +1 useDefaultTableModel
而不是AbstractTableModel
,因为您AbstractTableModel
错过了方法addRow();
(以及其他方法)
public void addRow(Object[][] data) {
this.data.add(data);
this.fireTableRowsInserted(data.size() - 1, data.size() - 1);
}
example about AbstractTableModel
回答by Korhan Ozturk
I think you need to implement setValueAt()
function in your AbstractTableModel so that you can modify any data given the position.
我认为您需要setValueAt()
在 AbstractTableModel 中实现功能,以便您可以修改给定位置的任何数据。
Take a close look at the following tutorials:
仔细看看以下教程:
http://www.java2s.com/Code/Java/Swing-JFC/TablewithacustomTableModel.htm
http://www.java2s.com/Code/Java/Swing-JFC/TablewithacustomTableModel.htm