0

我想在我的 jTable 中添加一个链接。这个 jTable 有一些单元格填充了一个人的名字和姓氏。在第三个单元格中,我需要这个链接来从这个表中删除这个人。这可能吗?

现在我有这样的事情:

for(int i = 0; i < persons.size(); i++) {
    Person person = persons.get(i);
    jTabelRoster.setValueAt(lid.getPersonFirstname(), i, 0);
    jTabelRoster.setValueAt(lid.getPersonLastname(), i, 1);
}

在第三个水平位置我想要这个链接:)

这可能吗?

4

2 回答 2

4

查看Table Button Column以获得使用列中的按钮进行删除的解决方案。

于 2013-06-02T22:02:40.177 回答
2

您可以拥有CheckBox可以选择所需行的列deletion。下面的示例显示了一种方法。您可以选择一行,然后单击删除以删除该行。

我随身携带的示例代码。

行删除

TableExample.java(UI 类)

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;

public class TableExample {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                new TableExample().createUI();
            }
        };

        EventQueue.invokeLater(r);
    }

    private void createUI() {

        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());

        JButton addBtn = new JButton("Add");
        JButton delBtn = new JButton("Delete");
        JPanel buttonPnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPnl.add(addBtn);
        buttonPnl.add(delBtn);

        final StudentTableModel tableModel = new StudentTableModel();
        JTable table = new JTable();
        table.setModel(tableModel);
        table.getTableHeader().setReorderingAllowed(false);

        addBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                StudentDO do1 = new StudentDO();
                do1.setName("Student " + tableModel.getRowCount());
                do1.setAge(tableModel.getRowCount());
                tableModel.addRow(do1);
            }
        });

        delBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                tableModel.deleteRow();
            }
        });

        frame.add(table.getTableHeader(), BorderLayout.NORTH);
        frame.add(table, BorderLayout.CENTER);
        frame.add(buttonPnl, BorderLayout.SOUTH);

        frame.setTitle("JTable Example.");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

StudentTableModel.java(TableModel 类)

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class StudentTableModel extends AbstractTableModel {

    private static final long serialVersionUID = 1L;

    private List<StudentDO> data;
    private List<String> columnNames;

    public StudentTableModel() {
        data = getTableDataList();
        columnNames = getColumnNamesList();
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
        case 0:
            return Boolean.class;
        case 1: 
            return String.class;
        case 2: 
            return Integer.class;
        default:
            return String.class;
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex == 0 ? true : false;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames.get(column);
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
        case 0:
            return data.get(rowIndex).isSelect();
        case 1:
            return data.get(rowIndex).getName();
        case 2:
            return data.get(rowIndex).getAge();
        default:
            return null;
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        switch (columnIndex) {
        case 0:
            data.get(rowIndex).setSelect((Boolean) aValue);
            break;
        case 1:
            data.get(rowIndex).setName(aValue != null ? aValue.toString() : null);
            break;
        case 2:
            data.get(rowIndex).setAge(aValue != null ? Integer.parseInt(aValue.toString()) : null);
            break;
        default:
            break;
        }
    }

    public void addRow(StudentDO do1) {
        data.add(do1);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void deleteRow() {
        for(int rowIndex = data.size() - 1; rowIndex >= 0; rowIndex--) {
            if(data.get(rowIndex).isSelect()) {
                data.remove(rowIndex);
            }
        }

        fireTableDataChanged();
    }

    private List<StudentDO> getTableDataList() {

        List<StudentDO> list = new ArrayList<StudentDO>();

        StudentDO do1 = null;
        for(int i = 0; i < 5; i++) {

            do1 = new StudentDO();
            do1.setSelect(false);
            do1.setName("Student " + i);
            do1.setAge(i);

            list.add(do1);
        }

        return list;
    }

    private List<String> getColumnNamesList() {
        List<String> names = new ArrayList<String>();

        names.add("Select");
        names.add("Name");
        names.add("Age");

        return names;
    }
}

StudentDO.java(学生班)

public class StudentDO {

    private boolean select;
    private String name;
    private int age;

        // Add getter's and setter's
}
于 2013-06-03T07:33:22.463 回答