JTable中删除列的问题,请高手做答,问题解决立刻给分 通过TableColumnModel添加和删除列不会影响表格的数据,仅影响哪些显示在表格中的列。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我不可能插入列的时候,再从XML里面读一把吧?我现在想单一的只对table进行操作,如何实现呢? You can implement your own tableModel. 需要通知你的tableModel内容更新fireTableRowsDeleted();fireTableDataChanged();fireTableRowsInserted(); setLayout(new BorderLayout()); String[][] row = {{"111", "222"},{"333","444"}}; String[] col = {"aaa", "bbb"}; DefaultTableModel dtm = new DefaultTableModel(row, col); JTable table = new JTable(dtm); this.add(new JScrollPane(table), BorderLayout.CENTER); //添加两列 dtm.addColumn("ccc"); dtm.addColumn("ddd"); //删除一列(“ccc”) table.getColumnModel().removeColumn(table.getColumnModel().getColumn(2)); 那是因为你只是删除了ColumnModel中的数据,TableModel仍然保留原来的数据结构。但TableModel并没有提供这样的接口方法,你只有自己去扩展。好的方法是将TableModel只作为显示层,你对结构的任何变动都通过操作数据层来完成例如在删除一列数据时,需要删除row和col ((Vector)row.get(0)).removeElementAt(1); ((Vector)row.get(1)).removeElementAt(1); col.removeElementAt(1); dtm.setDataVector(row , col); 我现在不仅仅涉及每个单元格的数据,我每个单元格还有字体、字色、背景色等属性,要是这么一移动,不很麻烦吗?在显示层,怎么能让他插入的时候,别显示出以前删除的列啊,在我保存的XML串里,插入列后是不影响,最主要就是显示有问题 怎么会麻烦呢?你所需要进行增删改操作的是数据对象,而单元格的字体、字色、背景色等属性怎么来显示与你的数据结构是没有关系的举个简单的例子:当前为一个三行两列的表,你规定第二列(或者你规定列名为“***”)的显示为粗体,背景为蓝色,其他均为默认显示。然后,你删除数据结构中第二列数据,重新设置(tableModel.setDataVector(row , col);)tableModel会重排表信息,并按照你所规定的要求显示数据 tableModel里面有办法把删除的列彻底移除吗? Give you an example. You can add as many colums as you can. public class Panel extends JPanel { private JTable table_ = new JTable(); private ButtonPanel buttonPanel_ = new ButtonPanel(); public Panel() { JScrollPane scrollPane = new JScrollPane(); scrollPane.getViewport().add(table_); setLayout(new BorderLayout()); add(scrollPane, BorderLayout.CENTER); add(buttonPanel_, BorderLayout.SOUTH); setModel(); } private void setModel() { Vector colums = new Vector(); colums.add("Colum1"); colums.add("Colum2"); MyTableModel tableModel = new MyTableModel(colums); table_.setModel(tableModel); } private void addCol(String newColName) { ( (MyTableModel) table_.getModel()).addCol(newColName); } private void removeCol() { ( (MyTableModel) table_.getModel()).removeCol(); } private class ButtonPanel extends JPanel { private JButton addBtn_ = new JButton("Add Colum"); private JButton removeBtn_ = new JButton("Remove Colum"); ButtonPanel() { this.setLayout(new FlowLayout()); ActionListener listener = new BtnListener(); this.add(addBtn_); this.add(removeBtn_); addBtn_.addActionListener(listener); removeBtn_.addActionListener(listener); } class BtnListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == addBtn_) addCol("Test"); else if (e.getSource() == removeBtn_) removeCol(); } } } private class MyTableModel extends AbstractTableModel { Vector value_ = null; public MyTableModel(Vector value) { value_ = value; } public void refresh() { fireTableDataChanged(); } public int getColumnCount() { return value_.size(); } public int getRowCount() { return 0; } public String getColumnName(int col) { return (String) value_.get(col); } public Object getValueAt(int row, int col) { return null; } public boolean isCellEditable(int row, int col) { return true; } public void setValueAt(Object value, int row, int col) { } public void removeCol() { int lastElement = value_.size()-1; if(lastElement < 0) return; value_.remove(lastElement); fireTableStructureChanged(); } public void addCol(String newColName) { value_.add(newColName); fireTableStructureChanged(); } }} FileOutputStream读取文件夹时出错 请教一个IREPORT报表的问题 关于javamail java里有没有双重继承啊?怎么写? 请问一个eclipse写的java程序如何打包成类似lumaqq那种可以在win下用的? 如何在windowClosing()中取消关闭窗口 JAVA和COM 为什么我同一个包下的类无法直接引用? 线程问题 ImageBinaryGray包中只有一个类,import ImageBinaryGray.*为什么报错 在java中如何获取显示器分辨率的植 关于jdbc数据库程序生成jar得问题,请一定要帮忙看看。
fireTableRowsDeleted();
fireTableDataChanged();
fireTableRowsInserted();
String[][] row = {{"111", "222"},{"333","444"}};
String[] col = {"aaa", "bbb"};
DefaultTableModel dtm = new DefaultTableModel(row, col);
JTable table = new JTable(dtm);
this.add(new JScrollPane(table), BorderLayout.CENTER);
//添加两列
dtm.addColumn("ccc");
dtm.addColumn("ddd");
//删除一列(“ccc”)
table.getColumnModel().removeColumn(table.getColumnModel().getColumn(2));
好的方法是将TableModel只作为显示层,你对结构的任何变动都通过操作数据层来完成
例如在删除一列数据时,需要删除row和col
((Vector)row.get(0)).removeElementAt(1);
((Vector)row.get(1)).removeElementAt(1);
col.removeElementAt(1);
dtm.setDataVector(row , col);
当前为一个三行两列的表,你规定第二列(或者你规定列名为“***”)的显示为粗体,背景为蓝色,其他均为默认显示。
然后,你删除数据结构中第二列数据,重新设置(tableModel.setDataVector(row , col);)
tableModel会重排表信息,并按照你所规定的要求显示数据
private JTable table_ = new JTable();
private ButtonPanel buttonPanel_ = new ButtonPanel(); public Panel() {
JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().add(table_);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(buttonPanel_, BorderLayout.SOUTH);
setModel();
} private void setModel() {
Vector colums = new Vector();
colums.add("Colum1");
colums.add("Colum2");
MyTableModel tableModel = new MyTableModel(colums);
table_.setModel(tableModel);
}
private void addCol(String newColName) {
( (MyTableModel) table_.getModel()).addCol(newColName);
} private void removeCol() {
( (MyTableModel) table_.getModel()).removeCol();
} private class ButtonPanel extends JPanel {
private JButton addBtn_ = new JButton("Add Colum");
private JButton removeBtn_ = new JButton("Remove Colum"); ButtonPanel() {
this.setLayout(new FlowLayout());
ActionListener listener = new BtnListener();
this.add(addBtn_);
this.add(removeBtn_); addBtn_.addActionListener(listener);
removeBtn_.addActionListener(listener);
} class BtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addBtn_)
addCol("Test");
else if (e.getSource() == removeBtn_)
removeCol();
}
}
} private class MyTableModel
extends AbstractTableModel {
Vector value_ = null; public MyTableModel(Vector value) {
value_ = value;
} public void refresh() {
fireTableDataChanged();
} public int getColumnCount() {
return value_.size();
} public int getRowCount() {
return 0;
} public String getColumnName(int col) {
return (String) value_.get(col);
} public Object getValueAt(int row, int col) {
return null;
} public boolean isCellEditable(int row, int col) {
return true;
} public void setValueAt(Object value, int row, int col) {
} public void removeCol() {
int lastElement = value_.size()-1;
if(lastElement < 0) return;
value_.remove(lastElement);
fireTableStructureChanged();
} public void addCol(String newColName) {
value_.add(newColName);
fireTableStructureChanged();
} }}