如何使我的 JTable 可编辑?

How do I make my JTable editable?

按原样使用代码,我无法编辑 JTable 中的值。我需要能够 check/uncheck 复选框。我还需要能够从复选框中提取值,以查看哪些已选中,哪些已选中。

编辑:我发现我需要添加:

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

但我仍然无法单击数组中的复选框 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();
               }
          });
     }
}

您只需要覆盖 table 模型中的 isCellEditable

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

更新

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

您需要覆盖 setValueAt 并实际更改保存数据的值(即您的数组列表)。 table 是根据 ArrayList 中的值呈现的。如果您从不更改值,则渲染将永远不会更改

@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);
}

以上只设置了boolean列中的值。我会把它作为一个项目留给你来设置剩余的值:-)