利用AbstractTableModel创建自己的table

包括2个java文件:  TableTEST.java 和 MyTableModel.java.

 

//  1. TableTEST.java ///

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;

import java.util.*;

public class TableTEST extends JFrame {

 // 数据源 String tableHeads[] = { "A", "B", "C", "D", "E" };

 String tableContent[][] = { { "abc", "def", "ghi", "jkl" },
   { "pqr", "stu", "vwx", "yza" },
   { "efg", "hij", "klm", "nop" },
   { "tuv", "wxy", "zab", "cde" },
   { "ijk", "lmn", "opq", "rst" } };

 JTextField txtboxRow = new JTextField(10);

 JTextField txtboxCol = new JTextField(10);

 JTextField txtboxContent = new JTextField(10);

 JPanel pnlSouth = new JPanel(new GridLayout(1, 6));

 MyTableModel tbModel = new MyTableModel();

 // 生成模型类对象

 public TableTEST() { // 初始化窗体 super("DO A Test!");

  this.setBounds(50, 50, 500, 400);

  // 获取contentPane

  Container contentPane = getContentPane();

  contentPane.setLayout(new BorderLayout());

  // 初始化表格数据及表头

  tbModel.data = new Vector(1, 1);

  for (int i = 0; i < 5; i++)

   for (int j = 0; j < 4; j++)

   {

    tbModel.data.add(tableContent[i][j]);

   }

  tbModel.titles = new Vector(1, 1);

  for (int i = 0; i < 5; i++)

  {

   tbModel.titles.add(tableHeads[i]);

  } // 使用表模型对象生成表

  JTable myTable = new JTable(tbModel);

  // 窗体组件布局

  myTable.setAutoResizeMode(5);

  JScrollPane jspCenter = new JScrollPane(myTable);

  contentPane.add(jspCenter, BorderLayout.CENTER);

  pnlSouth.add(new JLabel("Row:"), BorderLayout.SOUTH);

  pnlSouth.add(txtboxRow, BorderLayout.SOUTH);

  pnlSouth.add(new JLabel("Col:"), BorderLayout.SOUTH);

  pnlSouth.add(txtboxCol, BorderLayout.SOUTH);

  pnlSouth.add(new JLabel("Content:"), BorderLayout.SOUTH);
  pnlSouth.add(txtboxContent, BorderLayout.SOUTH);

  contentPane.add(pnlSouth, BorderLayout.SOUTH);

  // 为表格添加监听器

  myTable.addMouseListener(new MouseAdapter()

  {

   public void mouseClicked(MouseEvent e)

   {
    if (e.getClickCount() == 2)

    // 实现双击
    {

     int row = ((JTable) e.getSource()).rowAtPoint(e.getPoint()); // 获得行位置

     int col = ((JTable) e.getSource()).columnAtPoint(e
       .getPoint()); // 获得列位置
     String cellVal = (String) (tbModel.getValueAt(row, col)); // 获得点击单元格数据
     txtboxRow.setText((row + 1) + "");
     txtboxCol.setText((col + 1) + "");

     txtboxContent.setText(cellVal);

    } else
     return;
   }
  });

  this.addWindowListener(new WindowAdapter() {

   public void windowClosing(WindowEvent me) {
    System.exit(0);

   }
  });

  this.setVisible(true);

 }

 public static void main(String Str[]) {

  TableTEST frm = new TableTEST();

 }

 }

 

//  2. MyTableModel.java ///

 

import java.util.Vector;

import javax.swing.table.AbstractTableModel;

class MyTableModel extends AbstractTableModel {

public Vector data;


public Vector titles;


public int getRowCount() {


return data.size()/getColumnCount();


}


public int getColumnCount() {


return titles.size();

}


public Object getValueAt(int rowIndex, int columnIndex) {

return data.get((rowIndex*getColumnCount())+columnIndex);

}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
使用 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.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值