1

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.

enter image description here

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,

Thank you all in advance for any suggestion and guidance

Community
  • 1
  • 1
user2241865
  • 113
  • 4
  • 14
  • 3
    You are posting a half-ton of code, 99% of which has no bearing on your question. Please don't make our volunteer help so difficult to do. Please consider putting in the effort of creating an [sscce](http://sscce.org) so we don't have to. – Hovercraft Full Of Eels Jun 16 '13 at 20:49
  • 1
    The solution is to change the JTable's TableModel, to give it another column that holds Boolean values, but don't ask me to try to manipulate your code to show you how this might be done, not until the code has been cleaned and pared down. – Hovercraft Full Of Eels Jun 16 '13 at 20:50
  • Very Sorry for long code Actually I am trying to add JRadiobutton since last 5-6 hours and now I'm getting frustrated. I cut down my code and only the table portion is submitted in post. – user2241865 Jun 16 '13 at 20:59
  • @HovercraftFullOfEels Code is cut down Now can I expect some help – user2241865 Jun 16 '13 at 21:06
  • 2
    `"Now can I expect some help"` -- please understand that we're volunteers. Hopefully you will get help, and usually the better the question, the better your chances. As for me, I'm waiting for you to read the link that I supplied in my first comment, and then for your SSCCE. – Hovercraft Full Of Eels Jun 16 '13 at 21:20
  • 1
    [crossposted](http://www.daniweb.com/software-development/java/threads/456797/how-to-add-jradio-button-at-run-time) – mKorbel Jun 17 '13 at 06:09
  • [another crosspost](https://forums.oracle.com/thread/2551252) – mKorbel Jun 18 '13 at 11:11

1 Answers1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I able to get the JRadioButton in JTable but I am getting only 1 record even if my SQL query returns more than 1 record. I just need 1 help HOW CAN CHANGE THE VALUE OF variable in this piece of code because it is inside while(rs.next) for fetching data from rs. So this all variables are overwritten by the next data and only last data show in `jTable1.setModel(dtm);` - `dtm.setDataVector(new Object[][]{ {strdtver1,var1,var5,var2,var3,var4,new JRadioButton("A")}}, new Object[]{"String","String","String","String","String","String","Select"});` – user2241865 Jun 17 '13 at 15:32
  • I'd expect your `TableModel` to use `Boolean.class`, as shown in the example cited. Please update your question to include an [sscce](http://sscce.org/) that exhibits any problem you encounter. – trashgod Jun 17 '13 at 16:56
  • @trashgodCan you please so some code snippet I am not getting your advice. – user2241865 Jun 18 '13 at 09:11
  • This recent [example](http://stackoverflow.com/a/17164751/230513) compares two default renderers with a custom renderer. The latter is specified by column; the former are chosen by type. – trashgod Jun 18 '13 at 09:21