编程语言
首页 > 编程语言> > Java带选择框的JTable

Java带选择框的JTable

作者:互联网

1.先上效果图:

 2.代码如下:

public static void main(String[] args) {
        
        final JFrame jf = new JFrame("请确认"); // 创建窗口
        jf.setSize(705, 450);
        jf.setLocationRelativeTo(null); // 把窗口位置设置到屏幕中心
        jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); // 当点击窗口的关闭按钮时退出程序(没有这一句,程序不会退出)
        jf.setResizable(false);
        
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch (Exception e1) {
            e1.printStackTrace();
        }
        
        JPanel jp = new JPanel();
        
        JScrollPane jscrollpane = new JScrollPane();
        
        final DefaultTableModel tableModel = new DefaultTableModel();
        
        tableModel.getDataVector().clear();    //清除tableModel
        
        final JTable table = new JTable(tableModel){
            private static final long serialVersionUID = 1L;
            public boolean isCellEditable(int row, int column){
                if (column != 4) {
                    return false;
                }
                return autoCreateColumnsFromModel;
            }
        };
        
        Object[] columnTitle = new Object[]{"列A", "列B", "列C", "列D", "是否导出"};//列名
        Object[][] rowData = new Object[30][];
        for(int i = 0; i < 30; i++){
            String[] str = new String[4];
            str[0] = i+"";
            str[1] = i*2+"";
            str[2] = i*3+"";
            str[3] = i*4+"";
            rowData[i] = new Object[]{str[0], str[1], str[2], str[3], Boolean.TRUE};
        }
        
        tableModel.setDataVector(rowData, columnTitle);
        table.setModel(tableModel);
        
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.getColumnModel().getColumn(0).setPreferredWidth(100); 
        table.getColumnModel().getColumn(1).setPreferredWidth(300); 
        table.getColumnModel().getColumn(2).setPreferredWidth(100); 
        table.getColumnModel().getColumn(3).setPreferredWidth(105); 
        
        table.getColumnModel().getColumn(4).setCellEditor(table.getDefaultEditor(Boolean.class));
        table.getColumnModel().getColumn(4).setCellRenderer(table.getDefaultRenderer(Boolean.class));
        
        DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
        cr.setHorizontalAlignment(JTextField.CENTER);
        table.getColumnModel().getColumn(0).setCellRenderer(cr);
        
        jscrollpane.setBounds(0, 0, 700, 330);
        jscrollpane.setViewportView(table);    //这句很重要
        
        JButton cancelButton = new JButton("取消");
        cancelButton.setBounds(165, 360, 80, 30);
        cancelButton.setFocusPainted(false);
        JButton okButton = new JButton("确定");
        okButton.setBounds(405, 360, 80, 30);
        okButton.setFocusPainted(false);
        
        jp.setLayout(null);
        // 将各个组件加入到JFrame
        jp.add(cancelButton);
        jp.add(okButton);
        
        jp.add(jscrollpane);
        jf.setContentPane(jp);
        
        //取消按钮监听
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jf.dispose();
            }
        });
        
        //确定按钮监听
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jf.dispose();
            }
        });
        
        jf.setVisible(true);
    }

 

标签:选择,getColumn,Java,getColumnModel,JTable,str,new,table,jf
来源: https://www.cnblogs.com/tan-cong/p/13755000.html