2

我是使用 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
}   

}

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

4

1 回答 1

2

类型可编辑列的默认单元格编辑器Boolean.classJCheckBox. 只有编辑器需要监听复选框,并且只有在编辑复选框时才需要监听。编辑结束后,检查TableModel更改。如果另一个组件需要了解此类更改,它应该注册为TableModelListener,如图所示。相关示例可在此处此处找到。

于 2013-08-13T00:09:07.573 回答