表在JAVA中无法正常工作

时间:2014-05-23 11:11:20

标签: java jtable jcheckbox

您好我已经创建了一个Java程序,我希望在表运行时显示一些数据。

使用以下功能可以很好地显示数据,但我无法点击JTable数据,如复选框或文字。

感谢。

我的代码是:

    public void TableDataModel()
    {
        String[] cols = {"<html>Task<br>Name</html>", "<html>Start<br>Time</html>", "Finished"};

        DefaultTableModel model = new DefaultTableModel(data, cols) 
        {
            @Override
            public Class<?> getColumnClass(int col)
            {
                return col == 2 ? Boolean.class : String.class;
            }

            @Override
            public boolean isCellEditable(int row, int column) 
            { 
                return column==2 ? true : false;
            } 
        };

        JTable table = new JTable(model)
        {
            @Override
            public boolean isCellEditable(int row, int column) {
                return column==2 ? true : false;
            }
        };            

        table.setFont(new java.awt.Font("Times New Roman", 0, 14));
        table.getTableHeader().setFont(new java.awt.Font("Times New Roman", 0, 14));        
        table.getTableHeader().enable(false);

        JCheckBox jcheckBox=new JCheckBox();                
        TableColumnModel columnModel = table.getColumnModel();        
        columnModel.getColumn(2).setCellEditor(new DefaultCellEditor(jcheckBox));      

        jcheckBox.addChangeListener(new ChangeListener() 
        {
            @Override
            public void stateChanged(ChangeEvent e) 
            {
                int rowCount = model.getRowCount();
                for(int i=0; i<rowCount; i++) 
                {
                    Boolean selected = (Boolean)model.getValueAt(i, 2);
                    if(selected != null && selected) 
                    {
                        System.out.println("Removed:"+i);
                        model.removeRow(i);
                        i--;
                    }
                } 
            }
        });

        JScrollPane jScrollpane=new JScrollPane(table);      
        jScrollpane.setViewportView(table);
        this.add(jScrollpane);
        jScrollpane.setFont(new java.awt.Font("Times New Roman", 0, 14));        
        jScrollpane.setBounds(10, 130, 295, 190);          
    }

1 个答案:

答案 0 :(得分:0)

我认为这是改变监听器中的一个逻辑问题。

jcheckBox.addChangeListener(new ChangeListener() 
    {
        @Override
        public void stateChanged(ChangeEvent e) 
        {
            int rowCount = model.getRowCount();
            for(int i=0; i<rowCount; i++) 
            {
                Boolean selected = (Boolean)model.getValueAt(i, 2);
                if(selected != null && selected) 
                {
                    System.out.println("Removed:"+i);
                    model.removeRow(i);
                    i--;
                }
            } 
        }
    });

首先,您获取rowCount并遍历它并在内部删除行。这会更改行数。所以你需要在这一点上更新rowCount变量。尝试在rowCount = model.getRowCount();

之后添加i--;

<强>示例

import java.awt.GridLayout;

import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

public class SimpleTableDemo extends JPanel {

    public SimpleTableDemo() {
        super(new GridLayout(1, 0));
        TableDataModel();
    }

    public void TableDataModel() {
        String[] cols = { "<html>Task<br>Name</html>",
                "<html>Start<br>Time</html>", "Finished" };
        Object[][] data = {
                { "Kathy", "", new Boolean(false) },
                { "John", "", new Boolean(false) },
                { "Sue", "", new Boolean(false) },
                { "Jane", "", new Boolean(false) },
                { "Joe", "", new Boolean(false) } };

        DefaultTableModel model = new DefaultTableModel(data, cols) {
            @Override
            public Class<?> getColumnClass(int col) {
                return col == 2 ? Boolean.class : String.class;
            }

            @Override
            public boolean isCellEditable(int row, int column) {
                return column == 2 ? true : false;
            }
        };

        JTable table = new JTable(model) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return column == 2 ? true : false;
            }
        };

        table.setFont(new java.awt.Font("Times New Roman", 0, 14));
        table.getTableHeader().setFont(
                new java.awt.Font("Times New Roman", 0, 14));
        table.getTableHeader().enable(false);

        JCheckBox jcheckBox = new JCheckBox();
        TableColumnModel columnModel = table.getColumnModel();
        columnModel.getColumn(2)
                .setCellEditor(new DefaultCellEditor(jcheckBox));

        jcheckBox.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                int rowCount = model.getRowCount();
                System.out.println("*************" + rowCount);
                for (int i = 0; i < rowCount; i++) {
                    System.out.println(i);
                    Boolean selected = (Boolean) model.getValueAt(i, 2);
                    if (selected != null && selected) {
                        System.out.println("Removed:" + i);
                        model.removeRow(i);
                        i--;
                        rowCount = model.getRowCount();
                    }
                }
            }
        });

        JScrollPane jScrollpane = new JScrollPane(table);
        jScrollpane.setViewportView(table);
        this.add(jScrollpane);
        jScrollpane.setFont(new java.awt.Font("Times New Roman", 0, 14));
        jScrollpane.setBounds(10, 130, 295, 190);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("SimpleTableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SimpleTableDemo newContentPane = new SimpleTableDemo();
        newContentPane.setOpaque(true); // content panes must be opaque
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
    }

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