DefaultTableModel示例程序(Java)

这个Java程序展示了如何使用DefaultTableModel填充JTable,包括使用二维数组初始化和直接操作DefaultTableModel的方法。通过DefaultTableModel,可以进行添加、插入和删除行等操作,提供了更灵活的数据管理。
摘要由CSDN通过智能技术生成

The Java code below is a simple program used to show the different methods of a DefaultTableModel in action.

下面的Java代码是一个简单的程序,用于显示实际使用的DefaultTableModel的不同方法。

背景 ( Background )

The first JTable created uses a two-dimensional object array to populate the row data and a String array to populate the column names. The program shows that although you can get to the TableModel interface of the table model to get and set values for individual table cells created for this JTable , you cannot get to the DefaultTableModel in order to manipulate the data any further.

创建的第一个JTable使用二维对象数组填充行数据,并使用String数组填充列名。 该程序显示,尽管您可以进入表模型的TableModel接口以获取和设置为此JTable创建的单个表单元格的值,但是您无法进入DefaultTableModel以便进一步操作数据。

The second JTable is created by defining a DefaultTableModel with the data first. This allows the full range of actions by the table model to be performed on the JTable (e.g, adding a row, inserting a row, removing a row, adding a column, etc.).

通过首先定义一个DefaultTableModel数据来创建第二个JTable 。 这允许在JTable上执行表模型的所有动作(例如,添加行,插入行,删除行,添加列等)。

You might also be interested in the AbstractTableModel class. This class allows you to create a custom table model for a JTable where you can store the data in any way you like. It does not have to be in a Vector of Vectors.

您可能也对AbstractTableModel类感兴趣。 此类允许您为JTable创建自定义表模型,您可以在其中以自己喜欢的任何方式存储数据。 它不必位于VectorsVector中。

Java代码 ( Java Code )

Note: See DefaultTableModel Overview for some more information. 

注意:有关更多信息,请参见DefaultTableModel概述

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import javax.swing.table.DefaultTableModel;
public class TableExample {
public static void main(String[] args) {
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new TableExample().BuildGUI();
}
});
}
public void BuildGUI()
{
JFrame guiFrame = new JFrame();
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Creating a Table Example");
guiFrame.setSize(700,860);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
//Create a two dimensional array to hold the data for the JTable.
Object[][] data = {{1,1,1},{2,2,2},{3,3,3},{4,4,4}};
//A string array containing the column names for the JTable.
String[] columnNames = {"Column 1","Column 2","Column 3"};
//Create the JTable using the data array and column name array.
JTable exampleJTable = new JTable(data, columnNames);
//Create a JScrollPane to contain for the JTable
JScrollPane sp = new JScrollPane(exampleJTable);
//The JTable will provides methods which access the DefaultTabelModel.
//created when the JTable object was created
System.out.println(exampleJTable.getValueAt(2, 2));
//The DefaultTableModel can be acessed through the getModel method.
TableModel tabModel = exampleJTable.getModel();
//Provides the same output as the exampleJTable.getValueAt method call
//above.
System.out.println(tabModel.getValueAt(2, 2).toString());
//Note: We can't cast the TableMode returned from the getModel method
//to a DefaultTableModel object because it is implemented as an anonymous
//inner class in the JTable. So let's create a JTable with a DefaultTableModel
//we can use:
//Create a DeafultTableModel object for another JTable
DefaultTableModel defTableModel = new DefaultTableModel(data,columnNames);
JTable anotherJTable = new JTable(defTableModel);
//Create a JScrollPane to contain for the JTable
JScrollPane anotherSP = new JScrollPane(anotherJTable);
//an array holding data for a new column
Object[] newData = {1,2,3,4};
//Add a column
defTableModel.addColumn("Column 4", newData);
//an array holding data for a new row
Object[] newRowData = {5,5,5,5};
//Add a row
defTableModel.addRow(newRowData);
//an array holding data for a new row
Object[] insertRowData = {2.5,2.5,2.5,2.5};
//Insert a row
defTableModel.insertRow(2,insertRowData);
//Change a cell value
defTableModel.setValueAt(8888, 3, 2);
//Add the JScrollPanes to the JFrame.
guiFrame.add(sp, BorderLayout.NORTH);
guiFrame.add(anotherSP, BorderLayout.SOUTH);
guiFrame.setVisible(true);
}
}

翻译自: https://www.thoughtco.com/defaulttablemodel-example-program-2033893

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值