如何在运行时添加j单选按钮

时间:2022-03-15 11:57:53

I am using Netbeans 7.1 and MySQL. I need 1 column in jtable which will contain jradiobutton and user can select any 1 row's jradiobutton Please refer the fig for detail. After selecting RB further processing will be done on jbutton click event.

我使用的是Netbeans 7.1和MySQL。我需要jtable中的1列,其中包含jradiobutton,用户可以选择任何1行的jradiobutton,请参考图中的细节。选择RB后,将对jbutton单击事件进行进一步处理。

如何在运行时添加j单选按钮

Here is the code -

这是密码-

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        final Object[] columnNames=new String[] {"Date","Flight Name","Departure Time","BC Seats Available","XC Seats Available","EC Seats Available"};
        DefaultTableModel dtm=new DefaultTableModel(columnNames,0);        
        String origin=jComboBox3.getSelectedItem().toString();
        String target=jComboBox4.getSelectedItem().toString();
        String fclass=jComboBox1.getSelectedItem().toString();
        String search = "";
        Date dt;

        //Economy Class Processing

        try
        {
           smt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
           rs = smt.executeQuery(sql);
           int i = 0;
           SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yyyy");
           boolean empty=true;
           String var1="", var2="", var3="", var4="", var5="";
           while(rs.next())
           {
               empty=false;
               var1=rs.getString(1);
               strdtver1=(String) sdf.format(rs.getDate(2));
               var2=Integer.toString(rs.getInt(3));
               var3=Integer.toString(rs.getInt(4));
               var4=Integer.toString(rs.getInt(5));
               var5=rs.getString(6);
               dtm.addRow(new Vector());
               dtm.setValueAt(strdtver1, i, 0);
               dtm.setValueAt(var1, i, 1);
               dtm.setValueAt(var5, i, 2);
               dtm.setValueAt(var2, i, 3);
               dtm.setValueAt(var3, i, 4);
               dtm.setValueAt(var4, i, 5);
               i++;

           }
           if(empty)
           {
               dtm.addRow(new Vector());
               strdtver2=(String) sdf.format(jDateChooser1.getDate());
               dtm.setValueAt(strdtver2, i, 0);
               dtm.setValueAt("No Flights", i, 1);
               dtm.setValueAt("No Flights", i, 2);
               dtm.setValueAt("0", i, 3);
               dtm.setValueAt("0", i, 4);
               dtm.setValueAt("0", i, 5);
           }
           jTable1.setModel(dtm);
           TableColumnModel m=jTable1.getColumnModel();
           TableColumn col=m.getColumn(3);
           TableColumn col1=m.getColumn(4);
           //List<TableColumn> removed=col;
           //removed.add(col);
           m.removeColumn(col);
           m.removeColumn(col1);
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
    }

I simply want to add JRadioButton and user can select any 1 row JRadioButto,

我只想添加JRadioButton用户可以选择任意一行JRadioButto,

Thank you all in advance for any suggestion and guidance

谢谢大家的建议和指导

1 个解决方案

#1


2  

Several alternatives are considered here, but you probably want the one due to @Guillaume Polet shown here. It uses a PropertyChangeEvent to enforce the single-selection-per-row property.

这里考虑了几个替代方案,但是您可能希望看到这里显示的@Guillaume Polet。它使用PropertyChangeEvent来强制执行单个selectionper -row属性。

#1


2  

Several alternatives are considered here, but you probably want the one due to @Guillaume Polet shown here. It uses a PropertyChangeEvent to enforce the single-selection-per-row property.

这里考虑了几个替代方案,但是您可能希望看到这里显示的@Guillaume Polet。它使用PropertyChangeEvent来强制执行单个selectionper -row属性。