Ducksauce88
Ducksauce88

Reputation: 650

How do I make my JTable editable?

Using the code as is, I can't edit the values in the JTable. I need to be able to check/uncheck the checkboxes. I also need to be able to pull the values from the check boxes to see which are checked and which are.

Edit: I found I needed to add:

          public boolean isCellEditable(int row, int col) {
          return true;
      }

But I still can't click on the checkboxes from my array new Boolean(true)

public class Main extends JPanel {
     private boolean DEBUG = false;

     public Main() {
          super(new GridLayout(1, 0));

          ArrayList<MyObject> list = new ArrayList<MyObject>();
          list.add(new MyObject("Kathy", "Smith", "Snowboarding", new Integer(5),
                    new Boolean(false)));
          list.add(new MyObject("John", "Doe", "Rowing", new Integer(3),
                    new Boolean(true)));
          list.add(new MyObject("Sue", "Black", "Knitting", new Integer(2),
                    new Boolean(false)));
          list.add(new MyObject("Jane", "White", "Speed reading",
                    new Integer(20), new Boolean(true)));
          JTable table = new JTable(new MyTableModel(list));
          table.setPreferredScrollableViewportSize(new Dimension(500, 70));
          table.setFillsViewportHeight(true);
          // Create the scroll pane and add the table to it.
          JScrollPane scrollPane = new JScrollPane(table);
          // Add the scroll pane to this panel.
          add(scrollPane);
     }

     class MyObject {
          String firstName;
          String lastName;
          String sport;
          int years;
          boolean isVeg;

          MyObject(String firstName, String lastName, String sport, int years,
                    boolean isVeg) {
               this.firstName = firstName;
               this.lastName = lastName;
               this.sport = sport;
               this.years = years;
               this.isVeg = isVeg;
          }
     }

     class MyTableModel extends AbstractTableModel {
          private String[] columnNames = { "First Name", "Last Name", "Sport",
                    "# of Years", "Vegetarian" };
          ArrayList<MyObject> list = null;

          MyTableModel(ArrayList<MyObject> list) {
               this.list = list;
          }

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

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

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

          public Object getValueAt(int row, int col) {

               MyObject object = list.get(row);

               switch (col) {
               case 0:
                    return object.firstName;
               case 1:
                    return object.lastName;
               case 2:
                    return object.sport;
               case 3:
                    return object.years;
               case 4:
                    return object.isVeg;
               default:
                    return "unknown";
               }
          }

          public Class getColumnClass(int c) {
               return getValueAt(0, c).getClass();
          }
     }

     /**
      * Create the GUI and show it. For thread safety, this method should be
      * invoked from the event-dispatching thread.
      */
     private static void createAndShowGUI() {
          // Create and set up the window.
          JFrame frame = new JFrame("TableDemo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          // Create and set up the content pane.
          Main newContentPane = new Main();
          newContentPane.setOpaque(true); // content panes must be opaque
          frame.setContentPane(newContentPane);

          // Display the window.
          frame.pack();
          frame.setVisible(true);
     }

     public static void main(String[] args) {
          // Schedule a job for the event-dispatching thread:
          // creating and showing this application's GUI.
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                    createAndShowGUI();
               }
          });
     }
}

Upvotes: 2

Views: 65

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209122

You just need to override isCellEditable in the table model

@Override
public boolean isCellEditable(int row, int col) {
    return true;
}

UPDATE

I literally figured that out as you posted this. However I still can't select/deselect the last column full of checkboxes

You need to override setValueAt and actually change the values that hold the data (i.e. Your arraylist). The table is rendered based on the values in the ArrayList. If you never change the values, then the rendering will never change

@Override
public void setValueAt(Object value, int row, int col) {
    MyObject obj = list.get(row);
    if (col == 4) {
        obj.isVeg = (Boolean)value;
    }
    fireTableCellUpdated(row, col);
}

The above only set the value in the boolean column. I'll leave it as a project for you to set the remaining values :-)

Upvotes: 2

Related Questions