user2868900
user2868900

Reputation: 771

JAVA - Populate JTable with 2d String array from SQL

I am trying to show results from my String arrays (created methods) in a JTable - but it seems like it's not working.

Code for Methods (just pasting the first two):

    package t3.isprojekt.uppg2.dal;

    import java.sql.*;
    import java.util.*;

    public class DAL {

        private static String connStr = "jdbc:sqlserver://Localhost:1433;databases=CronusDB;user=root;password=root;";

        public static Connection getConn() throws SQLException {
            return DriverManager.getConnection(connStr);
        }

        // ------ Methods for Employee & related tables ------\\

        public String[][] getEmpData() throws SQLException {
            String getEmp = "SELECT [No_], [First Name], [Last Name], [Address], [City] FROM [CRONUS Sverige AB$Employee];";
            Statement stmt = null;
            stmt = getConn().createStatement();
            ResultSet rset = stmt.executeQuery(getEmp);
            ResultSetMetaData rsetMeta = rset.getMetaData();
            int colCount = rsetMeta.getColumnCount();
            int rowCount = 0;
            while (rset.next()) {
                rowCount = rset.getRow();
            }
            String[][] temp = convertRsToArray(rset, rowCount, colCount);
            return temp;
        }

        public String[][] getEmployeeMetaData() throws SQLException {
            String empMetaData = "SELECT TABLE_CATALOG, TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM [CronusDB].[INFORMATION_SCHEMA].[COLUMNS]";
            Statement stmt = null;
            stmt = getConn().createStatement();
            ResultSet rset = stmt.executeQuery(empMetaData);
            ResultSetMetaData rsetMeta = rset.getMetaData();
            int colCount = rsetMeta.getColumnCount();
            int rowCount = 0;
            while (rset.next()) {
                rowCount = rset.getRow();
            }
            String[][] temp = convertRsToArray(rset, rowCount, colCount);
            return temp;
        }

GUI Class - Here I don't know how to connect the model to the String arrays. Tried to google but nothing returned any info to what I am trying to achieve:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import t3.isprojekt.uppg2.controller.Controller;
import t3.isprojekt.uppg2.dal.DAL;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class View extends JFrame {

    private JPanel contentPane;
    private Controller ctrl;
    private DefaultTableModel tableModel = new DefaultTableModel(new Object[][] { ctrl.getEmployeeData() },
            new Object[] { ctrl.getEmpMetaData() });
    private JTable table_2;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    View frame = new View();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * 
     * @throws SQLException
     */
    public View() throws SQLException {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JTable table = new JTable(tableModel);
        table.setBounds(0, 65, 432, 188);
        contentPane.add(table_2);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(0, 247, 432, -175);
        contentPane.add(scrollPane);

    }
}

Upvotes: 0

Views: 308

Answers (2)

Tom Wellbrock
Tom Wellbrock

Reputation: 3042

I suppose that you have a simple typo in your code. you call:

enterJTable table = new JTable(tableModel); 

but instead of adding table to your contentPane your adding table_2 which has no data in it:

     private JTable table_2;   

     ...

     contentPane.add(table_2);

is it even initialiset yet? Am I blind?

I think it should look like this:

contentPane.add(table);

Also if I didn't overlooked something your table_2 is dead code.

Edit:

were is your Controller class, was it instanciated?

Controller ctrl;

...

new DefaultTableModel(new Object[][] { **ctrl.getEmployeeData()** },
            new Object[] { **ctrl.getEmpMetaData()** });

I cant help you further if i cant see what your method does. Maybe, just a guess, is there another typo? do you want to call:

DAL dal = new DAL()

...

new DefaultTableModel(new Object[][] { **dal.getEmployeeData()** },
            new Object[] { **dal.getEmpMetaData()** });

if this is the case you can't do this:

new DefaultTableModel(new Object[][] { ctrl.getEmployeeData() },
        **new Object[]** { dal.getEmpMetaData() });

because getEmpMetaData() returns a String[][] instead of String[]

Last but not least, to display your Jtable in a scrollpane do he following:

JScrollPane scrollPane = new JScrollPane(table);

its a minor change to your code, but it does the trick :P

Upvotes: 1

sifho
sifho

Reputation: 665

DefaultTableModel has these following Constructors

DefaultTableModel()
DefaultTableModel(int rowCount, int columnCount)
**DefaultTableModel(Object[][] data, Object[] columnNames)**
DefaultTableModel(Object[] columnNames, int rowCount)
DefaultTableModel(Vector columnNames, int rowCount)
DefaultTableModel(Vector data, Vector columnNames)

You can not use

Object[][] array
as the second parameter of the constructor. getEmployeeMetaData() is returning a two dimensional String array where the constructor can accept an one dimensional array.

And instead of adding table, you have added table_2 to the contentPane.

JTable table = new JTable(tableModel);
table.setBounds(0, 65, 432, 188);
contentPane.add(table_2);

I am assuming, you are trying to add the JTable to the ScrollPane. The following code will help you do that.

 JTable table = new JTable(tableModel);
 JScrollPane scrollPane = new JScrollPane(table);
 scrollPane.setBounds(0, 65, 432, 188);
 contentPane.add(scrollPane);

I have also created a sample data set so that you can implement it easily.

 String [][] one = {{"SampleData01" , "SampleData02"},
                   {"SampleData11" , "SampleData12"}};
 String [] two = {"SampleColumn1" , "SampleColumn2"};
 private DefaultTableModel tableModel = new DefaultTableModel(one,two);

If you run the code by the changes I mentioned, the output will be like this. enter image description here

Upvotes: 0

Related Questions