• Post Reply Bookmark Topic Watch Topic
  • New Topic

jTable sum of a column  RSS feed

 
Hans Peter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there!

I´ve been creating a jTable where I can add / edit /delete new entries. Now i have 2 Questions :

1) My column 0 is filled with the row-number. I created a variable and with each "add" it adds +1 to the variable. That works, but as soon as i delete 1 row there is a gap ( 1..2..4..5..).
My question: Is there another way then mine to handle this?

2) The table is/should be working as some kind of database. The user is able to add Information (height, width,...). Now I want to sum the entries of 1 column (length,...). But as they´re objects I didn´t find a way.
My question: How can I sum the entries of 1 column of a table?

I appreciate any help!

Greeting Steffen
 
Knute Snortum
Sheriff
Posts: 3556
86
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Hans.

I will be helpful if you post your program and UseCodeTags.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We usually discuss such things in the GUIs forum where I am moving this discussion.

I presume you have read tutorials about tables?
And welcome again
 
Hans Peter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I´m not sure which part of my code is really important for the Problem..
For my first question this might be it:


And there are no codes so far for my second problem as i tried many different ways but I couldn´t solve the problem...
The table can be seen in the attachement. I want the program to sum the numbers that can be written into column 1 and an other sum of column 2.

Thanks for helping me
Table.PNG
[Thumbnail for Table.PNG]
jTable + add/edit/delete Buttons
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My column 0 is filled with the row-number


The row number should not be part of the table since it is not data related to the table but just a row identifier.

Instead you should use the "row header" of the scrollpane to indicate the row number. Check out Row Number Table for a class that does this for you.

How can I sum the entries of 1 column of a table?


Maybe use a TableModelListener. Whenever data in the table is changed you loop through all the rows and recalculate the new sum.
 
Hans Peter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
Instead you should use the "row header" of the scrollpane to indicate the row number. Check out Row Number Table for a class that does this for you.

Ok that might be the right way to handle my problem. I used the Netbeans GUI to create my table etc.. How can I change the table so that the rows get a "row Header" ?


Rob Camick wrote:
Maybe use a TableModelListener. Whenever data in the table is changed you loop through all the rows and recalculate the new sum.


I tried to use it but I failed :/ Can you help me here aswell? I tried following code:

I want to use the value of "total" when i´m clicking the "Berechnen" button, but it allways tells me that it can´t find the Symbol variable total. How can i fix this?

Thanks for the help
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a start, the total shown there is local to the if statement and goes out of cope when the if completes.
 
Hans Peter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:For a start, the total shown there is local to the if statement and goes out of cope when the if completes.


So i have to initialize it before the if statement starts - right?
 
Hans Peter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved my first problem

1) My column 0 is filled with the row-number. I created a variable and with each "add" it adds +1 to the variable. That works, but as soon as i delete 1 row there is a gap ( 1..2..4..5..).
My question: Is there another way then mine to handle this?


I don´t think it´s the best solution but it works. Due to the fact that I have to change the row-number only in case I delete an element of the table it only has to be corrected there. This is my solution:



My second - bigger - problem still exists:
2) The table is/should be working as some kind of database. The user is able to add Information (height, width,...). Now I want to sum the entries of 1 column (length,...). But as they´re objects I didn´t find a way.
My question: How can I sum the entries of 1 column of a table?

I´m thankful for any support

Steffen
 
Hans Peter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That´s actually what I´ve tried but it says "bad Operand type for binary Operator `+` first type: int second type: object
I know what´s the problem but I can´t solve it...
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I change the table so that the rows get a "row Header" ?


You don't change the table, you change the scrollpane. Did you not look at the link I provide you?

Its 3 lines of code:



I used the Netbeans GUI to create my table etc..


Well, you need to learn how to customize properties of a component without using the IDE. If you don't know how to do this you will never be able to learn how to code.

I don´t think it´s the best solution but it works.


You are correct, it is not a good solution. There is a big difference between providing a brute force solution and a proper solution. The only reason you are using the brute force solution is because you don't know how to use the IDE properly. This is one reason why I never use an IDE. I would rather spend my time learning Java instead of learning how to use an IDE. What happens if you ever need to use Eclipse? Then the code you generated is no longer valid.
 
Hans Peter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
You don't change the table, you change the scrollpane. Did you not look at the link I provide you?


I´ve been looking at your provided link and I´ve been trying to manage it as those 3 lines of code say, but I didn´t make it work. - sounds cheap but i really tried it with my bad knowledge for more then 2 hours

Rob Camick wrote:Well, you need to learn how to customize properties of a component without using the IDE. If you don't know how to do this you will never be able to learn how to code.


You´re right.
The Point is that I´m actually studying some energy engineering stuff and I´m not used to coding. This program is part of my Bachelor exam and due to fact that I have only 4 month to write it and the program is just a part of it I don´t have that much time to "learn" Java. I first started with eclipse, but a friend told me about Netbeans and I tried it and it worked out really fast to "see" something happen.

I´ve also been trying to solve my problem like this:

But then this error comes:
 
Hans Peter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok got it working this way:

I had to Change from:

into

Thanks for your help everybody

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!