public Object[][] showAll() { db = ds.getAll();//通过service-dao从数据库拿到数据 data = new Object[db.size()][Names.length]; for (int i = 0; i < db.size(); i++) { for (int j = 0; j < Names.length; j++) { data[i][0] = db.get(i).getName(); data[i][1] = db.get(i).getAccounts(); data[i][2] = db.get(i).getPwd(); data[i][3] = db.get(i).getId();//ID用于查询,不显示,使用removeColumn方法隐藏列 } } return data; } --------------------------------------------------------------------- /* * 查询所有数据显示到table */ dtm = new DefaultTableModel(showAll(), Names); DefaultTableCellRenderer r = new DefaultTableCellRenderer();// 设置文字居中显示 r.setHorizontalAlignment(JLabel.CENTER); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(25, 13, 705, 388); frame.getContentPane().add(scrollPane); table_1 = new JTable(); scrollPane.setViewportView(table_1); table_1.setFont(new Font("Adobe Caslon Pro", Font.PLAIN, 18)); table_1.setModel(dtm); TableColumnModel tcm = table_1.getColumnModel(); TableColumn tc = tcm.getColumn(3);//ID排在数组的第3位 table_1.removeColumn(tc);//隐藏某列 table_1.setDefaultRenderer(Object.class, r); table_1.setRowHeight(30);// 设置行高30像素
完成大概就这样了(自己做着练习的,过于简陋请无视)
如图,只显示了网站、帐号、密码,ID给隐藏了~