1

我正在处理程序,我需要从 Jtable 中获取选定的单选按钮

我找到了一个我正在研究的例子

有课

第一个:

  import java.awt.Component;
  import java.awt.event.ItemEvent;
  import java.awt.event.ItemListener;

 import javax.swing.DefaultCellEditor;
 import javax.swing.JCheckBox;
 import javax.swing.JRadioButton;
 import javax.swing.JTable;
 import javax.swing.table.TableCellRenderer;

 class RadioButtonRenderer implements TableCellRenderer {
   public Component getTableCellRendererComponent(JTable table, Object value,
       boolean isSelected, boolean hasFocus, int row, int column) {
     if (value == null)
       return null;
     return (Component) value;
   }
 }

 class RadioButtonEditor extends DefaultCellEditor implements ItemListener {
   /**
     * 
      */
     private static final long serialVersionUID = 1L;
 private JRadioButton button;

   public RadioButtonEditor(JCheckBox checkBox) {
     super(checkBox);
   }

   public Component getTableCellEditorComponent(JTable table, Object value,
       boolean isSelected, int row, int column) {
     if (value == null)
       return null;
     button = (JRadioButton) value;
     button.addItemListener(this);
          return (Component) value;
   }

   public Object getCellEditorValue() {
     button.removeItemListener(this);
     return button;
   }

   public void itemStateChanged(ItemEvent e) {
     super.fireEditingStopped();
   }
 }

第二类是:

     package TP2;
     import java.awt.event.ActionEvent;
              import java.awt.event.ActionListener;
     import java.awt.event.ItemEvent;

     import javax.swing.ButtonGroup;
     import javax.swing.ButtonModel;
     import javax.swing.JButton;
     import javax.swing.JCheckBox;
     import javax.swing.JFrame;
     import javax.swing.JPanel;
     import javax.swing.JRadioButton;
     import javax.swing.JScrollPane;
     import javax.swing.JTable;
     import javax.swing.JToggleButton;
     import javax.swing.event.TableModelEvent;
     import javax.swing.table.DefaultTableModel;

     import net.miginfocom.swing.MigLayout;

     public class tablesDesEtudiants extends JFrame {

           public tablesDesEtudiants(Object[][] objt) {
             super("List des etudiants");

           //  UIDefaults ui = UIManager.getLookAndFeel().getDefaults();

            // UIManager.put("RadioButton.focus", ui.getColor("control"));

             DefaultTableModel dm = new DefaultTableModel();
             dm.setDataVector(objt , new Object[]                           {"Nom","Prénom","Année","Succs","Type","Select"});

             JTable table = new JTable(dm) {
               public void tableChanged(TableModelEvent e) {
                 super.tableChanged(e);
                 repaint();
      }
    };
    final ButtonGroup group1 = new ButtonGroup();
    for(int i =0 ; i<objt.length;i++){
    if(objt[i][1]!=null)

        group1.add((JRadioButton) dm.getValueAt(i, 5));
    }
   // System.out.println(objt.length);

    table.getColumn("Select").setCellRenderer(new RadioButtonRenderer());
    table.getColumn("Select").setCellRenderer(new RadioButtonRenderer());
    table.getColumn("Select").setCellRenderer(new RadioButtonRenderer());
    table.getColumn("Select").setCellRenderer(new RadioButtonRenderer());
    table.getColumn("Select").setCellRenderer(new RadioButtonRenderer());
    table.getColumn("Select").setCellEditor(new RadioButtonEditor(new JCheckBox()));




   JScrollPane scroll = new JScrollPane(table);

   JButton bView = new JButton("View");
   bView.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


            ButtonModel choix   = group1.getSelection();
            if (choix != null) {

            System.out.println( choix.getActionCommand());
                            }
            else System.out.println("nullll");
        }
    });


   JPanel  pp =new JPanel();
   MigLayout layout = new MigLayout(

          "",           // Layout Constraints
          "[][]20[]",   // Column constraints
          "[]20[]");    // Row constraint


      pp.setLayout(layout);
      pp.add(scroll,"cell 1 2");

      pp.add(bView,"cell 2 3");
   getContentPane().add(pp);


    setSize(600, 400);
    setVisible(true);


  }


 /* public static void main(String[] args) {
    JRadioButtonTableExample frame = new JRadioButtonTableExample();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }*/
}

在第二堂课中,我试图在 ButtonGroup 上添加所有单选按钮,如下所示:

   final ButtonGroup group1 = new ButtonGroup();
    for(int i =0 ; i<objt.length;i++){
    if(objt[i][1]!=null)

        group1.add((JRadioButton) dm.getValueAt(i, 5));
    }

我的问题是我无法选择 RadioButton :

         ButtonModel choix  = group1.getSelection();
            if (choix != null) {

            System.out.println( choix.getActionCommand());
                            }
            else System.out.println("nullll");
        }

我总是得到nullll

任何帮助请!

4

2 回答 2

5

阅读我对这个问题的回答。您不应该将组件存储在表中。存储布尔值。然后,您应该配置一个渲染器以将布尔值显示为单选按钮,并配置一个编辑器来更改选中单元格的值以及先前选中的单元格的值。并了解渲染器的相同实例用于渲染/编辑同一类的所有单元格。

于 2012-05-28T07:25:00.253 回答
3
  • 我认为这并不容易,对于 ButtonGroup 中TableCellEditorJRadioButtons

  • JCheckBox尽可能使用

  • 使用JComboBoxasTableCellEditor而不是JRadioButtonsButtonGroupif 是可能的,对于 TableCellRenderer 你可以JRadioButtonsButtonGroup

于 2012-05-28T07:24:00.347 回答