Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. JScrollPane child dimesniosn not updating

JScrollPane child dimesniosn not updating

Scheduled Pinned Locked Moved Java
graphicshelpjavaquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rareseu
    wrote on last edited by
    #1

    Hy guys ! i'm running into a bit of trouble with a JScrollPane, the pane itself has to stay the same size but the viewport inside the pane has to get longer as i add stuff to it( right now i'm just drawing circles downwards) the problem is that when the viewport gets big enough to require scrolling my drawing dissapears my globals :

    //global variables :
    //params for the circle i draw :
    public int positionY=10;
    public int positionX=20;
    public int width=30;
    public int height=30;

    //used to set dimension of the viewport :
    private Dimension dim=new Dimension();
    
    //the viewport in which i draw, it extends JViewport
    private MyJViewport drawHere=new MyJViewport();
    
    private JScrollPane displayResult;
    
    private JButton jButton1;
    

    the MyJViewport class :

    class MyJViewport extends JViewport {

        @Override
        public void paint(Graphics g) {
            g.setColor(Color.BLUE);
            g.fillOval(positionX, positionY, width, height);
    
        }//paint
        
    }//MyJViewport
    

    in the constructor i instance the scrollpane :

    displayResult = new javax.swing.JScrollPane(drawHere);
    displayResult.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    displayResult.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    the event handler for the button press:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    //the new circle will be positioned under the previous
    positionY+=height;

        // if the point where the circle gets drawn exceeds the bounds of the viewport
        // resize the viewport
        if (positionY > drawHere.getHeight()) {
                dim.height = drawHere.getHeight() + height;
                dim.width = drawHere.getWidth();
                drawHere.setPreferredSize(dim);
                drawHere.revalidate();
                
            }//if
    
        drawHere.repaint();
    }
    

    can anyone help with this ?

    N 1 Reply Last reply
    0
    • R rareseu

      Hy guys ! i'm running into a bit of trouble with a JScrollPane, the pane itself has to stay the same size but the viewport inside the pane has to get longer as i add stuff to it( right now i'm just drawing circles downwards) the problem is that when the viewport gets big enough to require scrolling my drawing dissapears my globals :

      //global variables :
      //params for the circle i draw :
      public int positionY=10;
      public int positionX=20;
      public int width=30;
      public int height=30;

      //used to set dimension of the viewport :
      private Dimension dim=new Dimension();
      
      //the viewport in which i draw, it extends JViewport
      private MyJViewport drawHere=new MyJViewport();
      
      private JScrollPane displayResult;
      
      private JButton jButton1;
      

      the MyJViewport class :

      class MyJViewport extends JViewport {

          @Override
          public void paint(Graphics g) {
              g.setColor(Color.BLUE);
              g.fillOval(positionX, positionY, width, height);
      
          }//paint
          
      }//MyJViewport
      

      in the constructor i instance the scrollpane :

      displayResult = new javax.swing.JScrollPane(drawHere);
      displayResult.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
      displayResult.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

      the event handler for the button press:

      private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
      //the new circle will be positioned under the previous
      positionY+=height;

          // if the point where the circle gets drawn exceeds the bounds of the viewport
          // resize the viewport
          if (positionY > drawHere.getHeight()) {
                  dim.height = drawHere.getHeight() + height;
                  dim.width = drawHere.getWidth();
                  drawHere.setPreferredSize(dim);
                  drawHere.revalidate();
                  
              }//if
      
          drawHere.repaint();
      }
      

      can anyone help with this ?

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #2

      It looks like you are only painting the last item.


      Panic, Chaos, Destruction. My work here is done.

      R 1 Reply Last reply
      0
      • N Nagy Vilmos

        It looks like you are only painting the last item.


        Panic, Chaos, Destruction. My work here is done.

        R Offline
        R Offline
        rareseu
        wrote on last edited by
        #3

        I don't understand :| , do you mean the last item in the scrollpane ? there is only the viewport in there, can you please explain ?

        N 1 Reply Last reply
        0
        • R rareseu

          I don't understand :| , do you mean the last item in the scrollpane ? there is only the viewport in there, can you please explain ?

          N Offline
          N Offline
          Nagy Vilmos
          wrote on last edited by
          #4

          All your code is doing is painting a circle at the LAST position. The repaint needs to paint EVERY circle. The simplest approach is to have a collection of objects each representing on of the thing you wish to display. Each class knows how and where to piant itself on a given graphic: [this code is not tested]

          // you need a 'circle' class
          class MyCircle
          {
          private int top;
          private int left;
          private int radius; // don't need two vars here

          MyCircle (int top, int left, int radius)
          {
          this.top = top;
          this.left = left;
          this.radius = radius;
          }

          void Repaint (Graphic g)
          {
          g.setColor(Color.BLUE);
          g.fillOval(this.top,
          this.left,
          this.radius,
          this.radius);
          }
          }

          As you add your curcles create a new object with the X,Y,R. Hold these in a list object. On the scrollpane repant iterate through the list and paint each object.


          Panic, Chaos, Destruction. My work here is done.

          R 1 Reply Last reply
          0
          • N Nagy Vilmos

            All your code is doing is painting a circle at the LAST position. The repaint needs to paint EVERY circle. The simplest approach is to have a collection of objects each representing on of the thing you wish to display. Each class knows how and where to piant itself on a given graphic: [this code is not tested]

            // you need a 'circle' class
            class MyCircle
            {
            private int top;
            private int left;
            private int radius; // don't need two vars here

            MyCircle (int top, int left, int radius)
            {
            this.top = top;
            this.left = left;
            this.radius = radius;
            }

            void Repaint (Graphic g)
            {
            g.setColor(Color.BLUE);
            g.fillOval(this.top,
            this.left,
            this.radius,
            this.radius);
            }
            }

            As you add your curcles create a new object with the X,Y,R. Hold these in a list object. On the scrollpane repant iterate through the list and paint each object.


            Panic, Chaos, Destruction. My work here is done.

            R Offline
            R Offline
            rareseu
            wrote on last edited by
            #5

            I see now :) thank you for explaining !

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups