Maaz
Maaz

Reputation: 65

Printing Data on jTextArea in a table form

Actually I'm retrieving data from mysql and wanted to print that data on the jTextArea in the table form using swing.

Upvotes: 1

Views: 1129

Answers (1)

Bon
Bon

Reputation: 3103

You can use JTable instead of JTextArea, an example would be:

// load the data from database into a vector
    ResultSet resultSet = stmt.executeQuery();
    Vector data = new Vector();
    while (resultSet.next()) {
        Vector row = new Vector();
        row.add(resultSet.getString(1));
        row.add(resultSet.getInt(2));
        data.add(row);
    }

    // initialize the column names
    Vector columnNames = new Vector();
    columnNames.add("Name");
    columnNames.add("Num");

    // create the jtable with the data and the column names
    JTable jTable = new JTable(data, columnNames);

    JFrame jFrame = new JFrame();
    jFrame.setVisible(true);
    jFrame.add(new JScrollPane(jTable));
    jFrame.pack();

Details on JTable can be found on Oracle JTable tutorial: https://docs.oracle.com/javase/tutorial/uiswing/components/table.html

Upvotes: 1

Related Questions