luskbo
luskbo

Reputation: 165

Java JTable Not populating with DefaultTableModel

I have a JTable that I am trying to populate with the file names located in a particular folder.

I can load the JTable and it looks as if it is being populated from the data, but no test appears in the JTable rows.

I can tell its loading the files, as when I place 2 files in the folder, the JTable creates 2 rows when it loads. I can change the number of files in the folder, and when I re run the program, it will load the exact number of rows as files in the folder.

Any ideas? I used this methods of populating several other tables, and they seem to work, so i'm really confused as to why this one doesn't.

package testTable;

import java.awt.BorderLayout;


public class main extends JFrame {

public JPanel contentPane;
public JTable table;
public DefaultTableModel rulesModel;


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

/**
 * Create the frame.
 */
@SuppressWarnings("serial")
public main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    JScrollPane scrollPane = new JScrollPane();
    GroupLayout gl_contentPane = new GroupLayout(contentPane);
    gl_contentPane.setHorizontalGroup(
        gl_contentPane.createParallelGroup(Alignment.TRAILING)
            .addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE))
    );
    gl_contentPane.setVerticalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 187, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(54, Short.MAX_VALUE))
    );




    rulesModel = new DefaultTableModel();
    rulesModel.setColumnIdentifiers(new String[] {"File Name"}); 
    table = new JTable(rulesModel) {
        public boolean isCellEditable(int row, int column) {
            return false;
        }
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            return (Component) null;
        }
    };


    File folder = new File("C:\\Scripts\\new\\files\\folder\\");
    File[] listOfFiles = folder.listFiles();

    for (File file : listOfFiles) {
        if (file.isFile()) {
            String filename = file.getName();

            rulesModel.addRow(new String[] {filename});
            System.out.println(filename);
        }
    }     




    table.setModel(rulesModel);
    scrollPane.setViewportView(table);
    contentPane.setLayout(gl_contentPane);
}


}

Upvotes: 1

Views: 334

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

This is messing you up:

 public Component prepareRenderer(TableCellRenderer renderer, int row,
       int column) {
    return (Component) null;
 }

It takes whatever cell renderer that exists for the JTable, tosses it aside and thus guarantees that the table will not display any information. So get rid of this faulty override.

Although this does beg the question:
Why is this code even in there as it's guaranteed to make sure that nothing displays at all? Why get rid of the cell renderer?

Upvotes: 2

Related Questions