如何使JTable可编辑?

时间:2015-02-13 15:27:03

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

1 个答案:

答案 0 :(得分:2)

您只需要覆盖表格模型中的isCellEditable

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

<强>更新

  

我确实认为你发布了这个。但是,我仍然无法选择/取消选中最后一列的复选框

您需要覆盖setValueAt并实际更改包含数据的值(即您的arraylist)。该表基于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);
}

以上仅在布尔列中设置值。我将其留作项目供您设置剩余值: - )