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.
64496cca6997d07cf995950d67a3e3f0

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

Recommended Answers

All 6 Replies

@mKorbel and @Meir I know I have crossposted this. But those answers are not what I want to achive if you see all the examples they all have fixed rows and columns of jtable. Where as in my case I don't know how many rows will be generated after search. I tried a lot after getting those links but confused

dm.setDataVector(
      new Object[][]{
        {"Group 1","GRP 1.1","GRP 1.2",new JRadioButton("A")},
        {"Group 1","GRP 1.1","GRP 1.2",new JRadioButton("B")},
        {"Group 1","GRP 1.1","GRP 1.2",new JRadioButton("C")},
        {"Group 2","GRP 1.1","GRP 1.2",new JRadioButton("a")},
        {"Group 2","GRP 1.1","GRP 1.2",new JRadioButton("b")}},
      new Object[]{"String","String","String","JRadioButton"});

I got this code from this website Click Here
Now suggest me how can I convert my code

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

to the code given in example.

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

dtm.setDataVector(new Object[][]{
        {strdtver1,var1,var5,var2,var3,var4,new JRadioButton("A")}},
      new Object[]{"String","String","String","String","String","String","Select"});

jTable1.setModel(dtm);
jTable1.getColumn("Select").setCellRenderer(new RadioButtonRenderer());
jTable1.getColumn("Select").setCellEditor(new RadioButtonEditor(new JCheckBox()));
  • never to put JComponents into JTable, XxxTableModel would be contains only true/false not JCheckBox

  • then array could be only strdtver1,var1,var5,var2,var3,var4,true/false

  • answers (linked too) on another forum contains all requirements inc. reasonable description

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.