Add a title to a JTable

前端 未结 4 1320
醉酒成梦
醉酒成梦 2021-01-19 07:48

I have a JTable which is created using a TableModel JTable t = new JTable(tableModel) I want to add a title to it. I was hoping for something like t.setTi

4条回答
  •  星月不相逢
    2021-01-19 08:05

    You would have to add it when you instantiate your DefaultTableModel:

    String data[][] = {{"Vinod","MCA","Computer"},
    {"Deepak","PGDCA","History"},
    {"Ranjan","M.SC.","Biology"},
    {"Radha","BCA","Computer"}};
    
    String col[] = {"Name","Course","Subject"};
    
    DefaultTableModel model = new DefaultTableModel(data,col);
    table = new JTable(model);
    

    If it already exists, you can do something like this:

    ChangeName(table,0,"Stu_name");
    ChangeName(table,2,"Paper");
    
    public void ChangeName(JTable table, int col_index, String col_name){
        table.getColumnModel().getColumn(col_index).setHeaderValue(col_name);
    }
    

    Courtesy of RoseIndia.net

    Hope that helps.

提交回复
热议问题