How to get value of selected radioButton of buttonGroup - java

How to get value of selected radioButton?
I tried using buttonGroup1.getSelection().getActionCommand() (as posted in some of answers here) but it is not working.
Also, i am temporarily using this code but i want to know is this a good practice or not?
//Consider that maleRButton and femaleRButton are two radioButtons of
//same buttonGroup
String getGender()
{
if(maleRButton.isSelected())
{
return "Male";
}
else if(femaleRButton.isSelected())
{
return "Female";
}
else
{
return null;
}
}

I tried using buttonGroup1.getSelection().getActionCommand()
That approach will work, but for some reason it looks like you manually need to set the action command when you create the button. For example:
JRadioButton maleButton = new JRadioButton( "Male" );
maleButton.setActionCommand( maleButton.getText() );
This acutally seems like a bit of a bug to me since usually the action command defaults to the text if the action command is not set.

If you have several buttons you probably should do it this way :
String getSelectedButton()
{
for (Enumeration<AbstractButton> buttons = buttonGroup1.getElements(); buttons.hasMoreElements();) {
AbstractButton button = buttons.nextElement();
if (button.isSelected()) {
return button.getText();
}
}
return null;
}

String gender=group.getSelection().getActionCommand();
It will work but it show null value.

int selectedRadioButtonID = radio_group.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if (selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
answerList.add(selectedRadioButtonText);
} else {
Toast.makeText(this, "select radio button", Toast.LENGTH_SHORT).show();
return false;
}
For Deatils, check this

Related

Radio Button Unchecked mark didn't reduced using Java Mysql

I am creating a simple exam system in Java and Mysql. When I start the exam all the question displayed from the database successfully, if I read the question and answer it then click next button it will be moving to the next question. It is working fine. If the answer is clicked correctly marks added 1. unless unchecked the answer answer become 0 I want it. my problem is if I click correct answer on the radio button marks changes as one. The same question again I select as wrong answer it didn't changes 0, it still mark become 1. How to solve the problem I clear the radio buttons it won't clear. What I tried so far I attached below.
Click Correct answer . mark displayed 1 correctly
enter image description here
after that same question answer change as wrong answer. Mark displayed
1.I need to display if we select wrong answer second time marks become decrement. I need to change as 1 become 0
enter image description here
Code what tried so far I attached below.
Click the Answer
public void answercheck()
{
String studentanswer="";
if(r1.isSelected())
{
if(r1.isSelected() == true)
{
studentanswer = r1.getText();
r2.setSelected(false);
r3.setSelected(false);
r4.setSelected(false);
}
else
{
studentanswer="";
r1.setSelected(false);
r2.setSelected(true);
r3.setSelected(true);
r4.setSelected(true);
}
}
else if(r2.isSelected())
{
if(r2.isSelected() == true)
{
studentanswer = r2.getText();
r1.setSelected(false);
r3.setSelected(false);
r4.setSelected(false);
}
else
{
studentanswer="";
r2.setSelected(false);
r1.setSelected(true);
r3.setSelected(true);
r4.setSelected(true);
}
}
else if(r3.isSelected())
{
if(r3.isSelected() == true)
{
studentanswer = r3.getText();
r1.setSelected(false);
r2.setSelected(false);
r4.setSelected(false);
}
else
{
studentanswer="";
r3.setSelected(false);
r1.setSelected(true);
r2.setSelected(true);
r4.setSelected(true);
}
}
else if(r4.isSelected())
{
if(r4.isSelected() == true)
{
studentanswer = r4.getText();
r1.setSelected(false);
r2.setSelected(false);
r4.setSelected(false);
}
else
{
studentanswer="";
r4.setSelected(false);
r1.setSelected(true);
r2.setSelected(true);
r4.setSelected(true);
}
}
if(studentanswer.equals(answer))
{
marks = marks + 1;
String marks1 = String.valueOf(marks);
txtc.setText(marks1);
}
}
Load all question from the database
public void Connection()
{
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/onlineex","root","");
stat = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stat.executeQuery("select id,question,option1,option2,option3,option4,answer from questions order by id DESC");
while(rs.next())
{
txtqu.setText(rs.getString("id"));
txtques.setText(rs.getString("question"));
r1.setText(rs.getString(3));
r2.setText(rs.getString(4));
r3.setText(rs.getString(5));
r4.setText(rs.getString(6));
answer = rs.getString(7);
}
} catch (ClassNotFoundException ex) {
} catch (SQLException ex) {
}
}
Next Click Button
if(!rs.isFirst())
{
rs.previous();
txtqu.setText(rs.getString("id"));
txtques.setText(rs.getString("question"));
r1.setText(rs.getString(3));
r2.setText(rs.getString(4));
r3.setText(rs.getString(5));
r4.setText(rs.getString(6));
answer = rs.getString(7);
answercheck();
}
Previous Click Button
if(!rs.isLast())
{
rs.next();
txtqu.setText(rs.getString("id"));
txtques.setText(rs.getString("question"));
r1.setText(rs.getString(3));
r2.setText(rs.getString(4));
r3.setText(rs.getString(5));
r4.setText(rs.getString(6));
answer = rs.getString(7);
}

CheckBox isSelected() method always returns false

I am working on an Android application where I have to check the CheckBox selection onClick() so I made this
if (checkBoxRM.isSelected() == true) {
isSelectedValue = "True";
//debug
Toast.makeText(MainActivity.this, "Kiejölés értéke: " + isSelectedValue, Toast.LENGTH_SHORT).show();
} else {
//debug
isSelectedValue = "False";
Toast.makeText(MainActivity.this, "Kiejölés értéke: " + isSelectedValue, Toast.LENGTH_SHORT).show();
}
But seems like isSelectedValue is always false.
Instead of isSelected() perform isChecked().
You have to cast the checkBox to enable isChecked()
This works:
boolean selected = ((CheckBox)view.findViewById(R.id.main_cbSelectAll)).isChecked();
This not (here, you can only choose "isSelected"):
boolean selected = view.findViewById(R.id.main_cbSelectAll).isChecked();
If you want to use it as an SQLite-statement, you have to transform to an integer:
int selected = ((CheckBox)view.findViewById(R.id.main_cbSelectAll)).isChecked() ? 1 : 0;

can't delete from getContentResolver().delete

I can't delete a conversation from getContentResolver, I don't know in which part am doing mistakes, as I also searched about these but can't help myself and I also tried different sols which were given on stackoverflow but same result & thanks a lot in advance.
Here is the code:
public static boolean deleteSmsofContact(Context context, String number,
boolean deleteLocked)
{
int result;
if (deleteLocked) {
//changes values
String[] selectionArgs=new String[]{number};
String selection= ""+"address=?";
//
result = context.getContentResolver().delete(Uri.parse("content://sms/"),selection,selectionArgs);
// Log.d("UF","WOW "+result+" " +number);
} else {
result = context.getContentResolver().delete(Constants.URI_SMS,
"address=? AND locked=?", new String[] { number, "1" });
}
if (result > 0) {
return true;
}
return false;
}
Here is the method from which I am calling:
boolean result = Utils.deleteSmsofContact(InboxActivity.this, sms.getNumber(), true);
if (result) {
dataList.remove(threadPosition);
iAdapter.notifyDataSetChanged();
Toast.makeText(InboxActivity.this,"Removed",Toast.LENGTH_LONG).show();
}else
{
Toast.makeText(InboxActivity.this,"cant removed",Toast.LENGTH_LONG).show();
}
Well I posted it but did not get the answer so finally I searched a lot on this and the correct answer is until or unless your app is not set a default you can't delete any sms or whole conversation.
Follow this link it will make your app set a default OR you will be able to delete.

How do I catch the null value from my JtextField?

I have a JTExfield and if the user leaves the textfield empty I wanna catch it up and set the string to "NULL"
It works if I write blablabla or whatever into the string and it should do but I also wanna catch if they leave it empty and put the text "NULL" into my file.
I have tried two solutions no one is working :
When user click ok button it performs this:
setPicture(pictureTextField.getText());
which is calling this method :
public void setPicture(String picture) {
if (picture == null) {
picture = "NULL";
}
this.picture = picture;
}
and :
public void setPicture(String picture) {
if (picture == "") {
picture = "NULL";
}
this.picture = picture;
}
So to repeat what I want to do is to set my picture String to "NULL" is the user leaves the textField empty.
Combine the null and empty checks together:
public void setPicture(String picture) {
if (picture == null || picture.isEmpty()) {
picture = "NULL";
}
this.picture = picture;
}
if(pictureTextField.getText().length()>1){
//nothing in text field. Here you can set null
}else {
// text field contains something
}
or try
pictureTextField.getText().equals("")
You can check this in two ways...
1] checks if String is empty...
if ( "".equals(picture.trim()) ) {
picture = "NULL";
}
2] checks if String length is 0...
if ( picture.trim().length() == 0 ) {
picture = "NULL";
}

Check if jTextField input is an int or a string

My code:
name = jTextFieldName.getText();
admin = Integer.parseInt(jTextFieldAdmin.getText());
anal = Integer.parseInt(jTextFieldAnalytical.getText());
creat = Integer.parseInt(jTextFieldCreative.getText());
finish = Integer.parseInt(jTextFieldFinisher.getText());
persons.addPerson(name, admin, anal, creat, finish);
persons.savePersons();
I want to make sure that name is a string and that admin, anal, creat and finish are ints between 0 and 30. I'm thinking that I should use try-catch, but I don't know exactly how to use it in this context. Any help appreciated!
try catch isn't a bad way to handle this:
try {
name = jTextFieldName.getText();
admin = Integer.parseInt(jTextFieldAdmin.getText());
anal = Integer.parseInt(jTextFieldAnalytical.getText());
creat = Integer.parseInt(jTextFieldCreative.getText());
finish = Integer.parseInt(jTextFieldFinisher.getText());
persons.addPerson(name, admin, anal, creat, finish);
persons.savePersons();
} catch (NumberFormatException e) {
// One of the integer fields failed to parse. Do something to alert the user.
}
You can then also put some bounds checking in the try part. e.g.
if (admin < 0 || admin > 30) {
// Problem. Alert the user.
}
Why don't you use a JSpinner instead.
What you need is if-else which statisfies condition or ask user to input again if required.
ex -
if(admin<0 || admin>30){
// ask user to input again.
}
Use InputVerifier for JTextField, like below
public class MyInputVerifier extends InputVerifier {
#Override
public boolean verify(JComponent input) {
String text = ((JTextField) input).getText();
// Validate input here, like check int by try to parse it using Integer.parseInt(text), and return true or false
}
}
// Set input verifier to the text field
jTextField.setInputVerifier(new MyInputVerifier());

Categories

Resources