DefaultTableModel具有零列

时间:2014-08-05 03:12:41

标签: java swing user-interface netbeans

更新了问题,具体问题是数据没有显示,因为没有列:

run:
     [java] Aug 04, 2014 8:17:00 PM net.bounceme.dur.client.gui.ApplicationDriver <init>
     [java] INFO: starting log..
     [java] Aug 04, 2014 8:17:01 PM net.bounceme.dur.client.gui.GraphUserInterface setData
     [java] INFO: [[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]
     [java] Aug 04, 2014 8:17:01 PM net.bounceme.dur.client.gui.GraphUserInterface <init>
     [java] INFO: rows  7
     [java] columns 0

BUILD SUCCESSFUL
Total time: 6 seconds
thufir@dur:~/NetBeansProjects/table$ 

所以我只是没有正确填充表格模型和表格:

package net.bounceme.dur.client.gui;

import java.util.Vector;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

public final class GraphUserInterface extends javax.swing.JFrame {

    private static final Logger log = Logger.getLogger(GraphUserInterface.class.getName());
    private DefaultTableModel defaultTableModel = new DefaultTableModel();
    private int columns = 3;
    private int rows = 7;

    private Vector setData() {
        Vector dataVector = new Vector();
        Vector r = null;
        for (int i = 0; i < rows; i++) {
            r = new Vector();
            for (int j = 0; j < columns; j++) {
                r.add(j);
            }
            dataVector.add(r);
        }
        log.info(dataVector.toString());
        return dataVector;
    }

    private Vector setColumns() {
        Vector v = new Vector();
        for (int i = 0; i < columns; i++) {
        }
        return v;
    }

    public GraphUserInterface() {
        initComponents();
        defaultTableModel.setDataVector(setData(), setColumns());
        defaultTableModel.setColumnIdentifiers(setColumns());
        table.setModel(defaultTableModel);
        defaultTableModel.fireTableDataChanged();
        defaultTableModel.fireTableStructureChanged();
        log.info("rows\t" + table.getRowCount()+ "\ncolumns\t"+ table.getColumnCount());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        buttonGroup = new javax.swing.ButtonGroup();
        mainPane = new javax.swing.JTabbedPane();
        update = new javax.swing.JPanel();
        tablePane = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        email = new javax.swing.JTextField();
        addRemove = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        table.setAutoCreateRowSorter(true);
        table.setModel(defaultTableModel);
        table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tablePane.setViewportView(table);

        email.setText("foo@bar.com");
        email.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emailActionPerformed(evt);
            }
        });

        addRemove.setText("toggle");

        javax.swing.GroupLayout updateLayout = new javax.swing.GroupLayout(update);
        update.setLayout(updateLayout);
        updateLayout.setHorizontalGroup(
            updateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(updateLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(updateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(updateLayout.createSequentialGroup()
                        .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 285, Short.MAX_VALUE)
                        .addComponent(addRemove, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(tablePane))
                .addContainerGap())
        );
        updateLayout.setVerticalGroup(
            updateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, updateLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(updateLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addRemove, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(tablePane, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
                .addContainerGap())
        );

        mainPane.addTab("update", update);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(mainPane, javax.swing.GroupLayout.PREFERRED_SIZE, 803, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainPane)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void emailActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:
    }                                     

    // Variables declaration - do not modify                     
    private javax.swing.JToggleButton addRemove;
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JTextField email;
    private javax.swing.JTabbedPane mainPane;
    private javax.swing.JTable table;
    private javax.swing.JScrollPane tablePane;
    private javax.swing.JPanel update;
    // End of variables declaration                   

}

据推测。

2 个答案:

答案 0 :(得分:2)

setColumns方法中,您实际上并没有添加任何内容......

private Vector setColumns() {
    Vector v = new Vector();
    for (int i = 0; i < columns; i++) {
        // Empty loop here...?
    }
    return v;
}

尝试向表示实际列名称的Vector添加内容

private Vector setColumns() {
    Vector v = new Vector();
    for (int i = 0; i < columns; i++) {
        v.add(Integer.toString(i));
    }
    return v;
}

Table with columns

答案 1 :(得分:1)

问题是您的电话defaultTableModel.setDataVector(setData(), setColumns());

中的列是空的

使用以下代码替换setColumns方法后,数据可见:

private Vector setColumns() {
    Vector v = new Vector();
    v.add("Col1");
    v.add("Col2");
    v.add("Col3");
    return v;
}