我想创建一个可以动态添加行的JTable,最初表中应该没有数据。 我这样做是通过创建一个TableModel类扩展AbstractTableModel,代码附加。
class MyTableModel extends AbstractTableModel{
private static int initialRowCount = 3;
private ArrayList<ArrayList<Object>> data;
private static String[] columnName = {"Date Tested","Product","VI Test Result","Lot ID","MCN","SLT TIS","Tester","In","First Pass","FP%","RT1 Pass","RT2 Pass","Final Pass",
"SLT Final Yield %","SLT Fail Quantity","Remark"};
int i=0;
public MyTableModel(){
data = new ArrayList<ArrayList<Object>>(20);
}
public int getColumnCount() {
System.out.println(i++);
return columnName.length;
}
public int getRowCount() {
return initialRowCount;
}
public Object getValueAt(int r, int c) {
}
public String getColumnName(int c) {
return columnName[c];
}
public boolean isCellEditable(int row, int column) {
if(column==0)
return false;
else
return true;
}
public void setValueAt(Object vaule,int row,int column){
data.add();
}
public void addRow(){
initialRowCount++;
}
}
我的问题是,我必须使用2-D ArrayList作为数据容器吗?如果是这样,我应该如何在此代码中实现它? 我是Java新手,刚开始学习Swing,感谢任何帮助。
答案 0 :(得分:2)
您可以尝试使用自定义JTable
(TableModel
)的MyModel
的下一个简单示例,该示例允许添加/删除行和列。存储在包装对象(RowData
)中的行数据。模型中不支持过滤和排序,但您只需添加它们即可。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
public class TestFrame extends JFrame{
private MyModel model;
private JTable table;
public TestFrame(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
pack();
setVisible(true);
}
private void init() {
table = new JTable(model = new MyModel());
JButton add = new JButton("add row");
add.addActionListener(getAddListener());
JButton remove = new JButton("remove row");
remove.addActionListener(getRemoveListener());
JButton addC = new JButton("add column");
addC.addActionListener(getAddColListener());
JButton removeC = new JButton("remove column");
removeC.addActionListener(getRemoveColListener());
add(new JScrollPane(table));
JPanel p = new JPanel();
p.add(add);
p.add(remove);
p.add(addC);
p.add(removeC);
add(p,BorderLayout.SOUTH);
}
private ActionListener getRemoveColListener() {
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedColumn = table.getSelectedColumn();
if(selectedColumn != -1){
stopEditing();
model.removeColumn(selectedColumn);
}
}
};
}
private ActionListener getAddColListener() {
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
model.addColumn();
}
};
}
private ActionListener getRemoveListener() {
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if(selectedRow != -1){
stopEditing();
model.removeRow(selectedRow);
}
}
};
}
private ActionListener getAddListener() {
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
model.addRow();
}
};
}
private void stopEditing() {
TableCellEditor cellEditor = table.getCellEditor();
if(cellEditor != null){
cellEditor.stopCellEditing();
}
}
public static void main(String... strings) {
new TestFrame();
}
private class RowData{
private Map<Integer, Object> values = new HashMap<Integer, Object>();
public Object getValueForCol(int columnIndex) {
if(values.containsKey(columnIndex)){
return values.get(columnIndex);
}
return "";
}
public void setValueForCol(Object aValue, int columnIndex) {
values.put(columnIndex, aValue);
}
}
private class MyModel extends AbstractTableModel{
int colIndex=0;
private List<Integer> cols = new ArrayList<Integer>();
private List<RowData> rows = new ArrayList<TestFrame.RowData>();
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public String getColumnName(int column) {
return cols.get(column).toString();
}
@Override
public int getRowCount() {
return rows.size();
}
public void addRow() {
rows.add(new RowData());
fireTableRowsInserted(rows.size(), rows.size());
}
public void removeRow(int selectedRow) {
rows.remove(selectedRow);
fireTableRowsDeleted(selectedRow, selectedRow);
}
public void removeColumn(int selectedColumn) {
cols.remove(table.convertColumnIndexToModel(selectedColumn));
fireTableStructureChanged();
}
public void addColumn() {
cols.add(++colIndex);
fireTableStructureChanged();
}
@Override
public int getColumnCount() {
return cols.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
RowData rowData = rows.get(rowIndex);
return rowData.getValueForCol(cols.get(columnIndex));
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
RowData rowData = rows.get(rowIndex);
rowData.setValueForCol(aValue,cols.get(columnIndex));
fireTableCellUpdated(rowIndex, columnIndex);
}
}