• Post Reply Bookmark Topic Watch Topic
  • New Topic

error when try to singleton internal frame  RSS feed

 
cliff perkasa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to singleton internal frame, the code can run but when second time klik data employee get
error



this my internal code


i get error java.lang.IllegalArgumentException at desktop.add(data.getInstance());

please help me figure what went wrong and how to fix it

reference = i am using eclipse and windowbuilder
 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the full error, including stack trace and description?
 
cliff perkasa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Can you post the full error, including stack trace and description?


this is the full error
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to put a component in the wrong position, it says. Please show us which line that exception comes from

And welcome to the Ranch
 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bit of a guess here, but on the second click of the button you would be adding the same JInternalFrame to the desktop.
It's already in there, so I can imagine that might cause issues.
 
cliff perkasa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You are trying to put a component in the wrong position, it says. Please show us which line that exception comes from

And welcome to the Ranch

Thanks,
at line


i try to add the Jinternalframe to jdesktopPane
 
cliff perkasa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Bit of a guess here, but on the second click of the button you would be adding the same JInternalFrame to the desktop.
It's already in there, so I can imagine that might cause issues.

well my aim to limit this same frame to show more than once, i try to add singleton at internalframe and
call it and put it on desktop, is there wrong with my step
 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I know a component cannot exist in more than one parent, so I would expect a component cannot exist more than once in the same parent.

Why not create an additional component?
 
cliff perkasa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Well, I know a component cannot exist in more than one parent, so I would expect a component cannot exist more than once in the same parent.

Why not create an additional component?


well i still don't get it, please explain with "create additional component"
 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have one JInternalFrame instance (called myInstance).
It can only exist in one place at a time, and adding it to the frame a second time may be (I'm not saying it is) the cause of your exception.
So I  am asking why you don't just create a new instance when you need it?
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you trying to create singletons in the first place? And why are you using double checked locking? That isn't the correct way to implement a singleton.
 
cliff perkasa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why are you trying to create singletons in the first place? And why are you using double checked locking? That isn't the correct way to implement a singleton.


my aim is to prevent mutiple window of this internalframe, so i try to use singleton for this purpose, please correct me if i'm wrong
 
Paul Clapham
Sheriff
Posts: 22713
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cliff perkasa wrote:my aim is to prevent mutiple window of this internalframe


That doesn't answer the question, why do you want to have a singleton? What problems arise when you have more than one instance of the InternalFrame?
 
cliff perkasa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
cliff perkasa wrote:my aim is to prevent mutiple window of this internalframe


That doesn't answer the question, why do you want to have a singleton? What problems arise when you have more than one instance of the InternalFrame?


well i want to prevent the desktop from overcrowded with same instances, and that internal frame is for view table, also if i create another internalframe to modify(edit, insert, delete), i want to make that instance to be updated.
about singleton, i find singleton can prevent mutiple instance of same class when googling, so i try to use it for my problem
 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then, if you only want one instance, then why are you adding it to the desktop each time the button is clicked?


That seems to imply you want to add more than one.
 
cliff perkasa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Then, if you only want one instance, then why are you adding it to the desktop each time the button is clicked?


That seems to imply you want to add more than one.


to make it visible at desktop
 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, by the time you click the button the second time, you've already added it.
There's no where that removes the frame, so that means you are adding it twice...which apparently you don't want to do.
 
cliff perkasa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:But, by the time you click the button the second time, you've already added it.
There's no where that removes the frame, so that means you are adding it twice...which apparently you don't want to do.

ok i get it now, but how to fix it?  if i not wrong, remove internalframe and add new but how to know that same internalframe already exist at desktop 
 
Paul Clapham
Sheriff
Posts: 22713
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cliff perkasa wrote:well i want to prevent the desktop from overcrowded with same instances


Then write code which doesn't do that. Making a singleton isn't going to affect what your GUI-creating code does... except that it might throw an exception if you try to add the instance in more than one place on the screen. Actually it looks like that might be what's actually happening, but you don't seem to be satisfied with that solution to your problem. And rightly so because it isn't a solution to your problem.

 
Paul Clapham
Sheriff
Posts: 22713
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I'm like everyone else answering here, it looks like. You already have an InternalFrame in your GUI as soon as it starts up, so you don't need to add another one ever. You've got a perfectly good method to refresh the contents of that InternalFrame, so just call that method when you need it update.
 
cliff perkasa
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:And I'm like everyone else answering here, it looks like. You already have an InternalFrame in your GUI as soon as it starts up, so you don't need to add another one ever. You've got a perfectly good method to refresh the contents of that InternalFrame, so just call that method when you need it update.


thanks for the advice, so i try another way

i change my maincode:


at internal frame:


sorry now is not about singleton topic but to limit only one same instance.
i try to limit internalframe by checking it visible or not, if not add new one else call refresh method  and set internalframe to hide when close but the code above still not working
what lacking on my code
 
Paul Clapham
Sheriff
Posts: 22713
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why make it so complicated? Here's what I think you should do:

1. Create the InternalFrame and add it to the GUI when you create and initialize the GUI. Probably it won't have any data in it at this point.

2. When you want to show new data in the InternalFrame, call the method which you put there for that purpose.

I don't understand what you're saying about closing something, either. Seems to me you should just close the GUI when you're finished using it, I don't see why you need to close the InternalFrame separately.
 
cliff perkasa
Greenhorn
Posts: 13
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, problem already solve, not using singleton for mutiple instance same internalframe, i remove setDefaultCloseOperation(HIDE_ON_CLOSE) at internal frame and at main class
i change code

and at public class main make internalframe data as local variabel


Thanks for all advice
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!