-3

I am using Eclipse & Swing to create this project. I am trying to populate a JTable. I've fetched the values from database in the method Display_UserInfo_in_JTable().

But I do not know how to use this method to populate my JTable. I think the method should have a return type for this, but I do not know how can I do it?

package view;

import controller.*;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TableView {

    private JFrame frame;
    private JTable table;

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

    private ArrayList<UserInfo> UserInfoList()
    {
        try
        {
            UserInfo userinfo;
            ArrayList<UserInfo> UserInfoList = new ArrayList<UserInfo>();
            Connection sqlCon = DB_con.getSQLConnection();
            PreparedStatement ps = sqlCon.prepareStatement("select id,name,username,contact,gender from temp");

            ResultSet rs = ps.executeQuery();
            while(rs.next())
            {
                userinfo = new UserInfo(
                                        rs.getInt("id"),
                                        rs.getString("name"),
                                        rs.getString("username"),
                                        rs.getString("contact"),
                                        rs.getString("gender")
                                        );
                UserInfoList.add(userinfo);
            }
            return UserInfoList;
        }
        catch(Exception ex)
        {
            System.out.println(ex.toString());
            return null;
        }       
    }

    public void Display_UserInfo_in_JTable()
    {
        try
        {
            ArrayList<UserInfo> list = UserInfoList();
            DefaultTableModel dtm = new DefaultTableModel();
            Object row[] = new Object[5];

            for(int i = 0 ; i < list.size(); i++)
            {
                row[0] = list.get(i).getId();
                row[1] = list.get(i).getName();
                row[2] = list.get(i).getUsername();
                row[3] = list.get(i).getContact();
                row[4] = list.get(i).getGender();

                dtm.addRow(row);
            }
            table.setModel(dtm);
        }
        catch(Exception ex)
        {
            System.out.println(ex.toString());
        }
    }

    /**
     * Create the application.
     */
    public TableView() {
        initialize();
        Display_UserInfo_in_JTable();

    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 598, 402);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(10, 11, 562, 341);
        frame.getContentPane().add(panel);
        panel.setLayout(null);

        table = new JTable();
        //table.setBounds(49, 163, 92, -97);
        JScrollPane jsp = new JScrollPane(table);
        panel.add(table);
    }
}

I've added table.setModel(dtm); in the method Display_UserInfo_in_JTable(), still can not get the result.

nischalinn
  • 1,133
  • 2
  • 12
  • 38
  • http://stackoverflow.com/questions/10620448/most-simple-code-to-populate-jtable-from-resultset – Jett Apr 06 '17 at 03:26
  • You could [just perform a simple search](http://stackoverflow.com/search?q=%5Bjava%5D+%5Bswing%5D+jtable+arraylist) – MadProgrammer Apr 06 '17 at 03:29
  • Maybe you just missed a `table.setModel(dtm);` in your `Display_UserInfo_in_JTable()` BTW you could also improve upon the naming convetions. – Rajeev Sreedharan Apr 06 '17 at 03:31
  • [That's one way](http://stackoverflow.com/questions/20337246/jtable-display-data-in-arraylist/20337669#20337669), [that's another](http://stackoverflow.com/questions/18928112/create-jtable-from-arraylist-of-objects-java/18928222#18928222), [you could probably do something like this](http://stackoverflow.com/questions/24817251/how-to-display-records-in-a-jtable-from-an-arraylist-txt-file-in-java-mvc/24817578#24817578), [this might even work](http://stackoverflow.com/questions/30042651/is-there-a-way-to-put-an-arraylist-into-a-jtable-where-each-line-is-the-next-ind/30042798#300427980) – MadProgrammer Apr 06 '17 at 03:32
  • You'll also find that `null` layouts suck and more trouble than they are worth and you'll find a `JTable` prefers to be presented in a `JScrollPane` – MadProgrammer Apr 06 '17 at 03:33
  • It will be very helpful for me if someone could provide solution rather than commenting or providing links to another posts. – nischalinn Apr 06 '17 at 05:58
  • @RajeevSreedharan I've added the code in the method but still not working. Please help. – nischalinn Apr 06 '17 at 06:05
  • @nischalinn: [edit](http://stackoverflow.com/posts/43245102/edit) your question to show your call to `setModel()`. – Catalina Island Apr 06 '17 at 09:08
  • @CatalinaIsland edited my code as your suggestion. Please help. – nischalinn Apr 06 '17 at 10:23

1 Answers1

2

There are too many problems with your code :

  1. Coding and naming conventions
  2. The displayUserInfoInJTable() never gets called (or atleast you haven't posted that code)
  3. The table model isn't supposed to be coded this way
  4. There are no column identifiers, just the rows
  5. You have used an absolute layout (layout set as null) but your bounds are negative -97, this is likely your main issue

For point 5, either use a standard layout or set the bounds accordingly.

Rajeev Sreedharan
  • 1,753
  • 4
  • 20
  • 30
  • Thanks for your suggestions. I've put the method inside the constructor and commented the setBounds property of JTable. Still can not get the table. Please help. – nischalinn Apr 06 '17 at 14:16
  • Follow the suggestion for point 5 mentioned. use a standard layout or just comment out the `panel.setLayout(null);` – Rajeev Sreedharan Apr 06 '17 at 14:18
  • Commented the code `panel.setLayout(null);` still not working. I am very much confused. Please can you provide the link of using populating JTable with database in Eclipse. Most of the tutorials are using Netbeans. – nischalinn Apr 06 '17 at 15:12
  • Have you added `dtm.setColumnIdentifiers()` and `table.setModel(dtm)` – Rajeev Sreedharan Apr 07 '17 at 00:24