Java:删除DefaultTableModel的所有行

Java:删除DefaultTableModel的所有行,第1张

Java:删除DefaultTableModel的所有行

为什么要将简单的事情复杂化而将其删除,则必须反复进行,

if (myTableModel.getRowCount() > 0) {    for (int i = myTableModel.getRowCount() - 1; i > -1; i--) {        myTableModel.removeRow(i);    }}

代码示例

import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;import javax.swing.table.*;public class RemoveAddRows extends Jframe {    private static final long serialVersionUID = 1L;    private Object[] columnNames = {"Type", "Company", "Shares", "Price"};    private Object[][] data = {        {"Buy", "IBM", new Integer(1000), new Double(80.50)},        {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)},        {"Sell", "Apple", new Integer(3000), new Double(7.35)},        {"Buy", "Nortel", new Integer(4000), new Double(20.00)}    };    private JTable table;    private DefaultTableModel model;    public RemoveAddRows() {        model = new DefaultTableModel(data, columnNames) { private static final long serialVersionUID = 1L; @Override public Class getColumnClass(int column) {     return getValueAt(0, column).getClass(); }        };        table = new JTable(model) { private static final long serialVersionUID = 1L; @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {     Component c = super.prepareRenderer(renderer, row, column);     int firstRow = 0;     int lastRow = table.getRowCount() - 1;     int width = 0;     if (row == lastRow) {         ((JComponent) c).setBackground(Color.red);     } else if (row == firstRow) {         ((JComponent) c).setBackground(Color.blue);     } else {         ((JComponent) c).setBackground(table.getBackground());     }          return c; }        };        table.setPreferredScrollableViewportSize(table.getPreferredSize());        JScrollPane scrollPane = new JScrollPane(table);        add(scrollPane);        JButton button1 = new JButton("Remove all rows");        button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {     if (model.getRowCount() > 0) {         for (int i = model.getRowCount() - 1; i > -1; i--) {  model.removeRow(i);         }     }     System.out.println("model.getRowCount() --->" + model.getRowCount()); }        });        JButton button2 = new JButton("Add new rows");        button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {     Object[] data0 = {"Buy", "IBM", new Integer(1000), new Double(80.50)};     model.addRow(data0);     Object[] data1 = {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)};     model.addRow(data1);     Object[] data2 = {"Sell", "Apple", new Integer(3000), new Double(7.35)};     model.addRow(data2);     Object[] data3 = {"Buy", "Nortel", new Integer(4000), new Double(20.00)};     model.addRow(data3);     System.out.println("model.getRowCount() --->" + model.getRowCount()); }        });        JPanel southPanel = new JPanel();        southPanel.add(button1);        southPanel.add(button2);        add(southPanel, BorderLayout.SOUTH);    }    public static void main(String[] args) {        RemoveAddRows frame = new RemoveAddRows();        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);        frame.pack();        frame.setLocationRelativeTo(null);        frame.setVisible(true);    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5477988.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存