java - 如何使 JTable 可编辑?

标签 java jtable

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

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

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

最佳答案

您只需要覆盖 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并实际更改保存数据的值(即您的数组列表)。该表是根据 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 列中的值。我会将其保留为一个项目,供您设置其余值:-)

关于java - 如何使 JTable 可编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28502933/

相关文章:

java - 收集字段警告的返回 - setter 和构造函数

java - JBPM6 安装问题 - Jboss 服务器和 eclipse 未打开

java - 我如何用空行对 java JTable 进行排序并强制空行始终排在最后?

java - JTable 保持模型的颜色而不是行的颜色

java - eclipse 在启动时崩溃

java - 当 false 不是 XML 中的字段时,XQuery 返回 "false"?

java - Spring 3.0 MVC 数据绑定(bind)与 HTTP PUT

java - 将项目添加到 JTable 的数组中

java - 将图标添加到 jTable 的不同行中

java - JScrollPane 达到滚动限制事件