如何使用带有动态JTable的复选框监听器?

时间:2013-08-12 19:10:58

标签: java swing jtable tablecelleditor

我是使用Swing的新手,这就是为什么我要求很多;实际上,我正在寻找这个,但我找不到一个好的答案。我必须做一个动态表;它必须从XML文件中填充工作表的名称,并且还有另一个带有复选框的列,如果必须验证工作表,则会单击该复选框。

当我创建表时,它是空的。在执行其他操作后,它将填充数据。

在这种情况下,我需要知道如何使用复选框监听器。

这是主要代码,按钮打开执行填充。

public class CreateScenarioUI extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private JPanel contentPane;
private JTextField textField;
String filePath = null;
String[] sheetNames = null;



/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                CreateScenarioUI frame = new CreateScenarioUI();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public CreateScenarioUI() {


    setTitle("Scenario creation");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 450);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    final MainTable table = new MainTable(contentPane);

    /** Button Open */
    final JButton btnOpen = new JButton("Open");
    btnOpen.setFont(new Font("Tahoma", Font.PLAIN, 11));
    btnOpen.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            performBtnOpen();
        }

        private void performBtnOpen() {
            JFileChooser choose = new JFileChooser();
            choose.addChoosableFileFilter(new ExcelFilter());
            int returnVal = choose.showOpenDialog(null);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                textField.setText(choose.getSelectedFile().getName());
            }

            FileReader fileReader = new FileReader();
            System.out.println(choose.getSelectedFile());
            filePath = choose.getSelectedFile().toString();
            sheetNames = fileReader.getSheetNames(choose.getSelectedFile());
            table.getModel().addColumn("Sheet name", sheetNames);
            Boolean[] selectorColumn = new Boolean[sheetNames.length];
            for (int i = 0; i < sheetNames.length; i++) {
                selectorColumn[i] = new Boolean(false);
            }
            table.getModel().addColumn("Selector", selectorColumn);
        }
    });

    btnOpen.setBounds(320, 50, 100, 20);
    contentPane.add(btnOpen);

}}

以下是我创建MainTable

的方法
public class MainTable extends JFrame implements TableModelListener, ItemListener {

DefaultTableModel model = new DefaultTableModel();

@Override
public void tableChanged(TableModelEvent arg0) {
    // TODO Auto-generated method stub

}

public MainTable(JPanel contentPane){

    String[] setColumnIdentifiers = {};
    JTable table = new JTable(new Object[][] {}, new Object[] {
            "Sheet name", "Create" });
    JScrollPane scrollPane = new JScrollPane(table);

    /** Main Table */
    model = new DefaultTableModel(setColumnIdentifiers, 0) {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Override
        public Class getColumnClass(int columnIndex) {
            if (columnIndex == 1) {
                return Boolean.class;
            } else {
                return super.getColumnClass(columnIndex);
            }
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            boolean editable = true;
            if (column == 0) {
                editable = false;
                /**Object value = getValueAt(row, column);
                if (value instanceof Integer) {
                    editable = ((int)value) != 0;
                }*/
            }
            return editable;
        }
    };
    table.setModel(model);
    table.getModel().addTableModelListener(this);
    table.setFillsViewportHeight(true);
    table.setColumnSelectionAllowed(true);
    table.setBorder(new LineBorder(new Color(0, 0, 0)));
    table.setCellSelectionEnabled(true);
    table.setBounds(40, 100, 380, 190);
    scrollPane.setBounds(40, 100, 380, 190);
    scrollPane.setBorder(new LineBorder(new Color(0, 0, 0)));
    contentPane.add(scrollPane);

}

public DefaultTableModel getModel(){
    return model;
}

@Override
public void itemStateChanged(ItemEvent event) {
    //Object source = event.getItemSelectable();
    // TODO Auto-generated method stub
}   

}

任何帮助都会有用,谢谢大家!

1 个答案:

答案 0 :(得分:2)

Boolean.class类型的可修改列的default cell editorJCheckBox。在编辑复选框时,编辑器需要收听复选框,编辑器结束后,检查TableModel是否有更改。如果另一个组件需要了解此类更改,则应注册为TableModelListener,如图here所示。可以找到相关示例herehere