Java创建表格要利用JTable组件组件。
下面看看创建表格的具体步骤:
package cn.com;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class ArrayCreateTable extends JFrame
{
private JTable table;
public ArrayCreateTable()
{
init();
}
public void init()
{
//第一步:创建表格
this.table = new JTable();
//第二步:获取表格的数据模型
DefaultTableModel model = (DefaultTableModel) this.table.getModel();
//第三步:对表格的数据模型操作
model.setColumnIdentifiers(new String[] {"列1" , "列2" , "列3"});//设置表头
for(int i=0 ; i<10 ; i++) //增加行
model.addRow(new String[]{null , null , null});
//第四步:更新表格模型
this.table.setModel(model);
//第五步:将表格组件添加到JScrollPane面板上
JScrollPane cen_pan = new JScrollPane();
//第六步:为JScrollPane面板设置一个可视化图表
cen_pan.setViewportView(this.table);
this.add(cen_pan);
//设置窗口属性
this.setSize(500 , 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String args[])
{
new ArrayCreateTable();
}
}
下面再来看一个具体的例子:
package cn.com;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class ArrayCreateTable extends JFrame implements ActionListener
{
//private JFrame frame;
private JButton button1;
private JButton button2;
private JTable table;
private DefaultTableModel table_mode;
public ArrayCreateTable()
{
init();
}
@SuppressWarnings("deprecation")
public void init()
{
this.button1 = new JButton("增加行");
this.button2 = new JButton("删除行");
Object[][] p = {
{ "1", 91, 1949, 1910 },
{ "2", 82,1969, 1510 },
{ "3", 47, 1957, 1040 },
{ "4", 61, 1957, 1180 },
{ "5",90, 1987, 1770 }, };
String[] tab_title = { "姓名", "工号", "出生年月", "薪水" };
//1
this.table = new JTable();
//2
this.table_mode =(DefaultTableModel) this.table.getModel();
//3
this.table_mode.setColumnIdentifiers(tab_title); //设置表头
for (int i=0 ; i<tab_title.length ; i++) //向表格添加数据
table_mode.addRow(p[i]);
//4
this.table.setModel(table_mode); //更新表格
//5
JScrollPane scrollpane = new JScrollPane(this.table); //中部面板
//6
scrollpane.setViewportView(this.table);
this.add(scrollpane);
FlowLayout flowlayout = new FlowLayout();
flowlayout.setHgap(40); //设置组件间的间隔为40
JPanel nor_pan = new JPanel(flowlayout);
this.button1.setPreferredSize(new Dimension(80,30));
this.button2.setPreferredSize(new Dimension(80,30));
nor_pan.add(this.button1 );
nor_pan.add(this.button2);
this.add(nor_pan , BorderLayout.NORTH); //添加上部面板
//添加监视器
this.button1.addActionListener(this);
this.button2.addActionListener(this);
//设置窗口属性
this.setTitle("表格视窗");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("增加行"))
this.table_mode.addRow(new Vector<String>());
if (e.getActionCommand().equals("删除行"))
{
//获取选中行的行数
int numrow = 0;
numrow=this.table.getSelectedRows().length;
if (numrow == 0)
JOptionPane.showMessageDialog(null, "请选择您要删除的行",null , JOptionPane.WARNING_MESSAGE);
for (int i=0;i<numrow;i++)
{
//删除所选行;
this.table_mode.removeRow(this.table.getSelectedRow());
}
}
}
public static void main(String args[])
{
new ArrayCreateTable();
}
}
下面再来看看如何进行表格的删除和添加,这里以列为主:
这里就截取上面的代码片段:
//获取选中行的行数
int numrow = 0;
numrow=this.table.getSelectedRows().length;
if (numrow == 0)
JOptionPane.showMessageDialog(null, "请选择您要删除的行",null , JOptionPane.WARNING_MESSAGE);
for (int i=0;i<numrow;i++)
{
//删除所选行;
this.table_mode.removeRow(this.table.getSelectedRow());
}