This is my code i want to add JscrolPanel on jframe i used jpanel and paintCOmponent()
[heightlight]
package other;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.ScrollPane;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import javax.swing.*;
import javax.swing.plaf.ComboBoxUI;
public class Test extends JPanel implements ActionListener
{
JButton button;
JLabel label;
JTextField textField;
JScrollPane pane;
JPanel panel;
JFrame frame;
Container c;
static int val;
static int incValue=1;
int xa,ya,xb,yb,x1,y1;
int xc,yc,xd,yd;
int lsb=0,msb=7;
int maxValue=20*incValue;
char val1='A',val2='B';
Thread runner=null;
JComboBox cmb;
public Test()
{
setLayout(null);
setBackground(Color.LIGHT_GRAY);
textField=new JTextField();
label=new JLabel("Enter Clock value: ");
button=new JButton("Ok");
cmb=new JComboBox();
cmb.addItem("1");
cmb.addItem("2");
cmb.addItem("3");
System.out.println("in const");
label.setBounds(280,550,120,20);
textField.setBounds(410,550,50,25);
button.setBounds(480,550,70,25);
cmb.setBounds(410,580,70,25);
this.setBounds(0,0,900,700);
add(button);
add(textField);
add(label);
add(cmb);
button.addActionListener(this);
cmb.addActionListener(this);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
xa=100;ya=120;
xb=xa+maxValue/2;yb=ya;
xc=xb;yc=yb+maxValue/2;
xd=xa+maxValue;yd=yc;
maxValue=20*incValue;
System.out.println("maxvalu with incValu>>"+maxValue);
Font f=new Font("serif", Font.BOLD,20);
g.setColor(Color.black);
g.drawLine(100,100,100,530); // Draw Y axis.
//g.drawLine(100,500,,500); //Draw X axis.
g.drawString("CLK",xa-40 ,ya);
g.drawString("DATA", xa-40, 300);
g.drawString("["+msb+":"+lsb+"]", xa-40, 312);
System.out.println("incvlaue :"+incValue);
g.setFont(f);
g.setColor(Color.red);
g.drawString("Timing Analysis", 500,30);
Graphics2D g2d = (Graphics2D)g;
Stroke stroke=new BasicStroke(1f, BasicStroke.CAP_ROUND,BasicStroke.JOIN_BEVEL,1f, new float[] {2f}, 0f);
Stroke drawingStroke = new BasicStroke(2);
for(int i=1;i<=val;i++)
{
g2d.setColor(Color.black);
g2d.setStroke(stroke);
Line2D line = new Line2D.Double(xb,yb,xb,500);
g2d.draw (line);
Line2D line2 = new Line2D.Double(xd,yd-50,xd,500);
g2d.draw (line2);
g2d.setStroke(drawingStroke);
if(x1<xd)
{
x1=xd;
g.setColor(Color.black);
g.drawLine(50,500,x1,500); //Draw X axis.
}
g.drawString("t"+i,xa+20,ya-5);
g.setColor(Color.blue);
g.drawLine(xa,ya,xb,yb); //h1
g.setColor(Color.blue);
g.drawLine(xb,yb,xc,yc); //v1
g.setColor(Color.blue);
g.drawLine(xc,yc,xd,yd); //h2
if(i%2==0)
g.drawString(""+val1,(xa+xd)/2, 300);
else
g.drawString(""+val2,(xa+xd)/2 , 300);
g.drawLine(xa, 300, (xa+xb)/2, 275);
g.drawLine((xa+xb)/2, 275, (xc+xd)/2, 275);
g.drawLine((xc+xd)/2, 275, xd, 300);
g.drawLine((xa+xb)/2, 325, (xc+xd)/2, 325);
g.drawLine(xa, 300, (xa+xb)/2, 325);
g.drawLine((xc+xd)/2, 325, xd, 300);
xa=xa+maxValue;
xb=xa+maxValue/2;
yb=ya;
xc=xb;
yc=yb+maxValue/2;
xd=xa+maxValue;
yd=yc;
}
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
JScrollPane pane=new JScrollPane(new Test(),JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JS crollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(pane);
// Drawjp = new DrawingPanel(); // DrawingPanel is a subclass of JPanel which implements paint();
// JScrollPane scroller = new JScrollPane(jp);
// scroller.setVerticalScrollBarPolicy(ScrollPaneCons tants.VERTICAL_SCROLLBAR_ALWAYS);
// add(scroller);
// add(jp);
//
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
//frame.setSize(900,700);
frame.setExtendedState(frame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
val=Integer.parseInt(textField.getText());
repaint();
}
if(e.getSource()==cmb)
{
if(cmb.getSelectedItem().toString()=="1")
{
incValue=1;
}
if(cmb.getSelectedItem().toString()=="2")
{
incValue=2;
}
if(cmb.getSelectedItem().toString()=="3")
{
incValue=3;
}
}
}
// @Override
// public void run() {
//
//
// // lower ThreadPriority
// Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);
//
// // run a long while (true) this means in our case "always"
// while (true)
// {
//
// // repaint the applet
// repaint();
//
// try
// {
// // Stop thread for 20 milliseconds
// Thread.sleep (100);
// }
// catch (InterruptedException ex)
// {
// // do nothing
// }
//
// // set ThreadPriority to maximum value
// Thread.currentThread().setPriority(Thread.MAX_PRIO RITY);
//
// }
//
//
// // TODO Auto-generated method stub
// // while(runner!=null)
// // repaint();
// // try
// // {
// // Thread.sleep(100);
// // }catch(InterruptedException i)
// // {
// // System.out.println(i.getMessage());
// // }
//
// }
// public void start()
// {
// if(runner==null)
// {
// runner=new Thread(this);
// runner.start();
// }
// }
}
[/heightlight]