i want to add scroll bar to scroll this page..this page is user log-in form... to add more fields i have to add scroll bar how can i do so ..pls help

package user;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;

import javax.swing.*;

public class form extends JFrame

package user;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;

import javax.swing.*;

public class form extends JFrame
{
 JLabel pic,pic2;
 JLabel l1,l2,l3,l4,l5,p1,l7,l8,l9,l10;
 JTextField t1,t2,t3,t5,t7,t8,t9,t10;
 JPasswordField t6;
 JTextArea t4;
 JPanel p;
 JComboBox combo;
 String[] year= new String [4];
    public form()
    {   
        setLayout(null);
        ImageIcon i=new ImageIcon("green_background.jpg");
        pic=new JLabel(i);
        pic.setBounds(0,0,1500,1000);

        /*Container p=getContentPane();
    //  p.setBounds(0,0,1500,1000);
        add(p);
        JScrollPane scrollPane = new JScrollPane(p);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBounds(0,0,1500,1000);
        p.add(scrollPane);*/

        ImageIcon i2=new ImageIcon("rform.jpg");
        pic2=new JLabel(i2);
        pic2.setBounds(270,0,800,1000);

        l1=new JLabel("FirstName :");
        l1.setBounds(400,160,150,30);
        Font f=new Font("Lucida Handwriting",Font.ITALIC|Font.BOLD,20);
        l1.setFont(f);
        add(l1);
        t1=new JTextField(20);
        t1.setBounds(600,160,350,30);
        add(t1);

        l2=new JLabel("LastName :");
        l2.setBounds(400,220,150,30);
        l2.setFont(f);
        add(l2);

        t2=new JTextField(20);
        t2.setBounds(600,220,350,30);
        add(t2);

        l3=new JLabel("FatherName :");
        l3.setBounds(400,280,170,30);
        l3.setFont(f);
        add(l3);
        t3=new JTextField(20);
        t3.setBounds(600,280,350,30);
        add(t3);

        l4=new JLabel("Adress :");
        l4.setBounds(400,340,150,30);
        l4.setFont(f);
        add(l4);
        t4=new JTextArea(3,1);
        t4.setBounds(600,340,350,70);
        add(t4);

        l5=new JLabel("UserName:");
        l5.setBounds(400,440,150,30);
        l5.setFont(f);
        add(l5);
        t5=new JTextField(20);
        t5.setBounds(600,440,350,30);
        add(t5);

        p1=new JLabel("Password:");
        p1.setBounds(400,500,150,30);
        p1.setFont(f);
        add(p1);
        t6=new JPasswordField(20);
        t6.setEchoChar('#');
        t6.setBounds(600,500,350,30);
        add(t6);

        l7=new JLabel("Date Of Birth:");
        l7.setBounds(400,560,180,30);
        l7.setFont(f);
        add(l7);
        t7=new JTextField(20);
        t7.setBounds(600,560,350,30);
        add(t7);

        /*String[] date={"1","2","3"};
        combo=new JComboBox(date);
        combo.setBackground(Color.GRAY);
        combo.setBackground(Color.BLUE);
        combo.setBounds(600,560,50,30);
        add(combo);*/

        l8=new JLabel("Contact No:");
        l8.setBounds(400,620,170,30);
        l8.setFont(f);
        add(l8);
        t8=new JTextField(20);
        t8.setBounds(600,620,350,30);
        add(t8);

        l9=new JLabel("Class :");
        l9.setBounds(400,680,170,30);
        l9.setFont(f);
        add(l9);

        String[] year={"1st year","2nd year ","3rd year","4th year"};
        combo=new JComboBox(year);
        combo.setBackground(Color.GRAY);
        combo.setBackground(Color.WHITE);
        combo.setBounds(600,680,350,30);
        add(combo);

        l10=new JLabel("College :");
        l10.setBounds(400,740,170,30);
        l10.setFont(f);
        add(l10);

        //add(p);
        add(pic2);

        add(pic);
    }

    public static void main(String args[])
    {
        form f=new form();
        f.setSize(1500,1100);
        f.setVisible(true);
    }
}

Recommended Answers

All 4 Replies

The big problem here is that you have a null LayoutMamager. This is a very bad idea because when you run your program on a different computer (different screen resolution, different systen fonts) your controls will all be the wrong size.
It's also a bad idea because your JFrame has no control over the size or placement of any of its contents, so it doesn't have the info that scroll pane needs to scroll properly.
Also you are adding your controls to the JFrame, not the scroll pane.

Here's what you need to do:
create a JPanel with a sensible layout manager and add all your controls to that, and pack() it - do NOT use null manager or setBounds()!
create a scroll pane with your JPanel as its contents
add the scroill pane to your JFrame.

sir which layout i should choose because its vry diffcult to give alingment to various fields

It depends on how you want to arrange your fields, and how you want that to adapt when the user resizes the window, or you run on a different computer...

I suggest you start with the simplest layout manager possible - you can always go back later and replace it with a more sophisticated manager.

Here a good guide that will help you pick an appropriate one...
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

(one thing to remember is that you can "nest" layout managers by using JPanels - eg you may start with a BorderLayout for headings, content and buttons at the bottom. For the buttons you have a JPanel as the bottom component and place all the buttons along it using a FlowLayout)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.