I am working on some project, where a user can fill in student details in a form with textfields and radio buttons etc, and the values shall then be used to create a record in a database.
Now, this is how i started:
String firstName = textField_1.getText();
String surname = textField_2.getText();
String Gender = rdbtnM.getText();
Basically, what it does is, its retrieving values from particular input fields.
Now, my question is, for gender i have two radio buttons. rdbtnM and rdbtnF.
A user can only click on one: Male or Female.
How do i write in String Gender, that it should get the value from M or F ?
Please advise?
I suppose you are using Swing, with two JRadioButton objects inside the same group, but your question is very imprecise. If you want to do it in a simple way, you can do the following:
String gender // make your variable names start lower-case
if (rdbtnM.isSelected()) {
gender = "male";
}
else if (rdbtnF.isSelected()) {
gender = "female";
}
else {
gender = "unknown";
}
You can of course avoid the last case by initializing the button states properly, but you don't provide any initialization code. There is an official tutorial about buttons, which also explains how to deal with radio buttons by the means of ActionListeners, which I avoided here.
Related
Hi im doing a project for college and after finishing the majority of the code i hit a speedbump i need to register three types of users and one of them uses the department building nick as a basis for the password attribution i have an arraylist for each type, the password for this type of user should composed by the nick of the department and the number of registered users in that department so i wrote the code below which works which kinda works but the first password always gives me an empty field, the code i have is:
public String GivePasswordStaff(String signdept){
int counter = 0;
String password = "";
Iterator<Staff> liststaff = staff.iterator();
while (liststaff.hasNext()) {
Staff lists = liststaff.next();
if(signdept.equals(lists.getSigndept())){
counter++;
password = signdept + counter;
}
}
return password;
}
The thing is i need the first passwords of each department to be nick1 and with this code the first one is always "" and the second it gives the password i want except if i input a password for a different department in which case it always start with nick0 what can i do to solve this
Thanks in advance to all that reply
An ugly but posible solution would be to assign the desired name for the password at the declaration:
String password = signdept+"1";
For the part about nick0, try to write some example, I can't understand what happens. What do you mean by "except if i input a password for a different department"?
How to choose radio button. `//choose user gender
String male = "male" ;
String female = "female";
String other = "other";
System.out.println("User gender");
gender = sc.nextLine();
System.out.println(gender);
if (gender.equalsIgnoreCase(female)) {
driver.findElement(By.cssSelector("#gender_female")).click();
}
else if (gender.equalsIgnoreCase(male)) {
driver.findElement(By.cssSelector("#gender_male")).click();
}
else if (gender.equalsIgnoreCase(other)) {
driver.findElement(By.cssSelector("#gender_other")).click();
}`
by the code above I want to select the radio button according to the user choose: my problem is that when user chooses either female or male or other it will not work. the program will be terminated. How to choose radio button by user input. Please help
Look to lower left frame of your Eclipse window: There you've got an ElementNotFoundException. This means that your locator (in this case By.cssSelector("#gender_male")) does not resolve to an HTML element.
Change the locator to some valid expression (depending on your web page code) and it should work.
I have a swing application am working on, my application lets the user enter a first name, last name, and phone number. the the user clicks the add button and it adds the entries into the jlist(so it like a phone book). I have a jTextfield above the JList in which I want to allow the user to search for a specific name or phone number on the Jlist, so its kind like a google search you type a character and it shows names with relevant characters in the JList and such. Am really stuck and lost at this point any help would be appricated??
This is my add button code to add names to my Jlist:
private void btnAddContactActionPerformed(java.awt.event.ActionEvent evt) {
String firstName = txtFirstName.getText();
String lastName = txtLastName.getText();
String phoneNum = (txtPhoneNum.getText());
NumberFormat number = NumberFormat.getNumberInstance();
//Phone Number formatted
StringBuilder sb = new StringBuilder(phoneNum).insert(0, "(")
.insert(4,")").insert(8,"-");
String phoneNumFormatted = sb.toString();
contactsArrayList.add(firstName + "\t " + lastName + "\t " + phoneNumFormatted);
DefaultListModel<String> model = new DefaultListModel<>();
for(int i = 0; i < contactsArrayList.size(); i++)
{
String myArraylst = contactsArrayList.get(i);
model.addElement(myArraylst + "\t");
}
listPhoneBookContacts.setModel(model);
txtFirstName.setText("");
txtLastName.setText("");
txtPhoneNum.setText("");
}
It is possible to implement this kind of stuff in Swing, but it is gnarly and you are unlikely to do a good job of it (because it's hard). You're probably better off leaving it to some other library, like SwingX. They have a bunch of components you can use that might do exactly what you want.
If you don't want to use that, a quick Google search reveals a good tutorial for filtering JLists.
my application lets the user enter a first name, last name, and phone number
I would use a JTable to display all this information.
so its kind like a google search you type a character and it shows names with relevant characters
JTable has built in filtering. See Sorting and Filtering for a working example
I've googled the holy bejaysus out of it and just can't find the right combination of words. How do I use methods on something with no identifier? For instance;
String inputBio = JOptionPane.showInputDialog(null, heroPane, "Please enter details...", JOptionPane.OK_CANCEL_OPTION);
How do I refer to that dialog?
So you want to assign the value typed into the input box to the String? You need to create an Integer and assign it to the JOptionPane.showInputDialog, then use the constructor to instantiate a new instance of the Input Dialog with the options you want.
Now, to extract the data, add an IF statement and go through the possible options presented to the user. If you added JoptionPane.OK_CANCEL_OPTION then you have two options: OK and CANCEL.
The IF statement should be: If the user selected OK, extract the string, ELSE do whatever you want to do.
The string should be extracted from the textfield that was created (which I assume is somewhere in the 'heroPane')
Here's how that would look in code
//the string we use for input
String inputBio;
int optionBox = JOptionPane.showInputDialog(null, heroPane, "Please enter details...", JOptionPane.OK_CANCEL_OPTION);
//now the integer value represents the option selected (OK or CANCEL)
//All we need to do is extract the data and assign it to the string
if (optionBox == JOptionPane.OK_OPTION) {
//the OK option
inputBio = nameOfYourTextField.getText();
} else {
// we have a CANCEL option
}
You could, of course, do it without an IF statement, but make sure you handle all of the options accordingly
If you need a reference to the JOptionPane, you cannot use the showInputDialog convenience methods. You will need to construct a JOptionPane object.
I have design the search method. According to my requirement, If a user wants to search a customer’s record, he/she can input the customer’s family name or given name into the name text field and then click the search button. The following two screen shots show an example of the events before and after button click (with input the name to be searched is Lee)
And below is the code for searching. It is working fine but I want to make it better?
private void search()
{
String studentName=CNameTextField.getText();
String record="";
int count=0;
for(int i=0; i<myList.size();i++)
{
String name=myList.get(i).getName();
String [] splitName= name.split(" ");
if(studentName.equals(splitName[0]) || studentName.equals(splitName[1]))
{
count++;
record=record+"\n"+myList.get(i).toString();
}
display.setText("");
display.append(count + " result(s) found for "+ studentName);
display.append("\n "+ record);
}
}
So you've basically got a list of String items, and you're searching through all of them for the value?
My recommendation would be to create Objects for each line in your DisplayArea, rather than Strings. For example, when you read in the input file for your DisplayArea, do the split() for each line and create objects of type Customer that have fields called ID, name, room, etc. This would be better OO programming anyway - Strings don't really have any meaning, whereas a Customer has meaning.
If you do this, in the search you can simply loop over all the Customers in the list, asking whether the name.equals(customer.getName()); This would remove the need to split the line every time you search.