java怎么实现表格的行删除

用java怎么实现jtable表格的行删除... 用java怎么实现jtable表格的行删除 展开
 我来答
pieryon
推荐于2017-10-14 · 知道合伙人数码行家
pieryon
知道合伙人数码行家
采纳数:14410 获赞数:166869
获取软件设计师高级职称 万达金融最佳创新奖

向TA提问 私信TA
展开全部
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);
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-12-29
展开全部
public class Table extends JFrame {
private JPanel jContentPane = null;
private JScrollPane jScrollPane = null;
private JTable viewtable = null;
private JButton jButton = null;
public JCheckBox checkbox = new JCheckBox("取消");
DefaultTableModel dtm = null;
String[] columnNames = { "第一列", "第二列", "第三列", "取消"};
Object[][] data = {
{ "第一列", "第二列", "第三列",checkbox },
{ "第一列", "第二列", "第三列",checkbox },
{ "第一列", "第二列", "第三列", checkbox },
{ "第一列", "第二列", "第三列", checkbox },
{ "第一列", "第二列", "第三列", checkbox } }; private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new java.awt.Rectangle(40,40,650,120));
jScrollPane.setViewportView(getViewtable());
}
return jScrollPane;
} private JTable getViewtable() {
checkbox.addActionListener(listener);
if (viewtable == null) {
viewtable = new JTable();
dtm=new DefaultTableModel(data,columnNames);
viewtable=new JTable(dtm);
viewtable.getColumn("取消").setCellRenderer(new ButtonRenderer());
viewtable.getColumn("取消").setCellEditor(
new DefaultCellEditor(checkbox));
}
return viewtable;
}

ActionListener listener = new
ActionListener() {
public void actionPerformed(ActionEvent event) {
if (checkbox.isSelected()){
dtm.removeRow(viewtable.getSelectedRow());
checkbox.setSelected(false);
}
}
};
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("删除");
jButton.setBounds(new java.awt.Rectangle(289,180,120,28));
jButton.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int numrow=viewtable.getSelectedRows().length;
for (int i=0;i<numrow;i++){
dtm.removeRow(viewtable.getSelectedRow());
}
}
});
}
return jButton;
} public static void main(String[] args) {
Table table = new Table();
table.setVisible(true);
// TODO Auto-generated method stub
} public Table() {
super();
initialize();
}

private void initialize() {
this.setSize(772, 300);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
} private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
} class ButtonRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (value == null)
return null;
return (Component) value;
}
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式