带有可编辑复选框的JTable

编程入门 行业动态 更新时间:2024-10-28 13:25:57
本文介绍了带有可编辑复选框的JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面的代码是我的项目类之一。当我点击查找按钮时,会产生一个带有 JTable 的框架,一些数据将动态加载到表格中。表格的最后一列必须是带事件的复选框。 我试过这个代码用于复选框(从另一个项目中取出它......没有用)

The below code is one of my project class.It produce a frame with JTable when I click find button some data will load to table dynamically.The last column of table must be checkbox with event. I tried this code for checkbox (took it from another project..its not working)

DefaultTableModel dtm = new DefaultTableModel(data, colName){ public Class getColumnClass(int c) { return (c == 5? Boolean.class : String.class); } };

实际课程

public class BookReturnPanel { JPanel retunBookPanel; JTextField txtRegNo; JButton btnFind, btnSave; JTable retunTable = null; public JScrollPane jScrollPane = null; private int i; static Object[][] data; String regNo = null; Member member = null; DefaultTableModel model = new DefaultTableModel(); /** * Create the panel. */ public BookReturnPanel() { } public JPanel getRetunBookPanel() { if (retunBookPanel == null) { retunBookPanel = new JPanel(); retunBookPanel.setLayout(null); model.addColumn("Member"); model.addColumn("Book"); model.addColumn("Issue Date"); model.addColumn("Return Date"); model.addColumn("Return"); retunTable=new JTable(model); retunTable.setLocation(new Point(0,60)); retunTable.setSize(new Dimension(517, 386)); JLabel lblRegNo = new JLabel("Member Reg No:"); lblRegNo.setBounds(24, 40, 108, 14); retunBookPanel.add(lblRegNo); retunBookPanel.add(getJScrollPane(), BorderLayout.CENTER); txtRegNo = new JTextField(); txtRegNo.setBounds(129, 37, 200, 20); retunBookPanel.add(txtRegNo); txtRegNo.setColumns(10); btnFind = new JButton("Find"); btnFind.setBounds(359, 36, 91, 23); btnFind.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (model.getRowCount() > 0) { for (int i = model.getRowCount() - 1; i > -1; i--) { model.removeRow(i); } } regNo = txtRegNo.getText(); member = ServiceFactory.getMemberServiceImpl().findByregNo(regNo); List<Issue> issues = ServiceFactory.getIssueServiceImpl() .FindAllIssueByMemberId(member.getSerialNo()); for(Issue issue:issues){ Vector<Object>row=new Vector<Object>(); row.addElement( issue.getMemberId().getName()); row.addElement( issue.getBookId().getName()); row.addElement( issue.getIssueDate()); row.addElement( issue.getReturnDate()); row.addElement(issue.getStatus()); model.addRow(row); } } }); retunBookPanel.add(btnFind); btnSave = new JButton("Save"); btnSave.setBounds(425, 448, 91, 23); retunBookPanel.add(btnSave); } return retunBookPanel; } private JScrollPane getJScrollPane() { if (jScrollPane == null) { jScrollPane = new JScrollPane(); jScrollPane.setBounds(new Rectangle(0, 60, 517, 386)); jScrollPane.setViewportView(retunTable); } return jScrollPane; } }

以上代码生成 JTable 完美但我需要在最后一栏显示 JCheckbox 。 也需要在单击复选框时添加事件..!

The above code produce JTable perfectly but I need to show JCheckbox at the last column. also it need to add event when a checkbox is clicked..!

推荐答案

1) JTable 列索引从0开始,如果你想将 JCheckBox 设置为最后一列,你需要使用索引4而不是5代码。

1) JTable columns indexes starts from 0, if you want to set JCheckBox to last column, you need to use index 4 , not 5 as in your code.

2)为了向 JCheckBox 列添加操作,您可以使用 DefaultCellEditor 带 JCheckBox ,例如:

2)For adding action to your JCheckBox column you can use DefaultCellEditor with JCheckBox, for example:

JCheckBox chb = new JCheckBox(); chb.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { System.out.println("action"); } }); DefaultCellEditor editor = new DefaultCellEditor(chb) { @Override public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row,int column) { Component tableCellEditorComponent = super.getTableCellEditorComponent(table, value, isSelected, row, column); ((JCheckBox)tableCellEditorComponent).setHorizontalAlignment(JCheckBox.CENTER); return tableCellEditorComponent; } }; retunTable.getColumnModel().getColumn(4).setCellEditor(editor);

更多推荐

带有可编辑复选框的JTable

本文发布于:2023-05-31 22:04:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/400203.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   编辑   JTable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!