jTable开头的复选框

Checkbox in the beginning of jTable

我是 Swing 新手,想在这里寻求帮助。我需要在 jTable 中显示 .xls 文件中的数据。下面是我从 here 遵循的代码:-

jbClick.addActionListener(new ActionListener() { 

    public void actionPerformed(ActionEvent arg0) { 
        jChooser.showOpenDialog(null); 
        File file = jChooser.getSelectedFile(); 
        if(!file.getName().endsWith("xls")){ 
            JOptionPane.showMessageDialog(null, "Please select only Excel file.", "Error",JOptionPane.ERROR_MESSAGE);
            } 
        else { 
                fillData(file); 
                model = new DefaultTableModel(data, headers); 
                tableWidth = model.getColumnCount() * 150; 
                tableHeight = model.getRowCount() * 25;
                table.setPreferredSize(new Dimension( tableWidth, tableHeight));
                table.setModel(model); 
                jbClick.setVisible(false);
                jbText.setVisible(true);
                } 
        } 
    }); 

    JPanel chooserPanel = new JPanel(); 
    JPanel filterPanlel = new JPanel();
    //filterPanlel.add(jbText,"OLa");

    final Color alternate = new Color(186,246,244);
    table = new JTable(); 
    table.setAutoCreateRowSorter(true); 
    model = new DefaultTableModel(data, headers); 
    table.setModel(model); 
    table.setBackground(alternate); 
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
    table.setEnabled(false); 
    table.setRowHeight(25); 
    table.setRowMargin(4); 
    tableWidth = model.getColumnCount() * 200;
    tableHeight = model.getRowCount() * 25;
    table.setPreferredSize(new Dimension( tableWidth, tableHeight)); 
    scroll = new JScrollPane(table); 
    scroll.setBackground(alternate); 
    scroll.setPreferredSize(new Dimension(500, 500)); 
    scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
    scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
    getContentPane().add(buttonPanel, BorderLayout.NORTH); 
    getContentPane().add(scroll, BorderLayout.CENTER); 
    //setSize(600, 600); 
    setResizable(true); 
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    //setUndecorated(true);
    setVisible(true); 
    } 


    /** * Fill JTable with Excel file data. * * @param file * file :contains xls file to display in jTable */ 
    @SuppressWarnings({ "unchecked", "rawtypes" })
    void fillData(File file) { 
        Workbook workbook = null; 
        try { 
            try { 
                workbook = Workbook.getWorkbook(file); 
                } 
            catch (IOException ex) { 
                Logger.getLogger( excelTojTable.class. getName()).log(Level.SEVERE, null, ex); 
                } 
            Sheet sheet = workbook.getSheet(0);
            headers.clear(); 
            for (int i = 0; i < sheet.getColumns(); i++) 
            { 
                Cell cell1 = sheet.getCell(i, 0);
                headers.add(cell1.getContents()); 
            } 
            data.clear(); 
            for (int j = 1; j < sheet.getRows(); j++) 
            { 
                Vector d = new Vector(); 
                for (int i = 0; i < sheet.getColumns(); i++) 
                { 
                    Cell cell = sheet.getCell(i, j); 
                    d.add(cell.getContents()); 
                } 
                d.add("\n");
                data.add(d); 
                } 
            } 
        catch (BiffException e) { 
            e.printStackTrace(); 
            } 
        } 

问题 :- 现在我需要在 table.

中的每一行前面添加一个 editable 复选框

请帮忙。

I need to add a editable checkbox in-front of each row in the table.

在创建 "header" 和 "d" 向量的循环之前,您需要添加布尔信息。

所以对于页眉,您可以这样做:

headers.clear(); 
header.add("Select"); 

每行:

Vector d = new Vector(); 
d.add( Boolean.FALSE );. 

i got a new column in my table but its not a checkbox

然后您需要通过使用布尔值 [=36] 的默认渲染器更新 TableColumnModelTableColumn 来为 table 的第一列设置默认渲染器=]:

TableColumn tc = table.getColumnModel().getColumn(0);
tc.setCellRenderer( table.getDefaultRenderer( Boolean.class ) );

i am new to swing

从 [如何使用 Table] 的 Swing 教程开始 (http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) 渲染器的基础知识。

编辑:

演示解决方案的简单 SSCCE:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.table.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        Vector<String> header = new Vector<String>();
        header.add("Select");
        header.add("Column1");
        header.add("Column2");
        header.add("Column3");

        Vector<Vector<Object>> data = new Vector<Vector<Object>>();

        for (int row = 0; row < 5; row++)
        {
            Vector<Object> d = new Vector<Object>();
            d.add( Boolean.FALSE );

            for (int column = 0; column < 3; column++)
            {
                d.add(row + " : " + column);
            }

            data.add(d);
        }

        DefaultTableModel model = new DefaultTableModel(data, header);
        JTable table = new JTable( model );
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        TableColumn tc = table.getColumnModel().getColumn(0);
        tc.setCellRenderer( table.getDefaultRenderer( Boolean.class ) );
        tc.setCellEditor( table.getDefaultEditor( Boolean.class ) );

        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new SSCCE() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}