JTable

时间:2016-04-06 11:00:11

标签: java swing jtable jcheckbox

JTable Row Header captor 1

我在JTable行标题中遇到问题。 当我在同一列中选择两个不同的单元格时,将选择此列中它们之间的所有单元格。

            /**
         * column header vector
         */
        Vector<String> columnheaders = new Vector<String>();
        rs5=ajasp.consulterAspect();
        while (rs5.next()) {
            columnheaders.addElement(rs5.getString(2));
        }
        /**
         * row header vector
         */
        Vector<String> rowheaders = new Vector<String>();
        rs7=ajb.consulterBase();
        while (rs7.next()) {
            rowheaders.addElement(rs7.getString(2));
        }
        Vector<Object> enrg = new Vector<Object>();
        Vector<Vector> content = new Vector<Vector>();
        /**
         * fill in the jtable content
         */
        for (int i = 0; i < roheaders.size(); i++) {
            for (int j = 0; j < colheaders.size(); j++) {
                enrg2.addElement("");
            }
            content2.addElement(enrg2);
        }
        /**
         * Model for our row header
         */
        ListModel lm1= new AbstractListModel() {

            /**
             * Header text we want displayed
             */
            @Override
            public Object getElementAt(int index) {
                return rowheaders.elementAt(index);
            }

            @Override
            public int getSize() {
                return rowheaders.size();
            }

        };
        /**
         * Model for our column header
         */
        ListModel lm2= new AbstractListModel() {

            @Override
            public Object getElementAt(int index) {
                return columnheaders.elementAt(index);
            }

            @Override
            public int getSize() {
                return columnheaders.size();
            }

        };
        dm = new DefaultTableModel(lm1.getSize(),lm2.getSize());
        dm.setDataVector(content, columnheaders);
        /**
         * fill in the cell table with checkboxes
         */
        jtable = new JTable(dm){
             private static final long serialVersionUID = 1L;
                @Override
                public Class getColumnClass(int column) {
                    return Boolean.class;
                }
        };
        jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        /**
         * Create row header
         */
        JList rowHeader = new JList(lm1);
        rowHeader.setFixedCellWidth(75);
        rowHeader.setFixedCellHeight(jtable.getRowHeight());

        /**
         * Set renderer
         */
        rowHeader.setCellRenderer(new RowRenderer(jtable));

        /**
         * JScrollPane
         */
        JScrollPane scp = new JScrollPane(jtable);
        scp.setBounds(200, 100, 800, 300);
        scp.setRowHeaderView(rowHeader);
        panel_11.add(scp, BorderLayout.CENTER);

0 个答案:

没有答案