表格的增删改

public class ToolTipList extends JFrame{
	JTextField aTextField;
	JTextField bTextField;
	public ToolTipList(){//构造方法
		 JScrollPane scrollPane=new JScrollPane();
		 getContentPane().add(scrollPane, BorderLayout.CENTER);
		 String[] columnNames={"A","B"};
		 String[][] tableValues={{"A1","B1"},{"A2","B2"},{"A3","B3"}};
		 DefaultTableModel tableModel=new DefaultTableModel(tableValues, columnNames);
		 JTable table=new JTable(tableModel);
		 table.setRowSorter(new TableRowSorter(tableModel));//设置排序起
		 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		 table.addMouseListener(new MouseAdapter(){
			 public void mouseClicked(MouseEvent e){
				 int selectedRow=table.getSelectedRow();
				 Object oa=tableModel.getValueAt(selectedRow, 0);
				 Object ob=tableModel.getValueAt(selectedRow, 1);
				 aTextField.setText(oa.toString());
				 bTextField.setText(ob.toString());
			 }
		 });
		 scrollPane.setViewportView(table);//表格放到滚动条面板
		 JPanel p=new JPanel();
		 getContentPane().add(p,BorderLayout.SOUTH);
		 p.add(new JLabel("A: "));
		 aTextField=new JTextField("A4:",10);
		 p.add(aTextField);
		 bTextField=new JTextField("B4",10);
		 p.add(new JLabel("B: "));
		 p.add(bTextField);
		 JButton addButton=new JButton("添加");
		 addButton.addActionListener(new ActionListener(){
			 public void actionPerformed(ActionEvent e){
				 String[] rowValues={aTextField.getText(),bTextField.getText()};
				 //想表格中添加一行
				 tableModel.addRow(rowValues);
				 int rowCount=table.getRowCount()+1;
				 aTextField.setText("A"+rowCount);;
				 bTextField.setText("B"+rowCount);
			 }
		 });
		 p.add(addButton);
		 JButton updButton=new JButton("修改");
		 updButton.addActionListener(new ActionListener(){
			 public void actionPerformed(ActionEvent e){
				 int selectedRow=table.getSelectedRow();
				 if(selectedRow!=-1){
					 tableModel.setValueAt(aTextField.getText(),selectedRow,0);
					 tableModel.setValueAt(aTextField.getText(),selectedRow,1);
					 
				 }
			 }
		 });
		 p.add(updButton);
		 JButton delButton=new JButton("删除");
		 delButton.addActionListener(new ActionListener(){
			 public void actionPerformed(ActionEvent  e){
				int selectedRow=table.getSelectedRow();
				if(selectedRow!=-1){
					tableModel.removeRow(selectedRow);;
				}
			 }
		 });
		 p.add(delButton);
		 setSize(600, 300);
		 setVisible(true);
		 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
	}
	public static void main(String[] args) {
		 new ToolTipList();
		 
	}

}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值