how to add text rather than replacing it in textfields java - java

I have multiple buttons containing some information.
Now I want that the information written on the buttons are appended when i press them i.e when i press the first button
- the information gets printed into text field
When I press second button
- the information written on button gets appended or added into the text field with the older information (data in button 1).
code for what I am trying is:
private void EActionPerformed(java.awt.event.ActionEventevt) {
String x = ans.getText();
for(int i = 0; i < x.length(); i++) {
ans.setText("H");
}
}
private void FActionPerformed(java.awt.event.ActionEvent evt) {
String x = ans.getText();
for(int i = 0; i > x.length(); i++) {
ans.setText("A");
System.out.println("completed");
}
}

Simply:
ans.setText(ans.getText() + newString);

There are a few ways to this, setText is one, but it's not particularly efficient (it's easier to type though), as you are creating additional temporary objects though the process
If you need to update the field often, you might consider using something more like...
Document doc = userNameField.getDocument();
doc.insertString(doc.getLength(), newString, null);

Related

Expand the range of numbers

I was trying a solve a issue which is bothering me for a while. I created a small parser that reads an .ini file and then stores the data in an ArrayList. However, I got stuck with the following snippet:
while (!(sCurrentLine.equals("[End]"))) {
formats.add(sCurrentLine);
for (int i = 0; formats.size() > 0; i++) {
}
sCurrentLine = br.readLine();
}
Now this is the place where I have to add values into formats, which is of type ArrayList.
The values that will be added like this:
0900.013-017=LABEL
0900.018-029=LABEL
Now the range is in between and I also have to make sure that '0900' and '=label' repeats themselves along with the expansion of numbers, for example:
0900.013=LABEL
0900.014=LABEL
0900.015=LABEL
0900.016=LABEL and so on...
and store it back in the ArrayList.
I don't want to depend upon third-party libraries. Please help me out with this.
Use a regular expression to parse the range, then loop over the parsed values. There is some fine tuning to be done but I think this should get you started.
Pattern rangePattern = Pattern.compile("([0-9]+)\\.([0-9]+)-([0-9]+)=(.*)$");
Matcher rangeMatcher = rangePattern.matcher("0900.13-17=First label");
if (rangeMatcher.matches()) {
String prefix = rangeMatcher.group(1);
int start = Integer.parseInt(rangeMatcher.group(2));
int end = Integer.parseInt(rangeMatcher.group(3));
String label = rangeMatcher.group(4);
for (int r = start; r < end; r++) {
System.out.println(prefix + "." + r + "=" + label);
}
}
Create the pattern once and then just get new matchers each time through your loop.
The results:
0900.13=First label
0900.14=First label
0900.15=First label
0900.16=First label

Java - How to create a loop for TextFields? [duplicate]

This question already has answers here:
adding elements defined in FXML to list with loop
(3 answers)
Closed 3 years ago.
Basically, I would like to make a loop which would change the name of every TextField to a name stored in a folder. I fully understand how to make loops for int values, but completely don't know how to make it to affect a methods (like I show it below). Any ideas how to solve the problem?
Check the code and I am sure you will understand what I mean
#FXML
private TextField t1;
#FXML
private TextField t2;
// etc...
#FXML
void music(ActionEvent event) {
if (event.getSource() == dmusic) {
File folder = new File("C:\\eclipse\\MP2");
File[] list = folder.listFiles();
for (int i = 0; i < list.length; i++) {
System.out.println(list[i].getName());
// Here i would like to update TextField name for every "t" method like I did below, but without writing it all the time.
}
// t1.setText(list[0].getName()); // can't make infinite "t"'s and would like to make it in a loop
// t2.setText(list[1].getName());
// t3.setText(list[2].getName());
// t4.setText(list[3].getName());
// t5.setText(list[4].getName());
// t6.setText(list[5].getName());
// ...
}
}
Try using an arraylist
Arraylist<TextField> fields = new ArrayList<>();
<add TextFields to arraylist>
and then use
for(TextField field : fields) {
field.setText("");
}
or
for(int i=0; i< fields.size(); i++){
fields.get(i).setText("");
}
to access your individual text fields.

Changing an array attempt

I posted this question up earlier and it was pretty much a lazy post as I didn't provide the code I had and as a result got negged pretty badly. Thought I'd create a new one of these ....
I have an array dogArray which takes ( name, secondname, dogname )
I want to be able to change the dogname:
here's my attempt :
public void changeName(Entry [] dogArray) {
Scanner rp = new Scanner(System.in);
String namgeChange = rp.next(); {
for (int i = 0; i < dogArray.length; i++){
for (int j = 0; j < dogArray[i].length; j++){
if (dogArray[i][j] == name){
dogArray[i][j] = nameChange;
}
}
}
}
For a start it doesn't like the fact I've used ' name ' although it is defined in the dogArray. I was hoping this would read input so that users are able to change 'name' to whatever they input. This will change the value of name in the array.
Do you guys think I'm getting anywhere with my method or is it a pretty stupid way of doing it?
Only one loop is necessary, also move your call to next.
public void changeName(Entry [] dogArray) {
Scanner rp = new Scanner(System.in);
for (int i = 0; i < dogArray.length; i++){
String nameChange = rp.next(); {
if(!dogArray[i].name.equals(nameChange)){
dogArray[i].name = nameChange;
}
}
}
You might want to make this function static as well and use accessors and mutators to change and get the name. Might want to tell the user what you want them to type as well. Also there is no point in testing if the name changed, if it changed then set it, if it didnt change then set it (it doesnt hurt). Just get rid of if and keep the code inside.

How to dispose all children of Composite object?

I hava a
Composite descComp
with some stuff in it... basically it is a container for a form, consisting of number of labels, Combos and buttons, all aligned in a line.
My form is not finite, I have a button that adds one extra line for extra input. However for that to work it seams I have to dispose old children of my descComp...
private void populateConstantMain(ContentData tariffConstantsOfType, Composite descComp,GridLayout descCompLayout, Boolean resize) {
int arraySize;
if (resize == false) {
arraySize = tariffConstantsOfType.getQueryRowCount();
} else {
for (int i = 0 ; i < descComp.getChildren().length; i++) {
System.out.println("Disposing: " + i + " " + descComp.getChildren()[i].toString());
//descComp.getChildren()[i].setEnabled(false);
descComp.getChildren()[i].dispose();
}
arraySize = tariffConstantsOfType.getQueryRowCount() + 1;
}
......
}
For some reason
descComp.getChildren()[i].dispose();
does not work, meaning it wont dispose all children, that results in errors in inserting new children, therefore spoiling the layout :/ Interesting thing is that
descComp.getChildren()[i].setEnabled(false);
works, when I uncoment it, for all children...
I have a hunch that calling getChildren() on a composite returns you only the non-disposed children at the time you call it. So calling descComp.getChildren()[i].dispose(); is all messed up as your index is incrementing but your array is decreasing in size. Why don't you try:
for (Control control : descComp.getChildren()) {
control.dispose();
}
This way you get a static view of the children in the composite before you start disposing of each of them.
I've also switched the code to use the nicer J5 for-each syntax. If you are stuck on J1.4 then unfortunately you'll need to stick to the for(;;) loop:
Control[] children = descComp.getChildren();
for (int i = 0 ; i < children.length; i++) {
children[i].dispose();
}
When disposing children (or anything in an array) I use the for next loop but go through from end to start, not start to end. (And get the length before the loop or it changes.)
int i = length-1;i>=0;i--
otherwise you delete every other one.

Problem with converting a string to an integer and passing as an ID to a view

There are 20 buttons in an Activity .
The ids are R.id.ButtonR1C1; R.id.ButtonR1C2 .. and so on ie. Row 1, Col 1..
now earlier I had created 20 buttons.
private Button b1,b2,b3...;
and then
b1=(Button)findViewbyId(R.id.ButtonR1C1);
b2=(Button)findViewbyId(R.id.ButtonR1C2);
.... and so on.
Finally
b1.setOnClickListener(this);
b2.setOnClickListener(this);
... 20
so I thought I'd create a Button Array
Button barray[][]=new Button{4][5];
for(int i=1;i<=4;i++) {
for (int j=1;j<=5;j++) {
String t="R.id.ButtonR"+i+"C"+j;
barray[i-1][j-1]=(Button)findViewbyId(Integer.parseInt(t));
}
}
Gives an error..
Any help??
If you have copied your code directly, your syntax is wrong.
Button barray[][]=new Button{4][5];
should be
Button barray[][]=new Button[4][5];
Note the curly bracket instead of square bracket before the 4.
Your next problem, is that you are trying to get the value of R.id.something, but you only have the string representation of it. So, the only way I know to do this is using reflection.
What you need to do is,
Button barray[][]=new Button{4][5];
for(int i=1;i<=4;i++) {
for (int j=1;j<=5;j++) {
Class rId = R.id.getClass();
// this gets the id from the R.id object.
int id = rId.getField("ButtonR"+i+"C"+j).getInt(R.id);
barray[i-1][j-1]=(Button)findViewbyId(id);
}
}
String t="R.id.ButtonR"+i+"C"+j;
Integer.parseInt(t);
This part of your code will definitly throw a runtime exception, because t does not contain a numeric value.
I don't know the format/value of your ID values, but this might work:
Button barray[][]=new Button[4][5]; // type corrected
for(int i=1; i<4; i++) { // changed the for loop...
for (int j=1; j<5; j++) { // changed the for loop...
String t="R.id.ButtonR"+i+"C"+j;
barray[i-1][j-1]=(Button)findViewbyId(t); // t *is* the id (?)
}
}
The Problem is here:
String t="R.id.ButtonR"+i+"C"+j;
barray[i-1][j-1]=(Button)findViewbyId(Integer.parseInt(t));
You're trying to convert a String t to an Integer (while the String is not numeric at all).
This will result in NumberFormatException. You should make sure that t is absolutely numeric.
I used this code:
Class c = Class.forName("com.test.R$id");
Integer i = new Integer(c.getField("ToolsbuttonR3C4").getInt(new R.id()));
System.out.println("HEXA="+i.toHexString(i));
and the ids are in sequential order. so it can be done.
Thanks ppl
So you are trying to convert the String "R.id.ButtonR1C1" to a NUMBER...
That will most certainly give you a NumberFormatException since it is CLEARLY NOT A NUMBER! :)

Categories

Resources