• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cannot get stuff to repaint - please help

 
Alex Gorh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did some testing and I don't think it can be done this way. What you will need to do is something like...

 
Alex Gorh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JComponent method revalidate is one of the luxuries of Swing.
 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic