2012-01-05 72 views
2

JCheckBox被選中,但是當我使用system.out.print時,它仍然顯示爲false。如果焦點丟失,但仍然檢查JCheckBox,則返回true。 即,當我檢查2複選框時,結果顯示爲第一個複選框。第二個複選框的狀態未顯示。完整的編程如下所示:請運行編,並糾正我的錯誤。任何幫助是受歡迎的。JTable中的JCheckBox值

public class check extends JFrame { 

    public check() { 
     setBounds(00, 40, 400, 400); 
     Color c = new Color(160, 200, 100); 
     getContentPane().setBackground(c); 
     Color c3 = new Color(0, 50, 50, 2); 
     setTitle("MARKING OF TARGET HABITATION"); 
     setUndecorated(true); 
     getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     String[] columnNames = {"Country", "Capital", "Population in Millions", 
      "Democracy"}; 
     Object[][] data = { 
      {"USA", "Washington DC", 280, new Boolean(false)}, 
      {"Canada", "Ottawa", 32, new Boolean(false)}, 
      {"United Kingdom", "London", 60, new Boolean(false)}, 
      {"Germany", "Berlin", 83, new Boolean(false)}, 
      {"France", "Paris", 60, new Boolean(false)}, 
      {"Norway", "Oslo", 4.5, new Boolean(false)}, 
      {"India", "New Deli", 1046, new Boolean(false)} 
     }; 

     final JTable table = new JTable(data, columnNames) { 

      public boolean isCellEditable(int rowIndex, int colIndex) { 
       return (colIndex == 3);//Disallow the editing of any cell 
      } 
     }; 
     table.getColumnModel().getColumn(3).setCellEditor(new CheckBoxCellEditor()); 
     table.getColumnModel().getColumn(3).setCellRenderer(new CWCheckBoxRenderer()); 
     JScrollPane scrollPane = new JScrollPane(table); 
     JButton button = new JButton("check"); 
     button.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       boolean[] rsel = new boolean[table.getRowCount()]; 
       for (int i = 0; i < table.getRowCount(); i++) { 
        rsel[i] = (boolean) table.getValueAt(i, 3); 
        if (rsel[i]) { 
         System.out.println("checkbox selected at row" 
          + " " + i + "is" + rsel[i]); 
         for (int col = 0; col <= 3; col++) { 
          table.getValueAt(i, col); 
          System.out.println("data at row" + " " 
           + i + "is" + table.getValueAt(i, col)); 
         } 
        } 
       } 
      } 
     }); 
     JPanel buttonpanel = new JPanel(); 
     buttonpanel.add(button); 
     add(scrollPane, BorderLayout.CENTER); 
     add(buttonpanel, BorderLayout.SOUTH); 
     Color c1 = new Color(160, 200, 100); 
     table.setBackground(c1); 
     buttonpanel.setBackground(c1); 
     setBackground(c1); 
     setVisible(true); 
    } 

    public static void main(String args[]) { 
     new check(); 

    } 

    class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor { 

     private static final long serialVersionUID = 1L; 
     protected JCheckBox checkBox; 

     public CheckBoxCellEditor() { 
      checkBox = new JCheckBox(); 
      checkBox.setHorizontalAlignment(SwingConstants.CENTER); 
      // checkBox.setBackground(Color.white); 
     } 

     public Component getTableCellEditorComponent(
      JTable table, 
      Object value, 
      boolean isSelected, 
      int row, 
      int column) { 
      checkBox.setSelected(((Boolean) value).booleanValue()); 
      return checkBox; 

     } 

     public Object getCellEditorValue() { 
      return Boolean.valueOf(checkBox.isSelected()); 
     } 
    } 

    class CWCheckBoxRenderer extends JCheckBox implements TableCellRenderer { 

     private static final long serialVersionUID = 1L; 
     Border border = new EmptyBorder(1, 2, 1, 2); 

     public CWCheckBoxRenderer() { 
      super(); 
      setOpaque(true); 
      setHorizontalAlignment(SwingConstants.CENTER); 
     } 

     public Component getTableCellRendererComponent(
      JTable table, 
      Object value, 
      boolean isSelected, 
      boolean hasFocus, 
      int row, 
      int column) { 
      if (value instanceof Boolean) { 
       setSelected(((Boolean) value).booleanValue()); 
       //setEnabled(table.isCellEditable(row, column)); 
       setForeground(table.getForeground()); 
       setBackground(table.getBackground()); 

      } 
      return this; 
     } 
    } 
} 
+0

請提供[SSCCE](http://pscode.org/sscce.html)以獲得幫助。編輯器/渲染器本身不足以調查問題。 – Howard 2012-01-05 07:57:07

+0

你也可以看看[這裏](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html),其中也有一個你想要達到的相同行爲的例子。 – Howard 2012-01-05 08:00:13

+0

從重做您的支票開始。我不確定應該做什麼,但我懷疑它在做你認爲它做的事情。你使用npt指定的變量列,yoo只測試連接到變量列的布爾值是否設置爲true。 – Peter 2012-01-05 08:18:27

回答

5

也許最好重寫模型中的getColumnClass以返回布爾值。默認的渲染器/編輯器處理大小寫,你可以看到/使用複選框。

+0

那裏沒有理由 – mKorbel 2012-01-05 08:03:24

+0

@mKorbel - 斯坦是現貨,所以你的發現:-) – kleopatra 2012-01-05 09:36:17

+0

+1有一個示例[這裏](http://stackoverflow.com/a/7519403/230513)。順便說一下,我認爲mKorbel同意StanislavL。 – trashgod 2012-01-05 10:43:17

4

作爲參考,這sscce說明@斯坦尼斯拉夫的觀點。請注意,在Java 5中引入的autoboxing導致第三列的類型爲Boolean。在運行時,重寫的getColumnClass()將返回Boolean.class索引CHECK_COL。最後,Swing GUI對象應該在event dispatch thread上構建。

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.table.DefaultTableModel; 

public class Check extends JFrame { 

    private static final int CHECK_COL = 3; 

    public Check() { 
     setTitle("MARKING OF TARGET HABITATION"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     String[] columnNames = { 
      "Country", "Capital", "Population in Millions", "Democracy"}; 
     Object[][] data = { 
      {"USA", "Washington DC", 280, true}, 
      {"Canada", "Ottawa", 32, false}, 
      {"United Kingdom", "London", 60, false}, 
      {"Germany", "Berlin", 83, false}, 
      {"France", "Paris", 60, false}, 
      {"Norway", "Oslo", 4.5, false}, 
      {"India", "New Deli", 1046, false} 
     }; 
     DefaultTableModel dtm = new DefaultTableModel(data, columnNames) { 

      @Override 
      public Class getColumnClass(int col) { 
       return getValueAt(0, col).getClass(); 
      } 

      @Override 
      public boolean isCellEditable(int rowIndex, int colIndex) { 
       return (colIndex == CHECK_COL); 
      } 
     }; 
     final JTable table = new JTable(dtm); 
     JScrollPane scrollPane = new JScrollPane(table); 
     JButton button = new JButton("check"); 
     button.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       for (int row = 0; row < table.getRowCount(); row++) { 
        Boolean b = ((Boolean) table.getValueAt(row, CHECK_COL)); 
        if (b.booleanValue()) { 
         System.out.print("row " + row + " is " + b + ": "); 
         for (int col = 0; col < table.getColumnCount(); col++) { 
          System.out.print(table.getValueAt(row, col) + " "); 
         } 
         System.out.println(); 
        } 
       } 
      } 
     }); 
     JPanel buttonpanel = new JPanel(); 
     buttonpanel.add(button); 
     add(scrollPane, BorderLayout.CENTER); 
     add(buttonpanel, BorderLayout.SOUTH); 
     pack(); 
     setLocationByPlatform(true); 
     setVisible(true); 
    } 

    public static void main(String args[]) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new Check(); 
      } 
     }); 
    } 
} 
+0

謝謝,我的問題解決了。我不知道我爲什麼翻找checkboxcelleditor和渲染器。 – user1101703 2012-01-06 09:10:23