Question

I have the following code:

    class Files {       
    private static String files;
    private static String duration;
    private static String status;

    public Files(String files, String duration, String status) {
        this.files = files;
        this.duration = duration;
        this.status = status;
    }

    public static String getfiles() {
        return files;
    }

    public static String getduration() {
        return duration;
    }

    public static String getstatus() {
        return status;
    }
}

    Map<Files, String> hmap = new HashMap<Files,String>();

    private void AddFiles(String addfile,String addurr,String addstatus, String addpath){       
    Files f = new Files(addfile, addurr, addstatus);                
    hmap.put(f, addpath);       
}
    final JTable table = new JTable();
    table.setBounds(26, 27, 664, 274);
    table.setModel(new MyTableModel());

So I'm creating a new table and overriding "getValueAt".

        class MyTableModel extends AbstractTableModel {    
        @Override
        public int getColumnCount() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public int getRowCount() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
             switch (columnIndex) {
             case 0:
                    return Files.getfiles(); 
             case 1:
                    return Files.getduration();
             case 2:
                    return Files.getstatus();
             default:
                    throw new IndexOutOfBoundsException();
             }
        }
    }

Yet I am not able to load the variables from the class "Files" of HashMap into the JTable. Can anybody tell me what I'm doing wrong? I've basically been stuck for 3 days now and would really appreciate some help.

Was it helpful?

Solution 3

Ok Just found the solution:

private final Map<Files, String> list = new LinkedHashMap<Files,String>();

class MyTableModel extends AbstractTableModel {

private String[] columnNames = {"File","Duration","Status"};

public void addElement(String addfile,String addurr,String addstatus, String addpath) {             
Files f = new Files(addfile, addurr, addstatus);                
list.put(f, addpath); // edit
fireTableRowsInserted(list.size()-1, list.size()-1);   
}

@Override
public int getColumnCount() {
return columnNames.length;
}

@Override
public int getRowCount() {
return list.size();
}

@Override
public String getColumnName(int col) {
return columnNames[col];
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {

List<Entry<Files,String>> randAccess = new ArrayList<Entry<Files,String>>(list.entrySet());

switch (columnIndex) {
case 0:
return randAccess.get(rowIndex).getKey().getfiles();
case 1:
return randAccess.get(rowIndex).getKey().getduration();
case 2:
return randAccess.get(rowIndex).getKey().getstatus();
default:
throw new IndexOutOfBoundsException();
        }
    }
}

OTHER TIPS

There are many things wrong.

First of all, why are all the fields and methods static in Files. This means that a Files instance has in fact no state at all, and that all Files instance share the same files, duration and status. Thos fields and methods should certainly be instance fields and methods instead (i.e. you must remove the static modifier).

Then, your model implements getColumnCount() and getRowCount() by returning 0. This means that your table contains 0 row and 0 column. So I don't really see the point in using a table if you don't intend to have any value in it.

The getValueAt() method, on the other hand, implies that all the rows contain the same values, since you're returning the same values whatever the rowIndex argument contains.

Finally, You're saying you have a Map<Files, String>, but you don't say what the relationship between this map and the table should be. You don't use this map anywhere in your model, and since the code, as is, doesn't make sense, it's hard to guess what the map actually contains, and what the table should actually display.

I need a key/value pair.

For reference, EnvTableTest constructs an AbstractTableModel from an existing Map. It uses the map's keySet() for column zero, and it uses each key to obtain the value for that row.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top