try { BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated; //UIManager.put("RootPane.setupButtonVisible", false); org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF(); } catch(Exception e) { //TODO exception } // 创建窗体对象 JFrame jFrame =new JFrame(); // 设置窗体大小 jFrame.setSize(800, 500); // 设置窗体全屏展示 //jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); // 设置窗体显示位置 //jFrame.setLocation(100,200); // 设置窗体显示正中间 jFrame.setLocationRelativeTo(null); // 设置窗体标题 jFrame.setTitle("窗体标题"); // 设置窗体不可全屏显示 //jFrame.setResizable(false); // 设置窗体关闭后退出程序 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置此窗口是否应该始终位于其他窗口上方 jFrame.setAlwaysOnTop(true); // 设置窗体图标 jFrame.setIconImage(new ImageIcon(HelloWorld.class.getResource("/images/book.png")).getImage()); JPanel jPanel =new JPanel(null); Object[][] data=new Object[][] { {new Boolean(false),"1","zhangshan","24","男"}, {new Boolean(false),"2","lisi","25","女"}, {new Boolean(false),"3","lisi","25","女"}, {new Boolean(false),"4","lisi","25","女"}, {new Boolean(false),"5","lisi","25","女"}, {new Boolean(false),"6","lisi","25","女"}, {new Boolean(false),"7","lisi","25","女"} }; Object[] column=new Object[] {"请选择","序号","姓名","年龄","性别"}; DefaultTableModel model =new DefaultTableModel(data,column) { // 重写getColumnClass方法的目的是为了在第一列添加复选框,如果不需要可以不重写 @Override public Class<?> getColumnClass(int columnIndex) { if(columnIndex==0) { return Boolean.class; }else { return Object.class; } } }; // 创建表格,并添加数据,并设置单元格不可编辑 JTable table =new JTable(model) { public boolean isCellEditable(int row,int column) { if(column==0) { return true; } return false; } }; // 设置Tbody的字体 table.setFont(new Font("微软雅黑",Font.PLAIN,15)); // 设置Tbody的td高度 table.setRowHeight(40); // 设置Thead的字体 table.getTableHeader().setFont(new Font("微软雅黑",Font.PLAIN,15)); // 设置某个列的宽度 table.getColumnModel().getColumn(2).setPreferredWidth(200); // 设置表格一次只能选中一行数据 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 添加数据 model.addRow(new Object[] {new Boolean(false),"8","JACK","22","男"}); // 数据回显 修改数据 table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { // 只监听变化事件 if(e.getValueIsAdjusting()==true) { // 获取被选中的行返回当前行号 int row =table.getSelectedRow(); // 获取每个单元格的值 String id=(String) table.getValueAt(row, 1); String name=(String) table.getValueAt(row, 2); String age=(String) table.getValueAt(row, 3); String gender=(String) table.getValueAt(row, 4); System.out.println(id +',' +name +","+age +"," +gender); // 修改数据 table.setValueAt(id, row, 1); table.setValueAt(name, row, 2); table.setValueAt(age, row, 3); table.setValueAt(gender, row, 4); } } }); // 删除数据 JButton deleteBtn =new JButton("delete"); deleteBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { boolean selecton =false; for(int i=0;i<model.getRowCount();i++) { if(model.getValueAt(i, 0).equals(Boolean.TRUE)) { selecton=true; } } if(selecton) { for(int i=model.getColumnCount()-1;i>=0;i--) { if(model.getValueAt(i, 0).equals(Boolean.TRUE)) { //删除数据 model.removeRow(i); } } }else { System.out.println("请选择"); } } }); JScrollPane scrollPane =new JScrollPane(table); scrollPane.setSize(800, 200); scrollPane.setLocation(10, 10); jPanel.add(scrollPane); jFrame.setContentPane(jPanel); // 设置窗体可见 jFrame.setVisible(true);
标签:jFrame,Java,Swing,JTable,窗体,table,new,false,row From: https://www.cnblogs.com/liangqingyun/p/18593793