Value from button to be set in the next available JTextField? - java

In my app, I need for values from buttons (the value is taken from an SQLite database) to be displayed in one of 13 JTextFields, and they can be in any order. How do I make it possible for the value to be displayed in the next available JTextField, if say the first one is empty?
The only thing I could think of was
if (textField.getText().isEmpty())
{
String text = String.valueOf(num);
textField.setText(textField.getText() + text);
}
What should I do next? How should I go around the else statement? Should I even use it?
Thanks in advance!

If I understand your question correctly,this should be the idea
JTextField[] fields = new JTextField[13];
firstText.setName("First Text") ;
.......
field[0] = firstText;
field[1] = SecondText;
//then add remaining textfields
for(JTextField txtField : fields) {
if(txtField.getText().equals("") ) {
// do whatever you want
}else{
// do whatever you want
}
}

Related

Incrementing by 1 when pressing a button

My code is very long so I will only be adding snippets that are relevant.
Okay so I've been trying to increment a label by one using the following code:
btnComplete.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
//if the list has a minimum of 1 item
if (currentCartTxt.getItems().size() > 0) {
int sales=0;
sales++;
String x = Integer.toString(sales);
numberOfSalesTxt.setText(x);
}
}});
However it only changes my textfield to 1 and never increases it. Any help would be greatly appreciated.
the currentCartTxt is a listView and the numberOfSalesTxt is a textfield.
Basically to explain my app, I have a list of items that I am adding to a textfield (currentCartTxt) and I need to press the complete button whenever but there must be at least 1 item in the textfield. And every time the button is pressed the textfield(numberOfSalesTxt) increases by 1.
Thanks!
You have to:
read current value (from Label/View/TextView...)
increment it (just add 1)
set new value to view
if (currentCartTxt.getItems().size() > 0) {
// get current value
String text = numberOfSalesTxt.getText();
// convert it from "String" to "int"
int sales = Integer.parseInt(text);
// increment it
sales++;
// Convert from "int" to "String"
String x = Integer.toString(sales);
// Set new value
numberOfSalesTxt.setText(x);
}

How to print with JButton in JtextArea?

I have two buttons and I want to print their test continuously in one JTextarea, but when I print the first one and then I push the second, the last value gets deleted from the Jtextarea
This is my code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setText("1");
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setText("2");
}
I want to print 1 when I push key one and when I push key two, print 12. But my program first deletes the last one, the last pushed key, then prints the new value. How can I fix this problem?
I'm using Netbeans IDE
use append function instead of setText:
jTextArea1.append("2");
Reference document:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html#append(java.lang.String)
Just get the current text, and then add onto to:
jTextArea1.setText(jTextArea1.getText() + "2");
This will grab the text already in the text area, and uses string concatenation to add 2 onto the preexisting string.

How to get the blank jtextfield?

I am building a calculator (gas law calculator) which has four text fields, and I need three fields to be filled by numbers to calculate the fourth value. The equation is v1/p1 = v2/p2. But the problem is I don't know which three values the user will fill. So I need to find an algorithm to check each text field and determine which is empty. I am using swing classes. Jut give me direction.
Thank you!
DocumentListener
to try to avoiding KeyListener
possible way could be FocusListener also, notice Focus is asynchronous
Well, what do you expect the value of the "empty" field to be?
Of course it will be the empty string, "".
So just test for which fields contents equal the empty string (or have a length of 0).
How about this?
private boolean validateField(JComponent component)
{
if (component.getText().trim().length() == 0){
return false;
}
else{
return true;
}
}
If you want to check if JTextField is empty you just do it as follows:
Example(sudo code):
-> JTextField field = new JTextField("v1:");
-> if(field.getText().isEmpty == true){...}
else{...}

How to submit multiple vales from form control to the database

I have 8 MC questions in a panel. When submitted, I want all the selected answer to be recorded in the database. However, my code is only recording 1 question. Here is the code. (Note: All the jRadioButton names are not same since they are in one panel together.)
Here is the code :
public void submitButtonClicked(){
for(int i=1;i<9;i++){
username = "Smith";
questionID = i;
if(jRadioButton1.isSelected()){answer = jRadioButton1.getText();}
else if(jRadioButton2.isSelected()){answer = jRadioButton2.getText();}
if(jRadioButton3.isSelected()){answer = jRadioButton3.getText();}
else if(jRadioButton4.isSelected()){answer = jRadioButton4.getText();}
// and So on until the question 8.
}
Consider creating an array or ArrayList of the ButtonGroups for each JRadioButton cluster. You can then use a for loop to get the selection from each ButtonGroup which is the model of the JRadioButton selected, and if not null, get its actionCommand String.
For example, please look at my code here.
In your solution only one value is recorded because if one if statement is executed then it will bypass all other else if statements.
You could create a array of jradiobuttons and then use them in for loop,traversing each button one by one and then recording its answer.

GWT telephone number masking

Does anyone know how to go about creating field that would perform telephone number format masking, like here (___) ___-____:
http://www.smartclient.com/smartgwt/showcase/#form_masking
A better approach would be to let the user type whatever they want: "789-555-1234" or "(789) 555-1234" or "7895551234" and then when the field loses focus decide if what they typed can be a phone number. If so you can reformat it as "(789) 555-1234". There are several related questions about how to do that sort of thing with regular expressions; just be sure your regex accepts the format you're changing the user's input to, otherwise it will be really annoying to edit.
As an example, look what happens when you type ".5" into the left margin field in Microsoft's standard page setup dialog: when you tab out it changes it to "0.5".
UPDATE: Here's sample code in GWT to illustrate. For the sake of this example, assume there's an element called "phoneContainer" to put the text box in. GWT doesn't give you the full java.util.regex package, but it gives enough to do this:
private void reformatPhone(TextBox phoneField) {
String text = phoneField.getText();
text = text.replaceAll("\\D+", "");
if (text.length() == 10) {
phoneField.setText("(" + text.substring(0, 3) + ") " + text.substring(3, 6) + "-" + text.substring(6, 10));
}
}
public void onModuleLoad() {
final TextBox phoneField = new TextBox();
RootPanel.get("phoneContainer").add(phoneField);
phoneField.addBlurHandler(new BlurHandler(){
public void onBlur(BlurEvent event) {
reformatPhone(phoneField);
}
});
}
It looks like you'd want to create your own widget that extends the GWT input box and has a default value set to the mask you want. Then you handle the onKeypress event and update the field as needed (making sure to set the cursor position to the correct location).

Categories