• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Removing of component in Swing does not work

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. I am new to this forum, curently studying java swing and creating a small application that I need at work. For the sake of simplification, when I implement new features I tend to create simple programs where I add one feature at a time, thus observing what is happening.
Overall I am creating a table in swing and I will add several textfields and labels. User will add data there and then click save. This data will eventually be saved in some arrays and then...well there is a long way to there.
So I created a right click popup menu and when I click "add row" it will add some rows. In this example one jlabel and one jtextfield. All ok until now. But I don't know how many rows my users will insert, it can be anything from 5 to 50 so I allow user to add until 50 (feature not yet implemented in this example underneath, no worries).

I add rows, it works, I delete rows...and here the problem. The last component gets deleted. But what if user wants to delete two rows? It does not work apparently. If I add 5 rows, eachof 1 label and 1 jtextfield, the remove row only deletes the last row but not also one more or as many as user keeps hitting the "remove row" button. What is wrong with my code?





Thanks guys, wish you well!
 
Saloon Keeper
Posts: 6467
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of
You need to keep track of all of the rows
 
Mihai Xulescu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Instead of
You need to keep track of all of the rows




I didn't study yet Lists...I am still at arrays and next will be arraylists....

By using lists it will solve my issue?
 
Bartender
Posts: 715
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

By using lists it will solve my issue?

Well, yes. As it stands, in your action listener, you remove "label1" and "field1". The next time you click the button, you try to remove the same items that you have already removed. Those variables do not magically get reassigned to other items.
 
Mihai Xulescu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:

By using lists it will solve my issue?

Well, yes. As it stands, in your action listener, you remove "label1" and "field1". The next time you click the button, you try to remove the same items that you have already removed. Those variables do not magically get reassigned to other items.



ok...I tried and it gives me this error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException



 
Marshal
Posts: 66623
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are lucky to get that code to compile at all. You can add a panel to a display but not a List<JPanel>, and you cannot cast a List to a panel.
Where did you initialise those Lists? Are you declaring those Lists as fields; it may be possible for them to be local variables and reduce the size of your object.
 
Rancher
Posts: 3094
24
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Overall I am creating a table in swing



Maybe you can actually use a JTable. See the section from the Swing tutorial on How to Use Tables

But what if user wants to delete two rows?



When deleting rows you should always delete in reverse order.

That is delete the last row first. This will prevent your indexes from getting out of sync.
 
Mihai Xulescu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You are lucky to get that code to compile at all. You can add a panel to a display but not a List<JPanel>, and you cannot cast a List to a panel.
Where did you initialise those Lists? Are you declaring those Lists as fields; it may be possible for them to be local variables and reduce the size of your object.



Let me clarify.

1. I am having one JPanel. Inside this JPanel I have an array of JLabel and JTextField. When I click "add" I want it to add JLabels or JTextFields into these two Arrays. When I click "remove", I want them to be removed in the same way they were added (from last to first).
So I have created two arrays as underneath:


And then I added the actionListeners as underneath (code is not working now):



My questions are added there in the comment side. Thanks,
 
Carey Brown
Saloon Keeper
Posts: 6467
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
** Remove component from panel BEFORE removing it from the list.

[Edit] I still agree that using a JTable enclosed  in a JScrollPanel is the way to go.
 
Mihai Xulescu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:** Remove component from panel BEFORE removing it from the list.

[Edit] I still agree that using a JTable enclosed  in a JScrollPanel is the way to go.




ok noted. Since I am learning I will try to apply this and come back (for sure) when I will be stuck again. Will update. Don't close the thread please.

I like it because it is hard!
 
Bartender
Posts: 3673
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for learning purposes:

you can get an array with references to ALL your components without having them to be stored in a List. For instance:
 
Crusading Chameleon likes the size of this ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!