Swing图形界面学习_TableModel

一、在用JTable构造方法创建一个JTable时如果没有指定TableModel,那么会构造方法内部默认创建一个DefalutTableModel

        使用table的getModel()方法可以获得一个TableModel对象,需要指出的是TableModel是一个接口,不能直接使用。

       TableModel有两个子类AbstractTableModel和DefaultTableModel

二、每一个table对象都会使用一个TableModel对象来管理表中的数据

三、DefaultTableModel有一个方法addRow有两种重载形式,用于向表中追加一行数据

addRow(Vector rowData)

addRow(Object[] rowData)

需要说明的是在使用此方法时要注意JTable自身的TableModel必须和用addRow方法的DefaultTableModel引用一致

一個使用DefaultTableModel的例子

package test;

import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import java.awt.BorderLayout;

import java.util.Vector;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableColumn;

public class AddRemoveCells implements ActionListener{
 JTable table = null;
 DefaultTableModel defaultModel = null;
 public AddRemoveCells(){
  JFrame frame = new JFrame();
  String[] name = {"字段1","字段2","字段3","字段4","字段5"};
  String[][] data = new String[5][5];
  int value = 1;
  for(int i=0;i<data.length;i++){
   for(int j=0;j<data[i].length;j++){
    data[i][j] = String.valueOf(value++);
   }
  }
  defaultModel = new DefaultTableModel(data,name);//加入數據和字段名構造tablemodel
  table = new JTable(defaultModel);//設置表格模式為DefaultTableModel
  table.setPreferredScrollableViewportSize(new Dimension(400,80));
  JScrollPane s = new JScrollPane(table);

  JPanel panel = new JPanel();
  JButton b = new JButton("增加列");
  b.addActionListener(this);
  panel.add(b);
  b = new JButton("增加行");
  b.addActionListener(this);
  panel.add(b);
  b = new JButton("刪除列");
  b.addActionListener(this);
  panel.add(b);
  b = new JButton("刪除行");
  b.addActionListener(this);
  panel.add(b);
  
  Container contentPane = frame.getContentPane();
  contentPane.add(panel,BorderLayout.NORTH);
  contentPane.add(s,BorderLayout.SOUTH);

  frame.setTitle("AddRemoveCells");
  frame.pack();
  frame.setVisible(true);

  frame.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e){
    System.exit(0);
   }
  });
 }
 public void actionPerformed(ActionEvent e){
  if(e.getActionCommand().equals("增加列")){
   defaultModel.addColumn("增加列");
  }
  if(e.getActionCommand().equals("增加行")){
   defaultModel.addRow(new Vector());
  }
  if(e.getActionCommand().equals("刪除列")){//columnCount>=0說明表格中還有列,此時還可以繼續操作。
   int columnCount = defaultModel.getColumnCount()-1;
   if(columnCount>=0){
    TableColumnModel columnModel = table.getColumnModel();
    //拿到要刪除的列
    TableColumn tableColumn = columnModel.getColumn(columnCount);
    columnModel.removeColumn(tableColumn);
    //刪除完畢后設置列數
    defaultModel.setColumnCount(columnCount);
   }
   //相反,如果columnCount小于0,則表示此時表個已經沒有列,就不能再刪了,因此我們什么也不做
  }
  if(e.getActionCommand().equals("刪除行")){
   //使用getRowCount()方法可以返回行數
   int rowCount = defaultModel.getRowCount()-1;
   if(rowCount>=0){
    //刪除當前最后一行
    defaultModel.removeRow(rowCount);
    //設置當前表格的行數
    defaultModel.setRowCount(rowCount);
   }
  }
  table.revalidate();
 }
 public static void main(String[] args){
  new AddRemoveCells();
 }
}

 

使用 AbstractTableModel 构建Table 在表格中添加JButton按钮,之前在网上找了2天没有找到好用的程序,最终终于找到一个好用的例子。 不要使,我退你们分。。 sing the Swing JTable class can quickly become a sticky business when you want to customize it to your specific needs. First you must become familiar with how the JTable class is organized. Individual cells are rendered by TableCellRenderer implementations. The table contents are represented by an implementation of the TableModel interface. By default, JTable uses DefaultTableCellRenderer to draw its cells. DefaultTableCellRenderer recognizes a few primitive types, rendering them as strings, and can even display Boolean types as checkboxes. But it defaults to displaying the value returned by toString() for types it does not specifically handle. You have to provide your own TableCellRenderer implementation if you want to display buttons in a JTable. The TableCellRenderer interface contains only one method, getTableCellRendererComponent(...), which returns a java.awt.Component that knows how to draw the contents of a specific cell. Usually, getTableCellRendererComponent() will return the same component for every cell of a column, to avoid the unnecessary use of extra memory. But when the contents of a cell is itself a component, it is all right to return that component as the renderer. Therefore, the first step towards having JButtons display correctly in a JTable is to create a TableCellRenderer implementation that returns the JButton contained in the cell being rendered. In the accompanying code listing, JTableButtonRenderer demonstrates how to do this. Even after creating a custom TableCellRenderer, you're still not done. The TableModel associated with a given JTable does not only keep track of the contents of each cell, but it also keeps track of the class of data stored in each column. DefaultTableModel is designed to work with DefaultTableCellRenderer and will return java.lang.String.class for columns containing data types that it does not specifically handle. The exact method that does this is getColumnClass(int column). Your second step is to create a TableModel implementation that returns JButton.class for cells that contain JButtons. JTableButtonModel shows one way to do this. It just returns the result of getClass() for each piece of cell data. At this point, you're almost done, but not quite. What's the use of putting a JButton in a JTable if you can't press the darn thing? By default, JTable will not forward mouse events to components contained in its cells. If you want to be able to press the buttons you add to JTable, you have to create your own MouseListener that forwards events to the JButton cells. JTableButtonMouseListener demonstrates how you could do this.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值