0

this is my Controller (read data from model and assign these data to JTable object and MedalTableModel obejest in Class view).:

public class TableController { 

    private TableModel table_model;
    private TableView table_view;

    public TableController (TableModel table_model, TableView table_view) {            
        this.table_model = table_model;
        this.table_view = table_view;
    }

    public void getMedalData () {
        try {
            table_model.getCsvData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setFilePath (String file_path) {
        table_model.setFilePath(file_path);
    }
    /*
    public void addLineData (ArrayList<String> data_list) {     
        Object[] line_data = new Object[data_list.size()];          
        for (int index = 0; index < data_list.size(); index ++) {
            line_data[index] = data_list.get(index);
        }            
        table_view.getTableModel().addRow(line_data);
    }
    */
    public void addTableData (String file_path) {            
        Object[] col_names = {"Country name", "Official medal ranking",
                "GDP rank", "Pop rank", "Team size"};            
        setFilePath(file_path);
        getMedalData();            
        int row_size = table_model.getRowSize();
        int col_size = table_model.getColumnSize();
        Object[][] table_data = new Object[row_size][col_size];            
        ArrayList<String> data_list = new ArrayList<String>();          
        for (int row_index = 0; row_index < row_size; row_index ++) {               
            data_list = table_model.getLineData(row_index);
            for (int col_index = 0; col_index < col_size; col_index ++) {                   
                table_data[row_index][col_index] = data_list.get(col_index);          
            }
        }           
        //table_view.setTableData(table_data);
        JTable temp_table = new JTable (table_data, col_names);
        table_view.setTable(temp_table);
        MedalTableModel temp_model = new MedalTableModel(table_data, col_names);
        table_view.setTableModel(temp_model);    
    }        
}

and this is my view:

public class TableView extends JFrame{

    //private DefaultTableModel def_model;
    private MedalTableModel mtable_model;
    private JTable medal_table;
    private JButton close_button;
    private TableRowSorter sorter;
    private Object[][] table_data;

    /*
    public DefaultTableModel getTableModel () {
        return def_model;
    }

    // maybe do not need this method
    public void setTableModel (DefaultTableModel table_model) {
        this.def_model = table_model;
    }
    */
    public TableView(String frame_title) {
        super(frame_title);         
        medal_table = new JTable();         
        JScrollPane scroll = new JScrollPane(medal_table);          
        JPanel button_panel = new JPanel();
        close_button = new JButton("Close");      
        button_panel.add(close_button);
        getContentPane().add(scroll, BorderLayout.CENTER);
        getContentPane().add(button_panel, BorderLayout.SOUTH);
        pack();         
    }

    public void setTableData (Object[][] table_data) {
        this.table_data = table_data;
    }

    public Object[][] getTabledata () {
        return table_data;
    }

    public MedalTableModel getTableModel () {
        return mtable_model;
    }

    // maybe do not need this method
    public void setTableModel (MedalTableModel table_model) {
        this.mtable_model = table_model;
    }

    public void setTable (JTable table) {
        this.medal_table = table;
    }

    public void setTableModel () {
        //add data here
        medal_table.setModel(mtable_model);
    }

    // set sort feature should be added after setModel
    public void setSortMtd () {         
        //sorter = new TableRowSorter<DefaultTableModel>(def_model);            
         medal_table.setAutoCreateRowSorter(false);         
         TableRowSorter trs = new TableRowSorter(mtable_model);    
         class IntComparator implements Comparator {
             public int compare(Object o1, Object o2) {
                 Integer int1 = (Integer)o1;
                 Integer int2 = (Integer)o2;
                 return int1.compareTo(int2);
             }

             public boolean equals(Object o2) {
                 return this.equals(o2);
             }
         }    
         trs.setComparator(1, new IntComparator());
         trs.setComparator(2, new IntComparator());             
         medal_table.setRowSorter(trs);             
         medal_table.setModel(mtable_model);
    }
}

here is my MedalTableModel extends DefaultTableModel;

public class MedalTableModel extends DefaultTableModel {
    public MedalTableModel (Object[][] table_data, Object[] col_names) {
        super(table_data, col_names);
    }
        public MedalTableModel() {
        // TODO Auto-generated constructor stub
    }

    Class[] types = { String.class, Integer.class, Integer.class,
            Integer.class, Integer.class };

    //String[] ColumnName = {"Country name", "Official medal ranking",
    //        "GDP rank", "Pop rank", "Team size"};

    @Override
    public Class getColumnClass (int columnIndex) {
        return this.types[columnIndex];
    }

    /*
    @Override
    public String getColumnName(int index) {
        return ColumnName[index];
    }
    */
    /*
    @Override  
    public Class getColumnClass(int col) {  

        //zero column accepts only String values
        if (col > 0) {
            return Integer.class;   
        }       
        else {
            return String.class; 
        }
    }  
    */
    @Override  
    public boolean isCellEditable(int row, int col) {  

        return false;  
    }  
}

and here is the main method (in another file): (initial model, controller and view)

public static void main( String[] args ) {

    SwingUtilities.invokeLater(new Runnable() {

        public void run () {                
            String file_path = "London2012OlympicAlternativeMedalRankingsALL.csv";
            TableModel table_model = new TableModel(file_path);
            TableView table_view = new TableView("Medal Table");    
            TableController table_contrl = new TableController(table_model,
                    table_view);
            table_contrl.addTableData(file_path);               
            table_view.setSortMtd();
            table_view.setVisible(true);
            table_view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
        }
    });         
    //System.out.println( "Hello World!" );
}

The problem is that when i try to run this program, it shows nothing in the JFrame. What is the problem of my code? Thanks very much.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
chygo
  • 366
  • 2
  • 6
  • 16
  • 1
    why your code is in *italics*? Why did you mentioned `DefaultTableModel` since there is no reference to it in provided code? Provide your table model code as well. Finally, I thought that JTable is already designed in MVC pattern and there is no need to wraping it into MVC again. – Antoniossss Oct 30 '13 at 13:04
  • 2
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Oct 30 '13 at 13:06
  • i have edited my question, i think now it shows the normal font :P – chygo Oct 30 '13 at 13:12
  • What is `TableModel` ? – nachokk Oct 30 '13 at 13:41
  • It is just a class to read data from a csv file. I thought it is not an important class (via debugging, I can see the reading process is correct), so in order to avoid distract your attention from the problem, i didn't upload it. – chygo Oct 30 '13 at 13:50

1 Answers1

2

You never actually set the TableModel on the JTable. You have a variable for it which seems unnecessary and is possibly why it isn't obvious that you aren't setting it on the JTable object.

It looks like you have a method to set it:

public void setTableModel () {
    //add data here
    medal_table.setModel(mtable_model);
}

But that method is never called.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • Hi,jzd, I called setModel() in the Class view: public void setSortMtd (). Thanks for answering – chygo Oct 30 '13 at 13:23
  • Yes, but you never call setSortMtd. – jzd Oct 30 '13 at 13:24
  • I call it in the main method: table_view.setSortMtd(); :) – chygo Oct 30 '13 at 13:29
  • I tried to look at the code but it looks like you are using a strange TableModel class that you didn't include in your post. What you really need in your question is an SSCCE that demonstrates the problem as what you have posted is not complete, doesn't compile, and has a lot of extra junk. – jzd Oct 30 '13 at 13:48
  • My "TableModel" is a totally different class, coz it is my first time to program with swing and JTable, when I named my model class, I didn't know that there exits a TableModel in the library... And this is my first time to try to post a question at StackOverFlow, I am sorry for the errors in my editing – chygo Oct 30 '13 at 14:08