I'm trying to list some data gathered from files into a JTable - here's the code I'm using:

/* Packages listed in a JTable */
        Object[] columnNames = {"Package", "Version", "Installed"};
        
        int packageCount = getPackageCount();
                
        Object[][] data = new Object[packageCount][2];
        
        Scanner dbIndex = new Scanner(new File("/opt/npkg/dbindex.npkg"));
        
        int row = 0;
        
        while(dbIndex.hasNext()) {
            PackageReader pkg = new PackageReader(new File("/opt/npkg/" + dbIndex.nextLine() + ".pkg"));
            data[row][0] = pkg.getPackageName();
            data[row][1] = pkg.getPackageVersion();
            row++;
        }
        
        JTable table = new JTable(data, columnNames);
        table.setAutoResizeMode(4);
        table.setSize(windowSize.width, 20);
        table.setGridColor(Color.LIGHT_GRAY);
        table.revalidate();
        
        cpanel.add(table);

Here are some of the problems I'm running into:
- The only data I can see is in the first row. Anything after that, I have to click on
every element in the corresponding row to see any more data.
- The table stays very very small in the centre of the cpanel (JPanel - "Centre Panel")

Any help?

I would look into using a TableModel to display your data. Take your data, place it into the TableModel and then set the TableModel on the Table.

For the sizing issue. I would recommend using a layout manager for your panel. Which it looks like you're not using. Setting the size on components tends not to work very well in java and it really is best to use a layout manager.

That actually could be the issue with the Table not displaying your data. The table might not know that the lower columns are visible and therefore not displaying them when the table is first displayed.

Alright, thanks, hooknc. I'll change it up, and post my results. :cheesy:

Ok, I used this tutorial from Sun, but a problem quickly arose: I need to be able to read data from a file and then display it into a table. Here's the code I'm working with (it complains at the while() loop):

import javax.swing.table.AbstractTableModel;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
 * This is the Table Model for listing populated packages for use with NPKG.
 * 
 * @author (Nick Saika) 
 * @version (1.0 - 2006)
 */
public class PackageTableClass extends AbstractTableModel
{
    private String[] columnNames = { "Package", "Version", "Installed" };
    private Object[][] data = {
        {"Vim - VI Improved (Text Editor)", "7.0", "YES"},
        {"Nano - The GNU Nano Text Editor)", "0.68", "NO"}
    };
    
    int packageCount = getPackageCount();
                
    Object[][] data = new Object[packageCount][2];
        
    Scanner dbIndex = new Scanner(new File("/opt/npkg/dbindex.npkg"));
      
    int row = 0;
        
    while(dbIndex.hasNext()) {
        PackageReader pkg = new PackageReader(new File("/opt/npkg/" + dbIndex.nextLine() + ".pkg"));
        data[row][0] = pkg.getPackageName();
        data[row][1] = pkg.getPackageVersion();
        row++;
    }
    
    public int getColumnCount()
    { 
        return columnNames.length; 
    }
    
    public int getRowCount() 
    { 
        return data.length; 
    }
    
    public String getColumnName(int col) 
    { 
        return columnNames[col]; 
    }
    
    public Object getValueAt(int row, int col) 
    { 
        return data[row][col]; 
    }
    
    public Class getColumnClass(int c) 
    { 
        return getValueAt(0, c).getClass(); 
    }
    
    public void setValueAt(Object value, int row, int col)
    {
        data[row][col] = value;
        fireTableCellUpdated(row,col);
    }
    
    public int getPackageCount() throws FileNotFoundException
    {
        // This method gets the number of packages by reading the one
        // (and only) value from "/opt/npkg/pkgcount.npkg".
        int x;
        Scanner s = new Scanner(new File("/opt/npkg/pkgcount.npkg"));
        x = s.nextInt();

        return x;
    }        
}

EDIT: I'm writing a *.tar.gz package manager - I know about Pacman, but it won't compile on my system, and I figured this would be a good project to help me get better acquainted with Java.

EDIT: Also, even though the table works if I comment out the while() loop and the other stuff around it, it still shows up quite small in the middle of the panel, even after I've implemented a FlowLayout layout manager.

Ok, I fixed the whole sizing issue by throwing the table into a ScrollPane :cheesy:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.