2014-08-05 3 views
1

обновленный вопрос, конкретная проблема заключается в том, что данные не показываются, так как там нет колонн:DefaultTableModel имеет нулевые столбцы

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 
[email protected]:~/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("[email protected]"); 
     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

В вашем методе setColumns, вы на самом деле не добавляя ничего ...

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

Попробуйте добавить что-то к Vector, который представляет имя столбца acutal

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

Table with columns

+0

хех, спасибо, очень глупо. – Thufir

1

Проблема в том, что столбцы были пустыми в вашем вызове defaultTableModel.setDataVector(setData(), setColumns());

После замены вашего метода setColumns на коде ниже, данные были видны:

private Vector setColumns() { 
    Vector v = new Vector(); 
    v.add("Col1"); 
    v.add("Col2"); 
    v.add("Col3"); 
    return v; 
} 
Смежные вопросы