Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: add jscrollpane to jframe

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default add jscrollpane to jframe

    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]


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: add jscrollpane to jframe

    Please edit your post, select the code and wrap it in code tags to preserve the formatting.
    See: BB Code List - Java Programming Forums - The Java Community

Similar Threads

  1. PUTTING JSCROLLPANE INSIDE JFRAME
    By fueledbyjace in forum AWT / Java Swing
    Replies: 0
    Last Post: September 15th, 2011, 07:21 AM
  2. using objects from one jFrame on a different jFrame
    By Taskeen in forum AWT / Java Swing
    Replies: 3
    Last Post: September 14th, 2011, 08:51 AM
  3. connecting jframe to another jframe
    By ajtambalo in forum Member Introductions
    Replies: 2
    Last Post: May 11th, 2011, 11:24 AM
  4. JScrollPane
    By MysticDeath in forum AWT / Java Swing
    Replies: 1
    Last Post: February 17th, 2010, 10:21 PM
  5. JTable in JScrollPane
    By alwayslearner in forum AWT / Java Swing
    Replies: 1
    Last Post: January 31st, 2010, 10:24 PM

Tags for this Thread