Piet Jetse
Piet Jetse

Reputation: 418

How to do getValueAt() when using an AbstractTableModel

I'm trying to make an program that shows all files in the temerary folder using an AbstractTableModel but how do i code the getValueAt() method. The names of the files should be in the first column and the file path should be in the second column. I nulled it for now but can someone show me hoe i should code it?

This is what i got so far:

public class UI extends JFrame {

private JPanel contentPane;
private JTable table;
File[] dir = new File(System.getProperty("java.io.tmpdir")).listFiles();

public static void main(String[] args) {

public UI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 542, 422);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP);
    tabbedPane.setBounds(0, 0, 526, 384);
    contentPane.add(tabbedPane);

    JPanel panel = new JPanel();
    tabbedPane.addTab("Temp Files", null, panel, null);
    panel.setLayout(null);

    final AbstractTableModel myAbstractTableModel = new AbstractTableModel() {

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public int getRowCount() {
            return dir.length;
        }

        @Override
        public Object getValueAt(int arg0, int arg1) {
            return null;
        }


    };

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(10, 11, 402, 334);
    panel.add(scrollPane);

    table = new JTable();
    scrollPane.setViewportView(table);
    table.setModel(myAbstractTableModel);
}

}

Upvotes: 1

Views: 738

Answers (1)

trashgod
trashgod

Reputation: 205775

In your example, arg0 is the row and arg1 is the col, so dir[arg0] is the File for each row. In your implementation of getValueAt(), return file.getName() or file.getPath() as indicated by the col value. EnvTableTest is a related example that uses the more descriptive parameter names.

Upvotes: 1

Related Questions