Results 1 to 2 of 2
  1. #1
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default JTable questions

    I have created my own JTable model to use to display the data of my objects, but when I test it, the column names are not shown in the GUI

    Also, can I get some guidance on what to look for, in order to make the cells editable , so that it saves the data of the objects.

    Java Code:
    import javax.swing.table.AbstractTableModel;
    import java.util.*;
    import appLogic.*;
    
    @SuppressWarnings("serial")
    public class TableModelGUI extends AbstractTableModel {
        
        private List<Task> li = new ArrayList<Task>();
        private String[] columnNames = { "Task", "Start Date", "Due Date",
                    "Est.time needed", "Hours worked", "Priority", "Category", "Completed"};
    
        public TableModelGUI(List<Task> list){
             this.li = list;
        }
    
        @Override
        public String getColumnName(int columnIndex){
             return columnNames[columnIndex];
        }
    
        @Override     
        public int getRowCount() {
            return li.size();
        }
    
        @Override        
        public int getColumnCount() {
            return 8; 
        }
        
        @Override
        public boolean isCellEditable(int row, int col) {
            return (col != 0);
        }
    
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Task t = li.get(rowIndex);
            switch (columnIndex) {
                case 0: 
                    return t.getTitle();
                case 1:
                    return t.getStartDate().toString();
                case 2:
                    return t.getDueDate().toString();
                case 3:
                    return t.getEstimatedHours();
                case 4:
                    return t.getHoursWorked();
                case 5:
                    return t.getPriority();
                case 6:
                    return t.getCategory();
                case 7:
            	return t.isCompleted();
               }
               return null;
       }
    
       @Override
       public Class<?> getColumnClass(int columnIndex){
              switch (columnIndex) {
                 case 0:
                   return String.class;
                 case 1:
                   return String.class;
                 case 2:
                   return String.class;
                 case 3:
                   return Integer.class;
                 case 4:
                   return Integer.class;
                 case 5:
                   return Integer.class;
                 case 6:
                   return String.class;
                 case 7:
            	 return Boolean.class;
                 }
                 return null;
          }
     }

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: JTable questions

    Fixed

    I had to add a JScrollPanel


    Java Code:
    viewTasksPanel.add(new JScrollPane(table));

Similar Threads

  1. Jtable inside Jtable
    By anchitanc in forum AWT / Swing
    Replies: 5
    Last Post: 05-26-2012, 08:10 PM
  2. Replies: 4
    Last Post: 04-02-2012, 07:57 PM
  3. Need help with some questions
    By El_Davidos in forum New To Java
    Replies: 5
    Last Post: 11-23-2010, 10:13 AM
  4. Adding New JTable in JTable
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 01-27-2010, 08:27 AM
  5. Replies: 2
    Last Post: 05-15-2008, 10:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •