validate() help needed
im having problems when validating something once it has been deleted. this is my buildDeleteDialog method:
publicvoid buildDeleteDialog(){
delete = JOptionPane.showConfirmDialog(this,"Are you sure you want to delete this contact?","Delete", JOptionPane.YES_NO_OPTION);
if(delete == JOptionPane.YES_OPTION){
getParent().remove(this);
contactIO.deleteContact(this);
validate();
}
}
what happens is that when the user clicks on the delete button, this method is called. its basically a JOptionPane that asks if the user wants to delete the contact. if they choose yes, then the contact is removed from the JPanel that it was held inside, and the contacts info is also deleted from a database.
now heres my problem: im trying to validate something that has just been deleted, instead i should validate the JFrame that is was inside of. but that JFrame is in another class! so how can i validate a JFrame when it is in another class?
[1318 byte] By [
Alex1989a] at [2007-11-27 6:09:56]

# 1
> instead i should validate the JFrame that is was inside of. All you need to do is validate() the parent component and you have access to that with the getParent() method. Of course you would need to get the parent before removing the component.
# 2
when i changed validate() to getParent().validate(); i got this exception:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at JavaPhonebook.ContactPanel.buildDeleteDialog(ContactPanel.java:156)
at JavaPhonebook.ContactPanel.actionPerformed(ContactPanel.java:200)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
for some strange reason the contact does actually get deleted but only after a few seconds.
Message was edited by:
Alex1989
# 3
I already told you that wouln't work. Read my answer completely!
# 4
hmm... so how would i get the parent before i try validation? i tried called the getParent() method on the line above validate() but that didnt work :(
# 5
what are you getting a parent of? the component on the GUI or the object in which you are currently working (this)? Have you tested the getParent for null?
# 6
its getting a JPanel javax.swing.JPanel[,0,0,342x1110,layout=javax.swing.BoxLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
thats because each ContactPanel is added to a JPanel. the JPanel is added to a JTabbedPane and the JTabbedPane is finally added to a JFrame. so im trying to get to that JFrame :D
i just tried to see if i could use multiple getParent() methods after each other and it worked. this gets the JFrame as the parent: getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent().validate();
but im still getting a NullPointerException
Message was edited by:
Alex1989
# 7
> i tried called the getParent() method on the line above validate()
Of course it doesn't work. The component has already been removed from the panel to it doesn't have a parent anymore.
Read my answer 10 times:
Of course you would need to get the parent BEFORE removing the component.
# 8
only thing i can see now is to put this method in a different class and try mod it a bit, doesnt seem like itll be able to work how it is
# 9
if you follow camickr's advice, could you create a Container myParent = getparent() before you remove your object, then after you remove the GUI object, you validate the parent variable?
public void buildDeleteDialog(){
delete = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this contact?", "Delete", JOptionPane.YES_NO_OPTION);
if(delete == JOptionPane.YES_OPTION){
Container myParent = getParent();
getParent().remove(this);
contactIO.deleteContact(this);
myParent.validate();
}
}
Gotta warn you tho, I'm trying to learn this stuff myself. I'm just trying to learn and what Camickr posted, to know if it works like I think it might work.
Message was edited by:
petes1234
# 10
AHA! thnx petes1234! thnx also camickr, i know i dnt like spoon feeding :P
# 11
do you need a repaint in there too?also, don't thank me, thank camickr.apologies to camickr for stepping in here.