質問

i'm workin on programe and i need to get the selected radio buttom from a Jtable

I found an exemple that i'm workin on

there is to class

the 1st :

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

and the 2nd class is :

     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);
      }
    });
  }*/
}

in the 2nd class i'm trying to add all the radio button on ButtonGroup like this :

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

and my problem is that i can't get selected RadioButton :

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

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

i alwayse get nullll

any help plz !!

役に立ちましたか?

解決

Read my answer to this question. You shouldn't store components in a table. Store booleans. You should then configure a renderer to display the boolean as a radio button, and an editor to change the value of the checked cell as well as the value of the previously checked one. And understand that the same instance of renderer is used to render/edit all the cells of the same class.

他のヒント

  • I think that not easy job, TableCellEditor for JRadioButtons in the ButtonGroup

  • use JCheckBox if is possible

  • use JComboBox as TableCellEditor instead of JRadioButtons in the ButtonGroup if is possible, for TableCellRenderer you can use JRadioButtons in the ButtonGroup

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top