Replacing Components in a JPanel

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hal Vaughan

    Replacing Components in a JPanel

    I have a JPanel in a window and I am putting different components in it as
    the user goes through a setup wizard. In theory, I have an array of
    components (other JPanels), and I want to step through them, displaying
    panel_0, then panel_1....

    I have no problem removing panel_0 from the JPanel parent (I've even done a
    repaint() right after removing to be sure of this), but no matter what I
    do, I can't seem to do an "add(panel_ 1)" to the sampe panel. It's the same
    method I'm using to add the first panel, before I show() the window, but
    once I remove a component and try to add another in its place, I get a
    blank.

    Is there a reason I can't add components? Is it because the window is
    visible? What do I need to do?

    Thanks!

    Hal
  • Stella

    #2
    Re: Replacing Components in a JPanel

    You can try "invalidate ()" on the container. Adding of a component doesn't
    always trigger the layout manager to relayout the container and by
    invalidating the container the layout manager will be triggered to do
    relayout. The first time the layout manager will be triggered by the
    show().
    Stellah


    "Hal Vaughan" <hal@thresholdd igital.com> schreef in bericht
    news:NQhjb.7873 30$uu5.137635@s ccrnsc04...[color=blue]
    > I have a JPanel in a window and I am putting different components in it as
    > the user goes through a setup wizard. In theory, I have an array of
    > components (other JPanels), and I want to step through them, displaying
    > panel_0, then panel_1....
    >
    > I have no problem removing panel_0 from the JPanel parent (I've even done[/color]
    a[color=blue]
    > repaint() right after removing to be sure of this), but no matter what I
    > do, I can't seem to do an "add(panel_ 1)" to the sampe panel. It's the[/color]
    same[color=blue]
    > method I'm using to add the first panel, before I show() the window, but
    > once I remove a component and try to add another in its place, I get a
    > blank.
    >
    > Is there a reason I can't add components? Is it because the window is
    > visible? What do I need to do?
    >
    > Thanks!
    >
    > Hal[/color]


    Comment

    • Raymond DeCampo

      #3
      Re: Replacing Components in a JPanel

      Hal Vaughan wrote:[color=blue]
      > I have a JPanel in a window and I am putting different components in it as
      > the user goes through a setup wizard. In theory, I have an array of
      > components (other JPanels), and I want to step through them, displaying
      > panel_0, then panel_1....
      >
      > I have no problem removing panel_0 from the JPanel parent (I've even done a
      > repaint() right after removing to be sure of this), but no matter what I
      > do, I can't seem to do an "add(panel_ 1)" to the sampe panel. It's the same
      > method I'm using to add the first panel, before I show() the window, but
      > once I remove a component and try to add another in its place, I get a
      > blank.
      >
      > Is there a reason I can't add components? Is it because the window is
      > visible? What do I need to do?
      >[/color]

      Hal,

      I don't see any reason a priori your method doesn't work. However, I
      would recommend using the CardLayout instead. This is exactly what it
      was designed for.

      HTH,
      Ray

      Comment

      • Hal Vaughan

        #4
        Re: Replacing Components in a JPanel

        Raymond DeCampo wrote:
        [color=blue]
        > Hal Vaughan wrote:[color=green]
        >> I have a JPanel in a window and I am putting different components in it
        >> as
        >> the user goes through a setup wizard. In theory, I have an array of
        >> components (other JPanels), and I want to step through them, displaying
        >> panel_0, then panel_1....
        >>
        >> I have no problem removing panel_0 from the JPanel parent (I've even done
        >> a repaint() right after removing to be sure of this), but no matter what
        >> I
        >> do, I can't seem to do an "add(panel_ 1)" to the sampe panel. It's the
        >> same method I'm using to add the first panel, before I show() the window,
        >> but once I remove a component and try to add another in its place, I get
        >> a blank.
        >>
        >> Is there a reason I can't add components? Is it because the window is
        >> visible? What do I need to do?
        >>[/color]
        >
        > Hal,
        >
        > I don't see any reason a priori your method doesn't work. However, I
        > would recommend using the CardLayout instead. This is exactly what it
        > was designed for.[/color]

        Works perfectly. One of the problems of being self-taught is that I seem to
        have missed out on a LOT of the Java API and features. Often I don't even
        know where to look, what to look for, or if what I want to do can be done
        in a much simpler way.

        Thanks!

        Hal
        [color=blue]
        > HTH,
        > Ray[/color]

        Comment

        • Hal Vaughan

          #5
          Re: Replacing Components in a JPanel

          Stella wrote:
          [color=blue]
          > You can try "invalidate ()" on the container. Adding of a component doesn't
          > always trigger the layout manager to relayout the container and by
          > invalidating the container the layout manager will be triggered to do
          > relayout. The first time the layout manager will be triggered by the
          > show().
          > Stellah[/color]

          Tried it. I have no idea why, but it didn't work. Someone below suggested
          using a CardLayout (new to me -- which isn't a surprise).

          Thanks for the idea. I didn't know about invalidate() and will definately
          keep it in mind for similar situations in the future.

          Hal
          [color=blue]
          >
          > "Hal Vaughan" <hal@thresholdd igital.com> schreef in bericht
          > news:NQhjb.7873 30$uu5.137635@s ccrnsc04...[color=green]
          >> I have a JPanel in a window and I am putting different components in it
          >> as
          >> the user goes through a setup wizard. In theory, I have an array of
          >> components (other JPanels), and I want to step through them, displaying
          >> panel_0, then panel_1....
          >>
          >> I have no problem removing panel_0 from the JPanel parent (I've even done[/color]
          > a[color=green]
          >> repaint() right after removing to be sure of this), but no matter what I
          >> do, I can't seem to do an "add(panel_ 1)" to the sampe panel. It's the[/color]
          > same[color=green]
          >> method I'm using to add the first panel, before I show() the window, but
          >> once I remove a component and try to add another in its place, I get a
          >> blank.
          >>
          >> Is there a reason I can't add components? Is it because the window is
          >> visible? What do I need to do?
          >>
          >> Thanks!
          >>
          >> Hal[/color][/color]

          Comment

          • Raymond DeCampo

            #6
            Re: Replacing Components in a JPanel

            Hal Vaughan wrote:[color=blue]
            >
            > Works perfectly. One of the problems of being self-taught is that I seem to
            > have missed out on a LOT of the Java API and features. Often I don't even
            > know where to look, what to look for, or if what I want to do can be done
            > in a much simpler way.
            >[/color]

            I'm glad that worked out for you.

            If you are looking for a means of getting more familar with the Java
            API, I recommend the Java Tutorial from Sun (http://java.sun.com). All
            of the material is available free online, or, if you are a big spender,
            is available in book format as well. I think also, if you do not have a
            perpetual connection, the tutorial is available for download.

            Ray

            Comment

            • Hal Vaughan

              #7
              Re: Replacing Components in a JPanel

              Raymond DeCampo wrote:
              [color=blue]
              > Hal Vaughan wrote:[color=green]
              >>
              >> Works perfectly. One of the problems of being self-taught is that I seem
              >> to
              >> have missed out on a LOT of the Java API and features. Often I don't
              >> even know where to look, what to look for, or if what I want to do can be
              >> done in a much simpler way.
              >>[/color]
              >
              > I'm glad that worked out for you.
              >
              > If you are looking for a means of getting more familar with the Java
              > API, I recommend the Java Tutorial from Sun (http://java.sun.com). All
              > of the material is available free online, or, if you are a big spender,
              > is available in book format as well. I think also, if you do not have a
              > perpetual connection, the tutorial is available for download.
              >
              > Ray[/color]

              Cool! That's the kind of thing I never hear about unless I stumble on it in
              a search.

              Thanks!

              Hal

              Comment

              Working...