Vector for Jtextfield

Asked

Viewed 173 times

-1

I have to add 43 text fields that the user will fill the ones that will be used to have a total and this total will be used in my formula and I could not find a solution to reduce these lines that I manually assigned to a variable each field. My question is:

I can create an array to receive text fields like this? what would be the formula to take a Jtext and take the value of the next one and add?

  • What is a Jtext? And add one [mcve] so that it is possible to test and execute your code.

  • Jtextfield, I ended up abbreviating by custom, this is a text field.

1 answer

1


You can loop all components of your dashboard and if it is an instance of JTextField, you sum up. Ex:

Double total = 0.00;    

for (Component c : painel.getComponents()) {    
    if (c instanceof JTextField) {
        total += Double.parseDouble(((JTextField) c).getText());
    }
}

But remember that all your JTextField used in the calculation shall be on the same panel.

  • Zulian, it was Jtextfield himself, sorry, I ended up abbreviating by custom, but thanks man, I will test in my code here, only the "System.out.println" that I will really need, because this total variable I use in the formula.

  • There’s only one problem, as I understand your code, you’re creating a jTextField every round of the for, but I’ve already created all the ones I need on the jFrame screen, I just want to take the values and add now, there’s your second for, but I don’t know how it will pass from one name to the other, and each jText of mine are different the variable name.

  • @Gabrielcardoso, the System.out.println() it was just to visualize the result. This way I put it is more practical, but as you have already created, I will update the answer, giving another example.

  • @Gabrielcardoso updated the answer.

  • I got it, that’s what I figured, anyway I’d have to assign the 43 manually on the list, that’s pretty much what I did by assigning to a variable. Place a piece of code here. double Totalcc = 0; //Sum the values passed in the cost center. Totalcc += Double.parseDouble(spnCC1.getValue().toString(); Totalcc += Double.parseDouble(spnCC2.getValue().toString(); ... because imagine if I had a lot more fields..

  • @Gabrielcardoso without you presenting a [mcve] will be difficult to help even. The answer answers what was asked, is beginning to detract from the initial doubt, and all because you are presenting information that should have been added in the question, and not as comments, aside from not presenting an executable example of your code.

  • I posted a piece of code ai that I’m currently using, are 43 lines equal to this for each field except, I just wanted to do it within a for without needing to assign each field, what you gave me really works, but I will anyway assign each field when adding to the list. I wanted to see if any method passed by all jText on the panel and was adding them up.

  • @Gabrielcardoso if you do not want to manipulate all fields, the solution has already been given using loop. Now if you don’t know how to add it to your code, it’s another problem, and for that, you must present a [mcve] if you want to get something more specific for your solution.

  • You who haven’t understood by now that my problem is not wanting to set it all up manually, but relax man, thanks for the help.

  • In the last solution I put, taking the panel components, you do not need to manually set, look again and try to understand.

  • @Gabrielcardoso the solution is at the end of the answer, in the last loop, he will rescue all the text components of the panel. What is the difficulty? Just apply it to your code.

  • @Zulian not to confuse the author, it would be interesting to remove the rest, since apparently the other initial solution will not suit him.

  • Now appeared the edition for me, I will test this, I was until then talking about the previous solution.

  • 1

    Zulian, thank you so much guy, gave it right, created a panel, played all the rotary controls inside (now I’m using Jspinner) and made the formula, worked perfectly, worth the support. If anyone knows any good document of Swing features, I’m taking a pal here to edit its layout. Thanks in advance.

  • @Gabrielcardoso Good that was helpful. For the layouts, I recommend reading the official documentation. You can mark the answer as correct by clicking V next to it.

  • Expensive ball show, I scored yes, thanks for the document.

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.