dcsimg
Growing Canvas inside a scrollpane. The Scrollpane can't detect that the Canvas size increased. (The scrollbar of the scrollpane remains unchanged ??)
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sakibul_Khan
Posted On:   Friday, April 11, 2003 09:43 AM

Hi, I have a Canvas inside a Scrollpane. I want to increase the size of the canvas, when a certain event occurs, but even though the Canvas size increases, which I double check using "System.out.println("--> " + c.getSize());", but the scrollbar of the Scrollpane remains unchanged. Hence, it is as if the size of the canvas didn't change, even though I can see in the "System.out.println("--> " + c.getSize());" statement that the Canvas size has changed. Hence, the problem is that the Scrollpane is not detecting that the Canvas size has increased. Thanx in advance. -------------------------------SAKIB    More>>

Hi,



I have a Canvas inside a Scrollpane. I want to increase the size of the canvas, when a certain event occurs, but even though the Canvas size increases, which I double check using "System.out.println("--> " + c.getSize());", but the scrollbar of the Scrollpane remains unchanged. Hence, it is as if the size of the canvas didn't change, even though I can see in the "System.out.println("--> " + c.getSize());" statement that the Canvas size has changed.



Hence, the problem is that the Scrollpane is not detecting that the Canvas size has increased.



Thanx in advance.



-------------------------------SAKIB





The following is my code (NB. Clicking on the button (@ West) increases the size of the Canvas.):-

			
package client;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class CanvasScroll extends Applet {
Canvas c;
ScrollPane s;

private BorderLayout borderLayout1 = new BorderLayout();
private Button button1 = new Button();
Vector vect = new Vector();

int canvasWidth = 75, canvasHeight = 400;

//double buffering PART-1
Image myOffScreenImage;
Graphics myOffScreenGraphics;

//double buffering PART-2 & 3 together
public void update(Graphics g){
myOffScreenImage = createImage(getSize().width, getSize().height);
myOffScreenGraphics = myOffScreenImage.getGraphics();

paint(myOffScreenGraphics);
g.drawImage(myOffScreenImage, 0, 0, this);
}
public void init() {
setLayout(new BorderLayout());
s = new ScrollPane();
s.setSize(100,100);
add("Center", s);
c = new myCanvas();
c.setSize(canvasWidth,canvasHeight);//no horizontal scrollbar's//75, 400
s.add(c);

button1.setLabel("button1");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});

this.add(button1, BorderLayout.WEST);
}

class myCanvas extends Canvas {

//double buffering PART-1
Image myOffScreenImage;
Graphics myOffScreenGraphics;

//double buffering PART-2 & 3 together
public void update(Graphics g){
myOffScreenImage = createImage(getSize().width, getSize().height);
myOffScreenGraphics = myOffScreenImage.getGraphics();

paint(myOffScreenGraphics);
g.drawImage(myOffScreenImage, 0, 0, this);
}

public myCanvas(){ super(); }

public void paint(Graphics g){
int x = 10, y = 10, y_diff = 20;
g.drawString("Hello World = " + y, x, y); y += y_diff;
g.drawString("Hello World = " + y, x, y); y += y_diff;
g.drawString("Hello World = " + y, x, y); y += y_diff;
g.drawString("Hello World = " + y, x, y); y += y_diff;
g.drawString("Hello World = " + y, x, y); y += y_diff;
g.drawString("Hello World = " + y, x, y); y += y_diff;

for(int i = 0; i < vect.size(); i++){
g.drawString((String) vect.elementAt(i), x, y); y += y_diff;
}
}
}

public CanvasScroll(){
try{
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception{}

public void button1_actionPerformed(ActionEvent e){
vect.addElement("Hello World !!!");
canvasHeight += 20;
c.setSize(canvasWidth, canvasHeight);
System.out.println("--> " + c.getSize());
}
}
   <<Less

Re: Growing Canvas inside a scrollpane. The Scrollpane can't detect that the Canvas size increased....

Posted By:   Bahman_Barzideh  
Posted On:   Friday, April 11, 2003 02:50 PM

I ran your code on my Mac OS X machine under both Java 1.3.1 and
Java 1.4.1. Here is what I found:

  • Under Java 1.4.1 your code works as you expect it (the Canvas get a bit taller and the vertical scrollbar's control get's a bit
    smaller with each click of button 1).

  • Under java 1.3.1 the code does what your say above not what you would
    expect. I managed to get around this by forcing the ScrollPane to lay itself out with each click of button 1. Replace your button1_actionPerformed () method with the following:

    public void button1_actionPerformed(ActionEvent e){
    vect.addElement("Hello World !!!");
    canvasHeight += 20;
    c.setSize(canvasWidth, canvasHeight);
    s.doLayout ();
    System.out.println("--> " + c.getSize());
    }
  • About | Sitemap | Contact