This discussion is archived
1 Reply Latest reply: Jun 18, 2013 4:17 AM by mKorbel RSS

How to add JRadioButton in JTable at Runtime

VishOnNet Newbie
Currently Being Moderated

Hello Friends

I new to this forum I tried 2-3 forum but not received any concrete answer.

I am using Netbeans 7.1 and MySQL

My problem is I want to add a column of JRadioButton in JTable. Now situation is I am poulating JTable with MySQL data at runtime depending on search condition. Please refer the figure for clear picture

Flight DateFlight No.Departure timeBusiness Class SeatsExecutive Class  SeatsEconomy Class SeatsSelect your Flight
20-06-2013AS25309:451080JRadioButton1
20-06-2013AS57621:45302010JRadioButton2

 

I am able to fetch the data and from MySQL and display them in JTable but how can I add the last column for JRadioButton which can be selected by user.

Number of rows may vary depending on the search result.

 

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","Select"};
        DefaultTableModel dtm=new DefaultTableModel(columnNames,0);     
.......
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);
               dtm.setValueAt(new JRadioButton("A"), i, 6);
               i++;
               
           }.........
          jTable1.setModel(dtm);
        }.....

 

Above code doesn't help me. I need some Good tutorial or proper guidance as I am not expert in Java.

 

I got something telling about cell render and cell editor

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

But how to link this with my dtm or jTable1

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points