1

These are my codes for my jTable in java swing.

private JTable getJTable() {
    if (jTable == null) {
        Vector columnNames = new Vector(); //Vector class allows dynamic array of objects
        Vector data = new Vector();
        JPanel panel = new JPanel();   
        panel.setSize(new Dimension(198, 106));

        try {
            DBController db = new DBController();
            db.setUp("IT Innovation Project");
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
            String dsn = "IT Innovation Project";
            String s = "jdbc:odbc:" + dsn;
            Connection con = DriverManager.getConnection(s, "", "");
            String sql = "Select * from forumTopics";
            java.sql.Statement statement = con.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columns = metaData.getColumnCount();

            for (int i = 1; i <= columns; i++) {
                columnNames.addElement(metaData.getColumnName(i));
            }

            while (resultSet.next()) {
                Vector row = new Vector(columns);
                for (int i = 1; i <= columns; i++) {
                    row.addElement(resultSet.getObject(i));
                }
                data.addElement(row);
            }

            resultSet.close();
            ((Connection) statement).close();
        } catch (Exception e) {
            System.out.println(e);
        }

        jTable = new JTable(data, columnNames);
        TableColumn column;

        for (int i = 0; i < jTable.getColumnCount(); i++) {
            column = jTable.getColumnModel().getColumn(i);
            column.setMaxWidth(250);
        }

        String header[] = {"description", "title", "category", "posted by"};  

        for(int i=0;i<jTable.getColumnCount();i++) { 
            TableColumn column1 = jTable.getTableHeader().getColumnModel().getColumn(i);    
            column1.setHeaderValue(header[i]);  
        } 

        jTable.setBounds(new Rectangle(82, 218, 736, 292));
        jTable.setEnabled(false);
        jTable.setRowHeight(50);
        jTable.getRowHeight();         
    }
    return jTable;
}

I set an array for the header of my table. However, the program only shows the data inside my database without any header. Can somebody please enlighten me? Thanks in advance.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
GabrielHeng
  • 63
  • 1
  • 11
  • 2
    code which you've added in question cant help to provide answer. post [SCCEE](http://sscce.org) – vels4j Dec 21 '12 at 04:07
  • Please look at this answer http://stackoverflow.com/questions/13935934/java-jtable-column-headers-not-showing/13936925#13936925 – Amarnath Dec 21 '12 at 07:03

1 Answers1

3

Put your JTable in JScrollPane like

add(new JScrollPane( getJTable() ) );
vels4j
  • 11,208
  • 5
  • 38
  • 63
  • I added these in but my table is not scrollable. jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); JScrollPane scroller = new JScrollPane(jTable); scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); – GabrielHeng Dec 21 '12 at 04:42
  • Yup I wrap the table with a scroll pane. Then everything is fine now. Now I going to adjust the width for each column. Thanks everybody. – GabrielHeng Dec 21 '12 at 05:05