1
import javax.swing.*;
import javax.swing.event.*; 
import java.awt.*;
import java.awt.event.*;
import net.java.dev.designgridlayout.DesignGridLayout;
import java.sql.*;

class databaseprob 
{
JFrame JF;
Container C,C1;
JDesktopPane JDP;
JInternalFrame JIF5;
JLabel i1l1,i1l2;
JTextField i1t1;
JRadioButton i1r1,i1r2,i1r3,i1r4;
JButton i1b1,i1b2,i1b3;
JInternalFrame JIF1;
ButtonGroup i1bg;
String i1type;


public databaseprob()
{
JF = new JFrame();
JDP = new JDesktopPane();
JF.setVisible(true);
JF.pack();


    JIF1 = new JInternalFrame("Register",true,true, true, true);
C=JF.getContentPane();
C.add(JDP,BorderLayout.CENTER);
JIF1.setVisible(true);
JIF1.setBounds(10, 10, 600, 500); 
    C1 = JIF1.getContentPane();
DesignGridLayout layout = new DesignGridLayout(C1);



    i1l1 = new JLabel("Head ID : ");
    i1l2 = new JLabel("Type : ");

    i1t1 = new JTextField(10);


    i1bg = new ButtonGroup();
    ActionListener actionListener = new ActionListener() { @Override
        public void actionPerformed(ActionEvent e) {
                JRadioButton radioButton = (JRadioButton)e.getSource();
                i1type = radioButton.getText();
                System.out.println(i1type);
            }
        };
    i1r1 = new JRadioButton("Customer");
    i1r1.addActionListener(actionListener);
    i1bg.add(i1r1);

    i1r2 = new JRadioButton("Supplier");
    i1r2.addActionListener(actionListener);
    i1bg.add(i1r2);

    i1r3 = new JRadioButton("Staff");
    i1r3.addActionListener(actionListener);
    i1bg.add(i1r3);

    i1r4 = new JRadioButton("Others");
    i1r4.addActionListener(actionListener);
    i1bg.add(i1r4);






   i1b1 = new JButton("Save");
i1b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
    {
            try
        {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection i1conn =   DriverManager.getConnection("Jdbc:Odbc:TomsJava");

        int i1headID = Integer.parseInt(i1t1.getText());


        Statement i1stmt = i1conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        ResultSet i1rs = i1stmt.executeQuery("SELECT * FROM i1Register");
        i1rs.moveToInsertRow();
        i1rs.updateInt("Head_ID", i1headID);
        i1rs.updateString("Type",i1type);

        i1rs.insertRow();   
        i1stmt.close();
        i1rs.close();
        }       

    catch(SQLException e)
    {
    System.out.println(e);
    }
    catch(ClassNotFoundException z)
    {
    System.out.println(z);
    }
    catch(NumberFormatException n)
    {
    System.out.println(n);
    }
    }
});

   i1b2 = new JButton("Reset");
   i1b3 = new JButton("Close");



layout.row().grid(i1l1).add(i1t1);
layout.row().grid(i1l2).add(i1r1).add(i1r2).add(i1r3).add(i1r4);

layout.emptyRow();
layout.row().center().add(i1b1).add(i1b2).add(i1b3);

JDP.add(JIF1);
}
public static void main(String args[])
{
new databaseprob();
}
}

Seems this code is updating the database. Suggest me any more changes i should make. The variable declaration was creating the problem i guess, the String i1type was declared inside radiobutton and wasnt available for Save Button.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Toms
  • 239
  • 1
  • 5
  • 15
  • what the error you're getting ? – Salah Feb 27 '14 at 15:31
  • You don't add any buttons to the button group above, you don't appear to be getting the selection inside of an event, you appear to be checking if the **ButtonGroup** is selected(??) which doesn't make much sense ...Please post your real code, preferably a [small compilable runnable example program](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Feb 27 '14 at 15:37
  • use ItemListener ................ – mKorbel Feb 28 '14 at 08:46

3 Answers3

3

isSelected (you're missing some arguments) determines if any radiobutton is selected in the ButtonGroup.

You need getSelection

if (i1bg.getSelection() != null) {
   String i1type = i1bg.getSelection().getActionCommand();
   ...
}

For this to work the ActionCommand needs to be explicitly set for the radio button

Reimeus
  • 158,255
  • 15
  • 216
  • 276
2

You forgot to add your JRadioButtons to the ButtonGroup.

Hugo Sousa
  • 1,904
  • 2
  • 15
  • 28
2

My problem is that i am trying to retrieve value of radiobutton in String format.

In first place this line won't even compile:

String i1type = i1bg.isSelected();

Because:

  1. ButtonGroup.isSelected(ButtonModel model) requires a ButtonModel as argument.
  2. This method returns a boolean which you're trying to assig to a String variable.

You can achieve your goal by implementing an ActionListener as described in How to Use Buttons, Check Boxes, and Radio Buttons tutorial and attaching this action listener to your radio buttons. For instance:

ActionListener actionListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JRadioButton radioButton = (JRadioButton)e.getSource();
        String selectedOption = radioButton.getText();
        System.out.println(selectedOption );
    }
};

i1r1.addActionListener(actionListener);
i1r2.addActionListener(actionListener);
...

If the value you need and radio buttons text may differ then you can use putClientProperty() method (inherited from JComponent) as exemplified in this answer.

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • Thanks again dic19, I actually wanted to add the retrieved value from the buttongroup to a resultset. which when i try using rs.updateString("Type",i1type); gives me an error cannot find symbol i1type1. – Toms Feb 27 '14 at 18:01
  • @Toms you're welcome. Compiler is telling you there's no variable nor method called `i1type1`. The variable name is `i1type`, not `i1type1` (note the `1` at the end). – dic19 Feb 27 '14 at 18:45
  • 1
    oops, sorry i typed i1type in my code only... i am so stressed up of completing this project, its my last day tomorow and all things going wrong. I think i got the problem, the i1type was declared inside the radiobutton and it wasnt available to the save button. I'll Just post the code, suggest me if any more corrections. Thanks. – Toms Feb 27 '14 at 18:51
  • 1
    @Toms a variable scope problem. It usually happens :P My best wishes on your project! – dic19 Feb 27 '14 at 18:54
  • 1
    hehe.. thanks... i am only 60% done and still 40% percent remaining.. mind not working, as my first n last project of college is turning out to be very big than expected. – Toms Feb 27 '14 at 19:01