aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes cannot get stuff to repaint - please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "cannot get stuff to repaint - please help" Watch "cannot get stuff to repaint - please help" New topic
Author

cannot get stuff to repaint - please help

Alex Gorh
Greenhorn

Joined: Jan 16, 2004
Posts: 10
Hello,

This is probably an easy question, but I cannot seem to figure this out. This is a simplified example of what I'm trying to do:

JPanel myPanel = new JPanel();
JButton myButton = new JButton("HEY");
myPanel.add(myButton);
<add panel to JDialog, show the window>

JButton myNewButton = new JButton("REPLACEMENT");
myButton = myNewButton;

Now, what do I need to do to get panel and the window to refresh and show the new myButton object that has the "REPLACEMENT" tag? Again, this is just a simplified example of what I'm trying to do... I've played around with repaint() and validate()/invalidate() methods for components, panels, windows - but none seem to work. How can I do this? How can I get the thing to repaint if I'm switching the component object underneath? Thanks!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I did some testing and I don't think it can be done this way. What you will need to do is something like...



GenRocket - Experts at Building Test Data
Alex Gorh
Greenhorn

Joined: Jan 16, 2004
Posts: 10
I believe I tried that... It ends up removing the original button, but the new button never appears. I believe I was tesing this with JTable(s) though. I'll try doing it with buttons tomorrow at work. Thanks!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Alex Gorh:
I believe I tried that... It ends up removing the original button, but the new button never appears. I believe I was tesing this with JTable(s) though. I'll try doing it with buttons tomorrow at work. Thanks!


I am not sure why it didn't work for you. It should work with any component. If you don't get it working, show us the code and we will help. Good luck.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The JComponent method revalidate is one of the luxuries of Swing.
Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
It looks like you are just changing the text on the button. Would it be easier to just do a myButton.setText("Replace")? Then you don't have to go through any repaint, etc. It will be handled for you.

If you really have to add and remove the button. You could add the button with a specific index -

myPanel.add(myButton, 1);


Then when you need to replace the button, do a myPanel.remove(1); (This might be the wrong method signature, but you get the idea.) You'll always be able to replace the button you need by just using the same index for adding and removing.

Regards,
Aaron R>
 
 
subject: cannot get stuff to repaint - please help