2

My goal is to display data from a database using JTable.

Referencing code from: Most simple code to populate JTable from ResultSet

I modified the code to suit my situation. I have a class TopicData with the following method:

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);

}

And in another class 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;
}

I realized that my table does not have headers at the top of each column. https://i.stack.imgur.com/FcnHb.png

So I simplified my TopicData to troubleshoot:

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);
}

Once again, the table does not have headers. The word "test1" does not appear anywhere on the table, while there are 3 columns of "test2" displayed.

I'm really puzzled by this, any help would be appreciated.

Community
  • 1
  • 1
AmuletxHeart
  • 396
  • 4
  • 16

2 Answers2

4
  • put JTable to the JScrollPane,

  • otherwise have to change LayoutManager to the BorderLayout for container, then to put JTable to the CENTER area, and getTableHeader from JTable and put JTableHeader to the NORTH / SOUTH

  • proper way is by using JScrollPane

EDIT

how do I make the fields in the table uneditable? Right now if I double click I can edit the content of the cells

override both methods for 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);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I see! lol I spent the whole day reading on vectors and table models and it turns out I'm doing it wrong. On another note, how do I make the fields in the table uneditable? Right now if I double click I can edit the content of the cells. – AmuletxHeart Jan 10 '13 at 14:00
1

Put your table in the JScrollPane.

Example:

container.add(new JScrollPane(table));

or you can do it by directly adding to the container (as stated in the javadoc)

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

For your question on how to make a cell not editable is,

Override isCellEditable(..) in your TableModel

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

}
Amarnath
  • 8,736
  • 10
  • 54
  • 81