如何从JTable中获取所选的单选按钮

时间:2012-05-28 07:21:35

标签: java swing jtable jradiobutton buttongroup

我正在编写程序,我需要从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

任何帮助PLZ !!

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:3)

  • 对于JRadioButtons in the ButtonGroup

  • ,我认为这不容易,TableCellEditor
  • 如果可能,请使用JCheckBox

  • JComboBox使用TableCellEditor代替JRadioButtons ButtonGroup,如果可能的话,对于TableCellRenderer,您可以在{JRadioButtons使用ButtonGroup 1}}