0

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Omid
  • 823
  • 1
  • 11
  • 31

3 Answers3

1

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks for your input. pretty new to Java. So I want the 3 variables in files (Files, Duration and Status) to be displayed as three columns in the JTable. – Omid Apr 06 '13 at 12:32
  • And what's the relationship with the map? Do you want to display a single file in the table, which would thus have 1 row and 3 columns? Have you started fixing your code based on the indications I already gave you? – JB Nizet Apr 06 '13 at 12:44
  • yes, 1 row and 3 columns. But as soon as values are added in the map they would also be added to the JTable. Yes I removed all static. – Omid Apr 06 '13 at 12:47
  • A Map can't be observed. You'll have to manually add an element to your table model each time you add it to the map. Use a List to store the rows of your table model, and implement getRowCount() and getValueAt() by getting the size and the element at the given row index respectively. Read http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data – JB Nizet Apr 06 '13 at 12:55
  • agreed with List instead of XxxMap, I'd to suggest to start with DefaultTableModel based on Vector == `pretty new to Java` – mKorbel Apr 06 '13 at 13:14
  • but I need a keyvaluepair system so I can retrieve a value for each list item. – Omid Apr 06 '13 at 13:15
  • 1
    @mKorbel: I agree on the convenience of `DefaultTableModel`, but `AbstractTableModel` may allow less copying of data. – trashgod Apr 06 '13 at 14:23
  • 2
    @trashgod this abstraction isn't simple for understanding, yours and JB Nizets knowledges and experiences are so far ..., :-) – mKorbel Apr 06 '13 at 14:43
  • I am close so solving this, will post it soon – Omid Apr 06 '13 at 14:45
1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

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();
        }
    }
}
Greg Jennings
  • 1,611
  • 16
  • 25
Omid
  • 823
  • 1
  • 11
  • 31