Home arrow static arrow Java Programming [Archive] - Question on ScrollPane - AWT.
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Question on ScrollPane - AWT.
This topic has 3 replies on 1 page.

Posts:8
Registered: 4/21/99
Question on ScrollPane - AWT.  
Jun 23, 2004 7:22 PM



 
Hi,

I have the following code. When the program is launched, it has a check box "Add", when ever the user clicks on the add check box, a new check box is added. When the check box crosses the screen view, the vertical scroll bar do not appear automatically. Can any one le me know how to get the veritical scroll bar.

Thanks,
Mohan


import java.awt.*;
import java.awt.event.*;

public class AWTSp {

public static void main(String[] args) {

Frame f = new Frame();

f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0); }
});

ScrollPane sp = new ScrollPane();
sp.add(new AWTPanel());
f.add(sp);
f.setSize(250,250);
f.setLocation(200,200);
f.setVisible(true);

}
}

class AWTPanel extends Panel{

int x=10;
int y=10;

public AWTPanel() {
setLayout(null);
Checkbox cb = new Checkbox("Add");
cb.addItemListener(new java.awt.event.ItemListener()
{
public void
itemStateChanged(java.awt.event.ItemEvent evt) {
checkBoxSelected(evt);
}
});
add(cb);
cb.setBounds(x, y, 40, 10);
}

private void
checkBoxSelected(java.awt.event.ItemEvent evt) {
int iComp = getComponentCount();
Checkbox cb = new Checkbox("test");
System.out.println("aaaa : "+iComp);
iComp++;
cb.setBounds(x, iComp*y, 40, 10);
add(cb);
validate();
}

public Dimension getPreferredSize() {
int iComps = getComponentCount();
return new Dimension(10, iComps*10);
}
}

 

Posts:1,135
Registered: 1/16/04
Re: Question on ScrollPane - AWT.  
Jun 24, 2004 1:11 AM (reply 1 of 3)



 
Okay, I checked it out and verified the problem, the Panel new grew in size while you added the components so the bar would not "automatically" show up until you resized the window...

So what I did was first change AWTPanel from extending Panel to extending ScrollPane... Just so that I could keep everything contained in a similar structure as your original code...
Then I created a Panel in AWTPanel and added to your itemStateChanged() method a little more code that would get all the componets (CheckBoxes) and set the size for the Panel to their total size...

So what happens is the Panel height is increased everytime you add a component until it is larger than the ScrollPane's viewport.
At which point the Vertical ScrollBar shows up "automatically"...
import java.awt.*;import java.awt.event.*; public class AWTSp { public static void main(String[] args) { Frame f = new Frame();f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0); }});f.setSize(250,250);f.setLocation(200,200);AWTPanelScr sp = new AWTPanelScr(); // instantiates new AWTPanelScr which extends ScrollPanef.add(sp);// add  AWTPanelScr to Frame ff.setVisible(true);}}class AWTPanelScr extends ScrollPane{ // now extends ScrollPaneint x=10;int y=10;Panel pan;  // Panel pan declaredpublic AWTPanelScr() {super(); // call to ScrollPane's contructor which displays ScrollBars as need by defaultsetSize(250,250); // Set the size for the ScrollPane and viewportpan = new Panel(); // Panel pan instantiatedpan.setLayout(null); // like the original, layout set to nullCheckbox cb = new Checkbox("Add");pan.add(cb); // Checkbox cb added to Panel panadd(pan); // Panel pan added to AWTPanelScr which extends ScrollPanecb.addItemListener(new java.awt.event.ItemListener(){public voiditemStateChanged(java.awt.event.ItemEvent evt) {checkBoxSelected(evt);}});cb.setBounds(x, y, 40, 10);pan.add(cb); // changed this so that it adds new Checkboxes to Panel pan// get the components from Panel pan in sticks it in a Component arrayComponent[] cdarray = pan.getComponents(); // array createdint newSize = 0; // set newSize to 0 which will be used to increase Panel pans sizefor (int i =0; i < cdarray.length; i++){  // loop through all Components in arraynewSize += cdarray[i].getHeight(); // get the Height from each components and adds it} // to newSizepan.setSize(200,newSize);// "resizes" Panel pan with a new height once it get big enough the//ScrollBars will appear...pan.validate();// re-layout components in pan,validate(); // and same for AWTPanelScr}private voidcheckBoxSelected(java.awt.event.ItemEvent evt) {int iComp = pan.getComponentCount();Checkbox cb = new Checkbox("test");System.out.println("aaaa : "+iComp);iComp++;cb.setBounds(x, iComp*y, 40, 10);pan.add(cb);/* This does the same as the above commented code */Component[] cdarray = pan.getComponents();int newSize = 0;for (int i =0; i < cdarray.length; i++){newSize += cdarray[i].getHeight();}pan.setSize(200,newSize);pan.validate();validate(); // end modifications... - MaxxDmg...}public Dimension getPreferredSize() {int iComps = pan.getComponentCount();return new Dimension(10, iComps*10);}}


I commented the changes so that you can see what was changed to resolve the problem...
You can move those loops into a method and simplify the code if you like
I tried to stay within the bounds of the original code so that you can see for yourself how it differs from the original...

Have fun...

- MaxxDmg...

- ' He who never sleeps... '
 

Posts:8
Registered: 4/21/99
Re: Question on ScrollPane - AWT.  
Jun 24, 2004 9:10 AM (reply 2 of 3)



 
Hi,

Thanks for your help. My problem is solved.

Thanks,
Mohan
 

Posts:8
Registered: 4/21/99
Re: Question on ScrollPane - AWT.  
Jul 21, 2004 2:02 PM (reply 3 of 3)



 
Hi,

I have a panel with some components in it and this panel is added to the scroll panel.

When the screen is displayed, the components are not fully visible. I have to scroll the screen to see the components.

Instead of me doing this scrolling manually, I want to do the scrolling automatically, when the screen is launced so that I can see the components.

Can I use ScrollPane::setScrollPosition(), actually I tried to use this and it did not help me.

Can you pls help me on this.

I have posted this in general forum also.

Thanks,
 
This topic has 3 replies on 1 page.