2013-01-10 55 views
2

我的目标是使用JTable显示来自数据库的数据。JTable不显示表头

引用代码:Most simple code to populate JTable from ResultSet

我修改,以适应我的情况的代码。我有一个类TopicData以下方法:

public DefaultTableModel buildTableModel(){ 
    //open connection 
    ResultSet rs = null; 
    Vector<String> columnNames = new Vector<String>(); 
    Vector<Vector<Object>> data = new Vector<Vector<Object>>(); 
    int columnCount = 0; 
    DBController db = new DBController(); 
    db.setUp("myDatabase"); 
    String dbQuery = "SELECT topicName, topicDate, topicCategory, topicUser FROM topicTable ORDER BY topicDate"; 

    rs = db.readRequest(dbQuery); 
    try{  
     ResultSetMetaData metaData = rs.getMetaData(); 

     // names of columns 
     columnCount = metaData.getColumnCount(); 
     for (int column = 1; column <= columnCount; column++) { 
      columnNames.add(metaData.getColumnName(column)); 

     } 
     // data of the table 
     while (rs.next()) { 
      Vector<Object> vector = new Vector<Object>(); 
      for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) { 
       vector.add(rs.getObject(columnIndex)); 
      } 
      data.add(vector); 
     } 
    } 
    catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    //close connection 
    db.terminate(); 

    return new DefaultTableModel(data, columnNames); 

} 

而在另一个类TopicView:

private JTable getTable() { 
    if (table == null) { 
     TopicData topic= new TopicData(); 
     table = new JTable(topic.buildTableModel()); 
     table.setShowGrid(false); 
     table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     table.setBounds(95, 59, 625, 357); 
    } 
    return table; 
} 

我意识到,我的表没有在每一列的顶部有头。 http://i.stack.imgur.com/FcnHb.png

所以我简化我的TopicData解决:

public DefaultTableModel testTableModel(){ 
    Vector<String> columnNames = new Vector<String>(); 
    columnNames.add("test1"); 
    columnNames.add("test1"); 
    columnNames.add("test1"); 
    Vector<Vector<Object>> data = new Vector<Vector<Object>>(); 
    Vector<Object>vector = new Vector<Object>(); 
    vector.add("test2"); 
    vector.add("test2"); 
    vector.add("test2"); 
    data.add(vector); 

    return new DefaultTableModel(data, columnNames); 
} 

再次,表没有头。单词“test1”不会出现在表格的任何位置,而显示3列“test2”。

我真的很困惑这一点,任何帮助将不胜感激。

回答

4

编辑

如何让我在表中的字段不可编辑?现在,如果我 双击我可以编辑细胞

覆盖这两种方法对DefaultTableModel,pseudo_code

model = new DefaultTableModel(data, columnNames) { 

    private static final long serialVersionUID = 1L; 

    @Override 
    public Class getColumnClass(int column) { 
     return getValueAt(0, column).getClass(); 
    } 

    @Override 
    public boolean isCellEditable(int row, int column) { 
     return false; 
    } 
}; 
table = new JTable(model); 
+0

我明白了!大声笑我花了整整一天阅读向量和表模型,结果我做错了。另一方面,如何让表格中的字段不可编辑?现在,如果我双击,我可以编辑单元格的内容。 – AmuletxHeart

+0

@AmuletxHeart看到我的编辑 – mKorbel

1

的内容把你的表中JScrollPane

例子:

container.add(new JScrollPane(table)); 

,或者你可以直接添加到容器(如javadoc说明)

container.setLayout(new BorderLayout()); 
container.add(table.getTableHeader(), BorderLayout.PAGE_START); 
container.add(table, BorderLayout.CENTER); 

有关如何使细胞不可编辑是你的问题做,

覆盖isCellEditable(..)在您的TableModel

@Override 
public boolean isCellEditable(int rowIndex, int colIndex) { 
    // return false for not making it editable. 
    return false; 

}