Results 1 to 5 of 5
  1. #1
    kite949372 is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default Updating display methods?

    Hello, just a simple question thats been bugging me. Is there a standard way to update a display on a JFrame or a JPanel? So far I've gone through doing the pack and then set visible every time something changed until I realized I could skip the pack after the initial pack. However this got me wondering if there was a better way to update the display? Like say if I have a button that totally changes the display of the frame or panel, such as removing the buttong and placing another button and adding a text field or anything like that, what would be the best way to have the computer update it? setVisible? repaint? Something else?

    Thanks.

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    If you use Swing:
    Java Code:
    SwingUtilities.updateComponentTreeUI(Component c) ;
    In this case if ,for instance you have JButton b1 on JPanel p1 and you want to remove the button it you do write such line:
    Java Code:
    p1.remove(b1);
    SwingUtilities.updateComponentTreeUI(p1) ;

    or you can also use repaint method.
    Last edited by serjant; 09-16-2008 at 08:28 AM.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Actually updateComponentTreeUI() also calling the repaint on the component. So if you want directly can call repaint method.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    what would be the best way to have the computer update it
    When you make changes to a container (such as adding/removing or altering the size/location of child components) you have to tell it to redo its layout. Two ways to do this are with the JComponent method revalidate (for Swing) or the Component method validate (for AWT). Sometimes you also need to call repaint, but not always. You have to experiment to find out what is required as a minimum.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I wonder if CardLayout is what you really want to use here. You would do well to look over the tutorial on this on the Sun Swing tutorial site.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 0
    Last Post: 07-11-2008, 04:52 PM
  3. Replies: 0
    Last Post: 06-28-2008, 09:26 PM
  4. Updating Graphics
    By Greedful in forum Java 2D
    Replies: 2
    Last Post: 07-20-2007, 07:12 PM
  5. Updating into 2 tables in the DB
    By yuchuang in forum New To Java
    Replies: 2
    Last Post: 05-12-2007, 06:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •