0

I am trying to show the data I have in my database with Swing in a Frame. It works but It doesn't show the columns names! just the data in the table. This is the code I used.

try {
    con = (Connection) DriverManager.getConnection(DATABASE_URL , USERNAME, PASSWORD);
    stmt = (Statement) con.createStatement();
    ResultSet rs = stmt.executeQuery(QUERY_FIND_ITEMS);

        {
    ResultSetMetaData md = (ResultSetMetaData) rs.getMetaData();
    int columns = md.getColumnCount();

    //  Get column names
    for (int i = 1; i <= columns; i++)
    {
        columnNames.add( md.getColumnName(i) );
    }

    //  Get row data
    while (rs.next())
    {
        ArrayList row = new ArrayList(columns);

        for (int i = 1; i <= columns; i++)
        {
            row.add( rs.getObject(i) );
        }

        data.add( row );
    }
}
}   
catch (SQLException e)
{
    System.out.println( e.getMessage() );
}

Vector<String> columnNamesVector = new Vector();
Vector dataVector = new Vector();

for (int i = 0; i < data.size(); i++)
{
    ArrayList subArray = (ArrayList)data.get(i);
    Vector subVector = new Vector();

    for (int j = 0; j < subArray.size(); j++)
    {
        subVector.add(subArray.get(j));
    }
    dataVector.add(subVector);
}

for (int i = 0; i < columnNames.size(); i++ ) {
    columnNamesVector.add(columnNames.get(i));
}
//  Create table with database data    
JTable table = new JTable(dataVector, columnNamesVector)
{
    public Class getColumnClass(int column)
    {

        for (int row = 0; row < getRowCount(); row++)
        {
            Object o = getValueAt(row, column);

            if (o != null)
            {
                return o.getClass();
            }
        }


        return Object.class;
    }
};


          JScrollPane scrollPane =new JScrollPane(table);
            getContentPane().add( scrollPane, BorderLayout.WEST );

            JPanel buttonPanel = new JPanel();
            getContentPane().add( buttonPanel, BorderLayout.SOUTH );

            table.setForeground(Color.BLUE);

Why doesn't it display the columns names?

AIR
  • 73
  • 1
  • 9
  • @Sanjeev : String QUERY_FIND_ITEMS = "SELECT * FROM itemsunit "; – AIR Apr 29 '16 at 09:06
  • i'm not entirely sure about it but have u tried using a tableModel to set data to the table instead? – DriLLFreAK100 Apr 29 '16 at 09:16
  • @DriLLFreAK100 no, but my problem is not in the data , because they are all displayed correctly. What is missing are the names of the columns – AIR Apr 29 '16 at 09:34
  • 1
    1- Don't override `getColumnClass` from `JTable`, use a `TableModel`, that's it's job; 2- Wrap the `JTable` to a `JScrollPane`, see [How to Use Tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) and [How to Use Scroll Panes](http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html) for more details – MadProgrammer Apr 29 '16 at 09:57
  • @MadProgrammer The columns' names are still not displayed (only the data) – AIR Apr 29 '16 at 10:31
  • `catch (SQLException e) { System.out.println( e.getMessage() ); }` The code after the end of the `catch` block will not work correctly if the DB query fails. Best to include that code within the `try` block. And a tip: change `System.out.println( e.getMessage() );` to `e.printStackTrace();` both shorter ***&*** more informative. – Andrew Thompson Apr 29 '16 at 13:50
  • @AndrewThompson Perfect! It solved my problem Can you post it as an answer and I will accept it – AIR Apr 29 '16 at 14:17
  • *"Can you post it as an answer.."* The site won't allow anyone to post an answer on a question that has been closed, so I posted it in answer to the one that is not closed (and included the code snippet for context). – Andrew Thompson Apr 29 '16 at 14:24
  • @AndrewThompson: I understand. Thank you – AIR Apr 29 '16 at 14:26

0 Answers0