Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enabling the scrollbar when components are dynamically added in jpanel

 
GnanaPrakasam Rathinasabapathi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try calling revalidate() on the JPanel.

And please Use Code Tags.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the reply. i will do necessary changes and post the problem again.
Regards,
Gnanaprakasam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic