Chris
Chris

Reputation: 103

How to change from jList to JTable (data from DB have many columns and rows)

I'll try to explain as good as I possibly can. I'm a noob and have been trying to fix this for hours now...

What I want to do is to CHANGE so that the data shows up in a jTable instead of the jList. But I have no clue how to do it.

This is what the code looks like for now:

GUI

private void jComboBox1_actionPerformed(ActionEvent e)throws SQLException {

    String selected = jComboBox1.getSelectedItem().toString();
     String[]tmp = conn.showTable1(selected);
    jList1.setListData(tmp);

}

Controller

public String[] showTable1(String table)throws SQLException {
    ArrayList<String> list1 = dal.showTable1(table);
    return list1.toArray(new String[list1.size()]);
 }

Data Access Layer

public ArrayList<String> showTable1(String table)throws SQLException {
    ArrayList<String> list1 = new ArrayList<String>();

    Statement stmt = con.createStatement();
    String query1 = "select * from [" +table+"]";
        System.out.println(query1);
    ResultSet rset = stmt.executeQuery(query1);
    ResultSetMetaData rsmd = rset.getMetaData();
    while (rset.next()) {
        String result = "";

        for(int i = 1; i < rsmd.getColumnCount(); i++) {
            result += rset.getString(i) + "\t" + "\t";
   //     
        }
            list1.add(result);
        }

    return list1;

    }

Please help!

Kind Regards, Chris

Upvotes: 1

Views: 644

Answers (1)

camickr
camickr

Reputation: 324197

See Table From Database for a couple of simple suggestions:

  1. Search the web for an existing ResultSetTableModel.
  2. Use the TableFromDatabase Example code
  3. Use the ListTableModel presented in the blog

Upvotes: 2

Related Questions