找传奇、传世资源到传世资源站!

java swing 表格(JTable)动态添加、删除行 示例源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈


from clipboard// -*- mode:java; encoding:utf-8 -*-// vim:set fileencoding=utf-8:// @homepage@package example;import java.awt.*;import javax.swing.*;import javax.swing.table.DefaultTableModel;import javax.swing.table.TableCellRenderer;import javax.swing.table.TableColumn;public final class MainPanel extends JPanel { private MainPanel() { super(new BorderLayout()); RowDataModel model = new RowDataModel(); JTable table = new JTable(model) { private final Color evenColor = new Color(0xFA_FA_FA); @Override public Component prepareRenderer(TableCellRenderer tcr, int row, int column) { Component c = super.prepareRenderer(tcr, row, column); if (isRowSelected(row)) { c.setForeground(getSelectionForeground()); c.setBackground(getSelectionBackground()); } else { c.setForeground(getForeground()); c.setBackground(row % 2 == 0 ? evenColor : getBackground()); } return c; } }; TableColumn col = table.getColumnModel().getColumn(0); col.setMinWidth(60); col.setMaxWidth(60); col.setResizable(false); model.addRowData(new RowData("Name 1", "comment...")); model.addRowData(new RowData("Name 2", "Test")); model.addRowData(new RowData("Name d", "ee")); model.addRowData(new RowData("Name c", "Test cc")); model.addRowData(new RowData("Name b", "Test bb")); model.addRowData(new RowData("Name a", "ff")); model.addRowData(new RowData("Name 0", "Test aa")); table.setAutoCreateRowSorter(true); table.setFillsViewportHeight(true); table.setComponentPopupMenu(new TablePopupMenu()); add(new JScrollPane(table)); setPreferredSize(new Dimension(320, 240)); } public static void main(String... args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { createAndShowGui(); } }); } public static void createAndShowGui() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); Toolkit.getDefaultToolkit().beep(); } JFrame frame = new JFrame("@title@"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.getContentPane().add(new MainPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }}class RowDataModel extends DefaultTableModel { private static final ColumnContext[] COLUMN_ARRAY = { new ColumnContext("No.", Integer.class, false), new ColumnContext("Name", String.class, true), new ColumnContext("Comment", String.class, true) }; private int number; public void addRowData(RowData t) { Object[] obj = {number, t.getName(), t.getComment()}; super.addRow(obj); number ; } @Override public boolean isCellEditable(int row, int col) { return COLUMN_ARRAY[col].isEditable; } @Override public Class<?> getColumnClass(int column) { return COLUMN_ARRAY[column].columnClass; } @Override public int getColumnCount() { return COLUMN_ARRAY.length; } @Override public String getColumnName(int column) { return COLUMN_ARRAY[column].columnName; } private static class ColumnContext { public final String columnName; public final Class<?> columnClass; public final boolean isEditable; protected ColumnContext(String columnName, Class<?> columnClass, boolean isEditable) { this.columnName = columnName; this.columnClass = columnClass; this.isEditable = isEditable; } }}class RowData { private String name; private String comment; protected RowData(String name, String comment) { this.name = name; this.comment = comment; } public void setName(String str) { name = str; } public void setComment(String str) { comment = str; } public String getName() { return name; } public String getComment() { return comment; }}class TablePopupMenu extends JPopupMenu { private final JMenuItem delete; protected TablePopupMenu() { super(); add("add").addActionListener(e -> { JTable table = (JTable) getInvoker(); RowDataModel model = (RowDataModel) table.getModel(); model.addRowData(new RowData("New row", "")); Rectangle r = table.getCellRect(model.getRowCount() - 1, 0, true); table.scrollRectToVisible(r); }); addSeparator(); delete = add("delete"); delete.addActionListener(e -> { JTable table = (JTable) getInvoker(); DefaultTableModel model = (DefaultTableModel) table.getModel(); int[] selection = table.getSelectedRows(); for (int i = selection.length - 1; i >= 0; i--) { model.removeRow(table.convertRowIndexToModel(selection[i])); } }); } @Override public void show(Component c, int x, int y) { if (c instanceof JTable) { delete.setEnabled(((JTable) c).getSelectedRowCount() > 0); super.show(c, x, y); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复