user13353216
user13353216

Reputation:

JTable does not display values

I am implementing a GUI using java in eclipse IDE. I want to display a table. This is how I implement the program

import java.awt.EventQueue;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.text.TabableView;

import com.model.FloorDetails;

public class ClientGUI {

    private JFrame frame;
    private ClientMain clientMain = new ClientMain();
    private JTable table;

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

    /**
     * Create the application.
     */
    public ClientGUI() {
        initialize();
    }

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

        table = new JTable();
        table.setBounds(67, 146, 1, 1);
        frame.getContentPane().add(table);
        executeTable();

    }

    public void executeTable() {
        Object[] columns = new String[] {
                "ID", "Room No", "Floor No", "CO2 Level", "Smoke Level", "Status"
        };


        ArrayList<FloorDetails> arrayList = clientMain.getSensors();

        Object[][] data = new Object[arrayList.size()][6];

        for(int i = 0; i < arrayList.size(); i++) {
            data[i][0] = arrayList.get(i).getId();
            data[i][1] = arrayList.get(i).getRoomNo();
            data[i][2] = arrayList.get(i).getFloorNo();
            data[i][3] = arrayList.get(i).getCo2Level();
            data[i][4] = arrayList.get(i).getSmokeLevel();
            data[i][5] = arrayList.get(i).getStatus();
        }

        table = new JTable(data,columns);
        frame.setTitle("Sensor Details");

    }

}

clientMain.getSensors() method retrieves all the data as expected(I tried printing on the console and everything printed). But when I run the program, it display an empty window.

I tried like this just to see if I am making a mistake when assigning the values to the 2D array but nothing changed

        Object[][] data = {
                {"a", "b", "c", "d", "e", "f"},
                {"g", "h", "i", "j", "k", "l"}
        };

Where I have done wrong in this program? Thanx in advance!

Upvotes: 0

Views: 267

Answers (1)

Klaus
Klaus

Reputation: 1731

Well, one problem in your code is that, you are populating one table and adding another table to the frame. One approach to fix this would be

public JTable executeTable(  ) { // Make this method return a JTable
    Object[] columns = new String[] {
            "ID", "Room No", "Floor No", "CO2 Level", "Smoke Level", "Status"
    };

    ArrayList<FloorDetails> arrayList = clientMain.getSensors();

    Object[][] data = new Object[arrayList.size()][6];

    for(int i = 0; i < arrayList.size(); i++) {
        data[i][0] = arrayList.get(i).getId();
        data[i][1] = arrayList.get(i).getRoomNo();
        data[i][2] = arrayList.get(i).getFloorNo();
        data[i][3] = arrayList.get(i).getCo2Level();
        data[i][4] = arrayList.get(i).getSmokeLevel();
        data[i][5] = arrayList.get(i).getStatus();
    }

    table = new JTable( data, columns);
    return table;
}

And then change the initialize method to use this returned table

private void initialize() {
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    table = executeTable();
    frame.add(table.getTableHeader(), BorderLayout.PAGE_START);
    frame.add(table, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setTitle("Sensor Details");
    frame.setVisible(true);
}

You don't need window.frame.setVisible(true); at the main method in this approach

Upvotes: 1

Related Questions