java怎么实现表格的行删除

java怎么实现表格的行删除,第1张

java中表格的删除是通过事件监控来实现的,示例代码如下:

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.MouseAdapter

import java.awt.event.MouseEvent

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JPanel

import javax.swing.JScrollPane

import javax.swing.JTable

import javax.swing.JTextField

import javax.swing.ListSelectionModel

import javax.swing.table.DefaultTableModel

//维护表格

public class JTableDefaultTableModelTest extends JFrame{

private DefaultTableModel tableModel //表格模型对象

private JTable table

private JTextField aTextField

private JTextField bTextField

public JTableDefaultTableModelTest()

{

super()

setTitle("表格")

setBounds(100,100,500,400)

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

String[] columnNames = {"A","B"} //列名

String [][]tableVales={{"A1","B1"},{"A2","B2"},{"A3","B3"},{"A4","B4"},{"A5","B5"}}//数据

tableModel = new DefaultTableModel(tableVales,columnNames)

table = new JTable(tableModel)

JScrollPane scrollPane = new JScrollPane(table) //支持滚动

getContentPane().add(scrollPane,BorderLayout.CENTER)

//jdk1.6

//排序:

//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)

final JPanel panel = new JPanel()

getContentPane().add(panel,BorderLayout.SOUTH)

panel.add(new JLabel("A: "))

aTextField = new JTextField("A4",10)

panel.add(aTextField)

panel.add(new JLabel("B: "))

bTextField = new JTextField("B4",10)

panel.add(bTextField)

final 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 //行数加上1

aTextField.setText("A"+rowCount)

bTextField.setText("B"+rowCount)

}

})

panel.add(addButton)

final JButton updateButton = new JButton("修改") //修改按钮

updateButton.addActionListener(new ActionListener(){//添加事件

public void actionPerformed(ActionEvent e){

int selectedRow = table.getSelectedRow()//获得选中行的索引

if(selectedRow!= -1) //是否存在选中行

{

//修改指定的值:

tableModel.setValueAt(aTextField.getText(), selectedRow, 0)

tableModel.setValueAt(bTextField.getText(), selectedRow, 1)

//table.setValueAt(arg0, arg1, arg2)

}

}

})

panel.add(updateButton)

final JButton delButton = new JButton("删除")

delButton.addActionListener(new ActionListener(){//添加事件

public void actionPerformed(ActionEvent e){

int selectedRow = table.getSelectedRow()//获得选中行的索引

if(selectedRow!=-1) //存在选中行

{

tableModel.removeRow(selectedRow) //删除行

}

}

})

panel.add(delButton)

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

JTableDefaultTableModelTest jTableDefaultTableModelTest = new JTableDefaultTableModelTest()

jTableDefaultTableModelTest.setVisible(true)

}

}

import java.util.*

import java.text.*

import java.io.*

import jxl.*

public class jxl

{

public static void main(String[] args)

{

String fileName = "c://signupcheckin.xls"

InputStream is = null

FileInputStream fn = null

int cs = 0

int rs = 0

try{

fn = new FileInputStream(fileName)

is = fn

Workbook rb = Workbook.getWorkbook(is)

Sheet s = rb.getSheet(0)//此处只读取第一个sheet内容

Cell a2=s.getCell(0,1) //此处是获取单个单元格的数据

Cell c4=s.getCell(2,3)

String sa2=a2.getContents()

String sc4=c4.getContents()

System.out.println(sa2+" "+sc4)

System.out.print("\n\n\n")

cs = s.getColumns()

rs = s.getRows()

for(int j=0j<rsj++){

Cell[] c = s.getRow(j)

for(int x=0x<c.lengthx++){

System.out.print(c[x].getContents()+"\t")

}

System.out.print("\r\n")

}

System.out.println("Columns:"+cs+" Rows:"+rs)

}catch(Exception e){

System.out.println(e.toString())

}finally{

try{

fn.close()

is.close()

}catch(Exception e){

System.out.println(e.toString())

}

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11651081.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存