• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

cannot get stuff to repaint - please help

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JComponent method revalidate is one of the luxuries of Swing.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
reply
    Bookmark Topic Watch Topic
  • New Topic