Author
enabling the scrollbar when components are dynamically added in jpanel
GnanaPrakasam Rathinasabapathi
Greenhorn
Joined: Apr 21, 2009
Posts: 8
Hi all,
I am new to java swing.Can anyone tell me how to activate the scroll bar for a jpanel in which i am adding graphics contents dynamicallly.My code goes here..
import java.awt.BorderLayout ;
import java.awt.Color ;
import java.awt.Container ;
import java.awt.Dimension ;
import java.awt.Graphics ;
import java.awt.LayoutManager ;
import java.awt.event.AdjustmentEvent ;
import java.awt.event.AdjustmentListener ;
import java.awt.event.WindowAdapter ;
import java.awt.event.WindowEvent ;
import javax.swing.JFrame ;
import javax.swing.JPanel ;
import javax.swing.JScrollBar ;
public class Animation2 extends JPanel {
JScrollBar vbar;
public void paintComponent(Graphics g) {
super.paintComponent(g);
int x=10,y=10,x1=0,y1=0,x2=0,y2=0;
JScrollBar hbar = new JScrollBar (JScrollBar.HORIZONTAL, 30, 20, 0, 300);
final JScrollBar vbar1 = new JScrollBar (JScrollBar.VERTICAL, 30, 40, 0, 300);
hbar.setUnitIncrement(2);
hbar.setBlockIncrement(1);
for(int i=0;i<15;i++)
{
g.setColor(Color.blue);
g.drawRect(x, y, 100, 100);
g.setColor(Color.red);
g.drawString("hai",x+50 ,y+50);
g.setColor(Color.YELLOW);
x1=x+100;
y1=y+50;
x2=x1+60;
y2=y1;
//System.out.println("Start line (x1,y1),(x2,y2)"+x1+" "+y1+" "+x1+" "+y2);
g.drawLine(x1,y1,x2 ,y2);
x1=x2;
y1=y2;
x2=x1;
y2=y1+60;
g.setColor(Color.red);
//System.out.println("end line (x1,y1),(x2,y2)"+x1+" "+y1+" "+x2+" "+y2);
g.drawLine(x1, y1, x2, y2);
x+=100+10;
y+=100+10;
}
setAutoscrolls(true);
setLayout(new BorderLayout ());
vbar1.addAdjustmentListener(new AdjustmentListener () {
public void adjustmentValueChanged(AdjustmentEvent ae) {
if (vbar1.getValueIsAdjusting())
return;
System.out.println("Value of vertical scroll bar: " + ae.getValue());
}
});
hbar.addAdjustmentListener(new AdjustmentListener () {
public void adjustmentValueChanged(AdjustmentEvent ae) {
System.out.println("Value of horizontal scroll bar: " + ae.getValue());
}
});
add(hbar, BorderLayout.SOUTH);
add(vbar1, BorderLayout.EAST);
}
public static void main(String [] args) {
/*JPanel jp=new JPanel ();
jp.setAutoscrolls(true);
jp.setBackground(Color.green);
jp.add*/
Animation2 a2=new Animation2();
a2.setPreferredSize(new Dimension(1000,900));
JFrame frame = new JFrame ();
frame.setTitle("DrawRect");
frame.setPreferredSize(new Dimension(1000,900));
frame.addWindowListener(new WindowAdapter () {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
a2.setAutoscrolls(true);
Container contentPane = frame.getContentPane();
contentPane.add(a2,BorderLayout.LINE_START);
//contentPane.add(scroller,BorderLayout.CENTER);
//contentPane.add(new JScrollBar (JScrollBar.VERTICAL, 0,5, 0,10));
frame.show();
}
}
.....
I have tried a lot.But couldnt get. Only the rectangles are drawn.I have tried autoscrolls property also. So please any one help.
Thanks,
Regards,
Gnanaprakasam
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
Try calling revalidate() on the JPanel .
And please Use Code Tags .
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted Apr 21, 2009 13:28:16
0
paintComponent() is for painting......only
do not create and add components in paintComponent
do not create and add listeners in paintComponent
do not set the layout in paintComponent
etc
etc
paintComponent() is called numerous times, duplicating all the above - particularly the listeners
fix all of that, then repost with your updated problem
GnanaPrakasam Rathinasabapathi
Greenhorn
Joined: Apr 21, 2009
Posts: 8
Hi,
Thanks for the reply. i will do necessary changes and post the problem again.
Regards,
Gnanaprakasam
subject: enabling the scrollbar when components are dynamically added in jpanel