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.
Related
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
}
}
I'm working on a program in Javafx, which allows the user to input values in TextFields. Additionaly, there is an empty Choicebox, where a value beginning from "1" is added. Everytime he presses the "Submit" button, the TextFields also get cleared and he can input another set of values in said TextFields.
Let's say he inputs five sets of values and presses "Submit", I'd like to have those values stored in an array, so when he chooses a number in the ChoiceBox, the corresponding values are shown again in the TextFields.
For this I need to store them in an array. Below is the eventhandler for the buttonclick
private void next(javafx.event.ActionEvent event) {
double c_stueck = Double.parseDouble(stueck.getText());
double c_pr_pro_einheit = Double.parseDouble(pr_pro_eh.getText());
double c_betrag = Double.parseDouble(betrag.getText());
String c_bezeichnung=bezeichnung.getText().toString();
c_betrag += c_stueck * c_pr_pro_einheit;
String c_betragval = String.valueOf(c_betrag);
betrag.setText(c_betragval);
stueck.setText("");
pr_pro_eh.setText("");
bezeichnung.setText("");
for(int j=0; j<=49;j++)
{
arr_stueck[j]= c_stueck;
arr_pr_pro_eh[j]= c_pr_pro_einheit;
arr_bezeichnung[j]= c_bezeichnung;
position.getItems().add(j);
}
}
The problem lies herein, that everytime the eventhandler is fired, the array is reset. Is there a way to save the increment, so the next time the button is pressed, the for loop continues from the last increment?
For my current project I need to create a custom text box that gets each key pressed and adds it to a string. I'm constantly updating the method that retrieves the key a user is pressing and then I add it like so:
public void addCharacter(String c) {
String before = text;
String after = before;
if (!before.endsWith(c)) {
after = text + c;
} else {
//What can I do here to check if the key
//was released and then pressed again, so that
//it only adds the character the number of times the user presses the key.
}
text = after;
}
My problem is that if I type a key it adds tons of them because of the fact that it is constantly updating, which is why I had to check if it's the same letter as before, and not add it.
EDIT:
Example of how we add the key:
if (key.a) {
addCharacter("a");
return;
}
I think you can get the job done by using a KeyListener. This is basically a listener which sends events each time a key is typed, pressed and released. In a nutshell, I would listen to a keyPressed event and then I would not type that letter until I receive a keyReleased event.
OK, so I created a console app that, among other things, takes an array of numbers and prints them out one by one, line by line. Now, I have to take the class that I created for that console app, and pop it into a separate GUI app we're creating. I have all of the other methods working fine, but for the life of me I cannot get the array method to print out correctly. It just gives me the last number I typed into the text field. I'm hoping someone can give me a nudge to help me figure this part out so I can move along, and get to the whole SpringLayout stuff, (the main part of the new assignment) I am limited in what I can show you here because this is a current assignment, so I will have to stick to this stuff as specifically as I can. And please, don't just post the code as an answer, (because then I can't use it), thanks.
Here's what I had for my original project for the array method:
int [] getArray(int x)
{
breakUpNum(x);
return numAry;
}
From there, inside my new class I have this, in an attempt to get it to print:
private class ButtonTest implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
Lab1 tester = new Lab1();
int[] test4 = tester.getArray(num);
for(int i = 0; i < test4.length; i ++)
{
crossTest.getArrCross.setText("" + test4[i]);
}
}
}
Any help pointing me in the right direction would be greatly appreciated, thanks!
setText does just that, sets the text you pass to as the current text content, it does not append it.
If you were to use JTextArea, you could use it's append method...however, for a JTextField you need to have a different approach.
Now you could use getArrCross.setText(getArrCross.getText() + test4[i])...but to quite frank, that's rather inefficient, as each call to setText is going to stage a paint event...
StringBuilder sb = new StringBuilder(128);
for(int i = 0; i < test4.length; i ++)
{
sb.append(test4[i]);
}
crossTest.getArrCross.setText(sb.toString());
Now, if you want to separate each element, you need to add
if (sb.length() > 0) {
sb.append(", ");
}
Before sb.append(test4[i]);
The last bit of actionPerformed in the for loop isn't working right. setText replaces the current text with its argument, and it doesn't seem like you want to do that. To fix it, replace the line in the for loop with this:
crossTest.getArrCross.setText(crossTest.getArrCross.getText() + test4[i]);
I'm trying to learn something about GUI, using NetBeans6.8, starting with the GUI section in The java tutorial.
There is a simple exercise for a Celsius-Fahrenheit converter. I want that to have two TextFields, one for Celsius and one for Fahrenheit temperature; if the user types in the celsius text field he got the result "printed" in the fahrenheit text filed. and vice versa.
So, i put on both the textfields one KeyTyped event, here's the code:
private void celsiusTextKeyTyped(java.awt.event.KeyEvent evt) {
int cels = Integer.parseInt(celsiusText.getText());
int fahr = (int)(cels * 1.8 + 32);
fahrText.setText(fahr + "");
}
private void fahrTextKeyTyped(java.awt.event.KeyEvent evt) {
int fahr = Integer.parseInt(fahrText.getText());
int cels = (int)(fahr / 1.8 - 32);
celsiusText.setText(cels + "");
}
It doesn't work. If i type something in a textfield i got this exception: java.lang.NumberFormatException: For input string: ""
The code that attach the listeners:
celsiusText.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
celsiusTextKeyTyped(evt);
}
});
fahrText.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
fahrTextKeyTyped(evt);
}
});
[However, i can't modify it, it's autogenerated.]
Method .getText() returns a string not a number, if that string contains non-numeric characters (i.e. a letter, a space, nothing at all) then parseInt will throw a NumberFormatException. Since your using KeyEvent, as soon as you press say "7", the event is fired before 7 is entered into the text box. Thus the text box still only contains "", which is where the error comes from. You may wish to also listen to the keyUp event instead.
You need to enclose your code in a try catch block.
private void fahrTextKeyTyped(java.awt.event.KeyEvent evt)
{
try
{
int fahr = Integer.parseInt(fahrText.getText());
int cels = (int)(fahr / 1.8 - 32);
celsiusText.setText(cels + "");
}
catch(NumberFormatException ex)
{
//Error handling code here, i.e. informative message to the user
}
}
An alternative is you could filter out non-numbers on keydown event, see example here - http://www.javacoffeebreak.com/java107/java107.html (Creating a custom component - NumberTextField)
I suspect that what's happened is that you added these handlers with something like celsiusText.addKeyListener, yes?
The thing is, that'll give you not just the KEY_TYPED events you wanted, but also KEY_DOWN and KEY_UP. The KEY_DOWN event will happen before the text is really entered into the field, so your code firing on that will see the field as blank still. Trying to convert the empty string to a number gives you a format exception.
The easiest way to fix this is the try/catch construct other people have been posting.
You probably set action to keyDown, this mean that even occur before the key value is "added" to textbox, while You retrieve the value from it is still empty "".
There is a simple exercise for a
Celsius-Fahrenheit converter
That is a really old example. The better approach is to use a DocumentListener, not a KeyListener.