0

I have a problem creating a JTable in my GUI. The GUI is created in the main thread and enables a file to be opened. The file is then used to create a table model and add info to it. A JTable is then created with the table model and added to the GUI. My problem is that the GUI doesn't show. Code:

package example;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;


public class Example extends JFrame{

    private JButton button;
    private JTable table;
    private DefaultTableModel model;
    private String path = "C:/Users/gilbert/Documents/11111.xls";

    public Example(){
    super("Example");   
        setLayout(new BorderLayout());
        button = new JButton("Start");
        button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            createTableModel_Three_By_Six(path);
        }
    });
        add(button,BorderLayout.NORTH);
        setSize(400, 400);    
    }

    public void createTableModel_Three_By_Six(String fpath){

        model =  new  DefaultTableModel();
        ExcelParser exPareser =  new  ExcelParser(fpath);
        int rows = exPareser.getRowNumber();
        String rowToAdd[] = new String[3];
        int i, j = 0;
        while(j < rows){
            i= 0;
            while(i < 3){
                rowToAdd[i] = exPareser.accessRow(j);
                i++;
                j++;
            if(j == rows){
                if(i==1){
                    rowToAdd[1] = "";
                    rowToAdd[2] = "";
                }
                else if(i==2){
                    rowToAdd[2] = "";
                }
            }
        }
            model.addRow(rowToAdd);
        }
        table = new JTable(model);
        add(new JScrollPane(table));

    }

    public static void main(String[] args) {


        Example app = new Example();
        app.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        app.setVisible(true);
    }

}
  • 1
    An actually [runnable example that demonstrates your problem](https://stackoverflow.com/help/mcve) would involve less guess work and better responses – MadProgrammer Apr 23 '14 at 01:39
  • I know, but the whole program is pretty long. People tend to disregard questions with lots of code. – Nwaiwu Gilbert Apr 23 '14 at 01:41
  • There are a woeful number of reasons that this might occur, personally, I'm not about to try and list all of them. An "example" is just that. Write a small program that emulates the problem so we gain an idea of what it is you are doing and how you are doing it...As it stands, the question does not contain enough information to diagnose a probable cause... – MadProgrammer Apr 23 '14 at 01:42
  • *" People tend to disregard questions with lots of code."* The M of MCVE stands for minimal, and the article explains how to make source minimal. These two points make me think you did not read the linked article, or only glanced at it. -1 – Andrew Thompson Apr 23 '14 at 01:54
  • @Andrew Thompson: Edited the question. Provided short example. And the "ExcelParser" class is correct. My problem is that i cant add a JTable to an existing GUI. – Nwaiwu Gilbert Apr 23 '14 at 01:55
  • `private String path = "C:/Users/gilbert/Documents/11111.xls";` How is this 'complete'? How do you expect me to believe you absorbed that article, and prepared a shorter code, within 1 minute 59 seconds? I feel you are intending to continue posting random rubbish until everyone smiles. I'm done with this nonsense. Good luck with it. – Andrew Thompson Apr 23 '14 at 02:00
  • @Andrew Thompson: The simple question I am asking is how to add the JTable properly to the GUI after it has already been created. I take an Excel file, get info from it and store it in an array and use the array to create a table model. What else do you want? – Nwaiwu Gilbert Apr 23 '14 at 02:03
  • *"What else do you want?"* Else? This is your problem, not mine, so it is about whether you want help or not. I am entirely impartial. As to how to create an MCVE:- If we try to run that code, it will fail due to lack of the excel file, and now I look closer at the method that reads it `ExcelParser` ..WTF is that? It seems that code would not even ***compile*** for others. To make it into an MCVE, hard code some sample data in the source code and get rid of ***all*** external dependencies (whether they are data files, or software used to read them). – Andrew Thompson Apr 23 '14 at 02:10
  • @Andrew Thompson: Ok, I understand what you mean. I did not plan for people to try and compile what i wrote. I was just thinking that it was something simple that I missed in properly adding the JTable to the GUI. – Nwaiwu Gilbert Apr 23 '14 at 02:16

1 Answers1

2

The problem, with your example, is the fact that there are no columns for the table, which means when you add the table to the frame, it doesn't know how to display the table contents.

So, by doing something as simple as...

model = new DefaultTableModel(new Object[]{"A", "B", "C"}, 0);
//...
table = new JTable(model);
add(new JScrollPane(table));
revalidate();

I was able to get the table to appear properly, with it's contents

Beware though, each time you call this method, a new JTable will be created. Instead, you should construct the JScrollPane and JTable at an earlier stage and simply update the TableModel

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366