KarthiK
KarthiK

Reputation: 3

How to append a scrollbar for the window?

How to append a scroll pane for my window?

The program compiles properly, but the scroll pane for the window is not created. I really don't know why this is happening. I defined JScrollPane and even implemented it with scrollPane = new JScrollPane

Where is my mistake?

Below is my code:

import java.awt.*;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;

public class RegForm extends JFrame implements ItemListener{ 
JLabel l0,li,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13;
JButton b1,b2,b3,b4,b5;
JTextField t1,t2,t3,t4,t5,t6,t7,t8;
JTextArea a1,a2;
JComboBox<Integer> dd = new JComboBox<Integer>();
JComboBox<String> mm = new JComboBox<String>();
JComboBox<Integer> yyyy = new JComboBox<Integer>();
JComboBox<String> q = new JComboBox<String>();
JRadioButton rb1 = new JRadioButton( " Male ");
JRadioButton rb2 = new JRadioButton(" Female ");
JCheckBox cb1 = new JCheckBox (" C ");
JCheckBox cb2 = new JCheckBox (" C++ ");
JCheckBox cb3 = new JCheckBox (" Java ");
JCheckBox cb4 = new JCheckBox (" Oracle ");
JCheckBox cb5 = new JCheckBox (" Android ");
JCheckBox cb6 = new JCheckBox (" iOS ");
JCheckBox cb7 = new JCheckBox (" Web Designing ");
JCheckBox cb8 = new JCheckBox (" .Net ");
JCheckBox cb9 = new JCheckBox (" Same as Contact Address ");
JScrollPane scrollPane = new JScrollPane();
RegForm()
{

    l0 = new JLabel("REGISTRATION FORM");   
    Font f0 = new Font("Algerian",Font.ITALIC,20);
    l0.setFont(f0);
    l0.setBounds(600,10,250,50);
    scrollPane.add(l0);

    li = new JLabel(" * Fields are mandatory");
    Font fi = new Font("Arabic TypeSetting",Font.PLAIN,17);
    li.setFont(fi);
    li.setForeground(Color.RED);
    li.setBounds(10,50,150,30);
    scrollPane.add(li);


    l1 = new JLabel(" * FirstName: ");
    Font f1 = new Font("Bookman Old Style",Font.PLAIN,12);
    l1.setFont(f1);
    l1.setBounds(10,70,100,50);
    scrollPane.add(l1);

    t1 = new JTextField(20);
    t1.setBounds(165,85,140,20);
    scrollPane.add(t1);

    l2 = new JLabel("* Confirm FirstName: ");
    l2.setFont(f1);
    l2.setBounds(10,100,150,50);
    scrollPane.add(l2);

    t2 = new JTextField(20);
    t2.setBounds(165,115,140,20);
    scrollPane.add(t2);

    l3 = new JLabel(" Middle Name: ");
    l3.setFont(f1);
    l3.setBounds(15,130,120,50);
    scrollPane.add(l3);

    t3 = new JTextField(20);
    t3.setBounds(165,145,140,20);
    scrollPane.add(t3);

    l4 = new JLabel(" Confirm Middle Name: ");
    l4.setFont(f1);
    l4.setBounds(15,160,150,50);
    scrollPane.add(l4);

    t4 = new JTextField(20);
    t4.setBounds(165,175,140,20);
    scrollPane.add(t4);

    l5 = new JLabel(" * Sur Name: ");
    l5.setFont(f1);
    l5.setBounds(10,190,100,50);
    scrollPane.add(l5);

    t5 = new JTextField(20);
    t5.setBounds(165,205,140,20);
    scrollPane.add(t5);

    l6 = new JLabel(" * Confirm Sur Name: ");
    l6.setFont(f1); 
    l6.setBounds(10,220,150,50);
    scrollPane.add(l6);

    t6 = new JTextField(20);
    t6.setBounds(165,235,140,20);
    scrollPane.add(t6);

    l7 = new JLabel(" * DD / MM / YYYY" );
    Font f2 = new Font(" Comic Sans MS ",Font.ITALIC,12);
    l7.setFont(f2);
    l7.setBounds(10,260,150,50);
    scrollPane.add(l7);

    for(int j=1;j<=31;j++)
    dd.addItem(new Integer(j));
    dd.setBounds(165,275,47,20);
    scrollPane.add(dd);
    dd.addItemListener(this);

    mm.addItem("January");
    mm.addItem("February");
    mm.addItem("March");
    mm.addItem("April");
    mm.addItem("May");
    mm.addItem("June");
    mm.addItem("July");
    mm.addItem("August");
    mm.addItem("September");
    mm.addItem("October");
    mm.addItem("November");
    mm.addItem("December");
    mm.setBounds(212,275,90,20);
    scrollPane.add(mm);
    mm.addItemListener(this);

    for(int i=1990;i<=2016;i++)
    yyyy.addItem(new Integer(i));
    yyyy.setBounds(302,275,70,20);
    scrollPane.add(yyyy);
    yyyy.addItemListener(this);

    l8 = new JLabel(" Age: ");
    l8.setFont(f1);
    l8.setBounds(15,290,50,50);
    scrollPane.add(l8);

    t8 = new JTextField(10);
    t8.setBounds(165,305,50,20);
    scrollPane.add(t8);

    l9 = new JLabel(" Qualification ");
    l9.setFont(f1);
    l9.setBounds(15,320,120,50);
    scrollPane.add(l9);

    q.addItem(" B.Tech ");
    q.addItem(" M.Tech ");
    q.addItem(" MBA ");
    q.addItem(" MCA ");
    q.addItem(" Intermediate ");    
    q.addItem(" SSC "); 
    q.addItem(" Others ");
    q.setBounds(165,335,100,20);
    scrollPane.add(q);
    q.addItemListener(this);

    l10 = new JLabel(" Gender ");
    l10.setFont(f1);
    l10.setBounds(15,360,80,50);
    scrollPane.add(l10);

    rb1.setBounds(165,365,80,39);
    rb2.setBounds(250,365,80,39);

    ButtonGroup bg = new ButtonGroup();
    bg.add(rb1);
    bg.add(rb2);
    scrollPane.add(rb1);
    scrollPane.add(rb2);    

    l11 = new JLabel(" Courses Intrested: ");
    l11.setFont(f1);    
    l11.setBounds(15,450,150,50);
    scrollPane.add(l11);

    cb1.setBounds(165,390,100,50);
    scrollPane.add(cb1); 
    cb2.setBounds(285,390,100,50);
    scrollPane.add(cb2);
    cb3.setBounds(165,425,100,50);
    scrollPane.add(cb3);
    cb4.setBounds(285,425,100,50);
    scrollPane.add(cb4);
    cb5.setBounds(165,460,100,50);
    scrollPane.add(cb5);
    cb6.setBounds(285,460,100,50);
    scrollPane.add(cb6);
    cb7.setBounds(165,495,100,50);
    scrollPane.add(cb7);
    cb8.setBounds(285,495,100,50);
    scrollPane.add(cb8);
    cb9.setBounds(15,630,200,50);
    scrollPane.add(cb9);

    l12 = new JLabel(" Contact Address: ");
    l12.setFont(f1);
    l12.setBounds(15,550,150,50);
    scrollPane.add(l12);

    a1 = new JTextArea (5,20);
    a1.setBounds(165,545,250,80);
    scrollPane.add(a1);

    l13 = new JLabel(" Permenant Address: ");
    l13.setFont(f1);
    l13.setBounds(15,675,150,50);
    scrollPane.add(l13);

    a2 = new JTextArea (5,20);
    a2.setBounds(165,680,250,80);
    scrollPane.add(a2);

    cb9.addItemListener(this);


}

public void itemStateChanged(ItemEvent ie) 
{
    if(ie.getSource() == yyyy){
    int y = (Integer) ie.getItem();
    t8.setText(Integer.toString(2016-y));
    t8.setEditable(false);
    }
    if(cb9.isSelected()){
    a2.setText(a1.getText());
    a2.setEditable(false);
    }
}
public void actionPerformed(ActionEvent ae)
{   
    }

public static void main(String[] args)
{

    RegForm rf = new RegForm();
    rf.setTitle("Hai Hello");
    JScrollPane scrollPane = new JScrollPane();
       scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                     scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(10,10,100,100);
JPanel contentPane = new JPanel();
contentPane.setPreferredSize(new Dimension(1500, 800));
contentPane.add(scrollPane);
rf.setContentPane(contentPane);
rf.pack();
rf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
rf.setVisible(true);
}
}

Upvotes: 0

Views: 111

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168845

This works, but you'll need to layout the components better..

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RegForm extends JFrame implements ItemListener {

    JLabel l0, li, l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13;
    JButton b1, b2, b3, b4, b5;
    JTextField t1, t2, t3, t4, t5, t6, t7, t8;
    JTextArea a1, a2;
    JComboBox<Integer> dd = new JComboBox<Integer>();
    JComboBox<String> mm = new JComboBox<String>();
    JComboBox<Integer> yyyy = new JComboBox<Integer>();
    JComboBox<String> q = new JComboBox<String>();
    JRadioButton rb1 = new JRadioButton(" Male ");
    JRadioButton rb2 = new JRadioButton(" Female ");
    JCheckBox cb1 = new JCheckBox(" C ");
    JCheckBox cb2 = new JCheckBox(" C++ ");
    JCheckBox cb3 = new JCheckBox(" Java ");
    JCheckBox cb4 = new JCheckBox(" Oracle ");
    JCheckBox cb5 = new JCheckBox(" Android ");
    JCheckBox cb6 = new JCheckBox(" iOS ");
    JCheckBox cb7 = new JCheckBox(" Web Designing ");
    JCheckBox cb8 = new JCheckBox(" .Net ");
    JCheckBox cb9 = new JCheckBox(" Same as Contact Address ");
    JScrollPane scrollPane;
    JPanel panel = new JPanel(new GridLayout(0,1));

    RegForm() {

        l0 = new JLabel("REGISTRATION FORM");
        Font f0 = new Font("Algerian", Font.ITALIC, 20);
        l0.setFont(f0);
        l0.setBounds(600, 10, 250, 50);
        panel.add(l0);

        li = new JLabel(" * Fields are mandatory");
        Font fi = new Font("Arabic TypeSetting", Font.PLAIN, 17);
        li.setFont(fi);
        li.setForeground(Color.RED);
        li.setBounds(10, 50, 150, 30);
        panel.add(li);

        l1 = new JLabel(" * FirstName: ");
        Font f1 = new Font("Bookman Old Style", Font.PLAIN, 12);
        l1.setFont(f1);
        l1.setBounds(10, 70, 100, 50);
        panel.add(l1);

        t1 = new JTextField(20);
        t1.setBounds(165, 85, 140, 20);
        panel.add(t1);

        l2 = new JLabel("* Confirm FirstName: ");
        l2.setFont(f1);
        l2.setBounds(10, 100, 150, 50);
        panel.add(l2);

        t2 = new JTextField(20);
        t2.setBounds(165, 115, 140, 20);
        panel.add(t2);

        l3 = new JLabel(" Middle Name: ");
        l3.setFont(f1);
        l3.setBounds(15, 130, 120, 50);
        panel.add(l3);

        t3 = new JTextField(20);
        t3.setBounds(165, 145, 140, 20);
        panel.add(t3);

        l4 = new JLabel(" Confirm Middle Name: ");
        l4.setFont(f1);
        l4.setBounds(15, 160, 150, 50);
        panel.add(l4);

        t4 = new JTextField(20);
        t4.setBounds(165, 175, 140, 20);
        panel.add(t4);

        l5 = new JLabel(" * Sur Name: ");
        l5.setFont(f1);
        l5.setBounds(10, 190, 100, 50);
        panel.add(l5);

        t5 = new JTextField(20);
        t5.setBounds(165, 205, 140, 20);
        panel.add(t5);

        l6 = new JLabel(" * Confirm Sur Name: ");
        l6.setFont(f1);
        l6.setBounds(10, 220, 150, 50);
        panel.add(l6);

        t6 = new JTextField(20);
        t6.setBounds(165, 235, 140, 20);
        panel.add(t6);

        l7 = new JLabel(" * DD / MM / YYYY");
        Font f2 = new Font(" Comic Sans MS ", Font.ITALIC, 12);
        l7.setFont(f2);
        l7.setBounds(10, 260, 150, 50);
        panel.add(l7);

        for (int j = 1; j <= 31; j++) {
            dd.addItem(new Integer(j));
        }
        dd.setBounds(165, 275, 47, 20);
        panel.add(dd);
        dd.addItemListener(this);

        mm.addItem("January");
        mm.addItem("February");
        mm.addItem("March");
        mm.addItem("April");
        mm.addItem("May");
        mm.addItem("June");
        mm.addItem("July");
        mm.addItem("August");
        mm.addItem("September");
        mm.addItem("October");
        mm.addItem("November");
        mm.addItem("December");
        mm.setBounds(212, 275, 90, 20);
        panel.add(mm);
        mm.addItemListener(this);

        for (int i = 1990; i <= 2016; i++) {
            yyyy.addItem(new Integer(i));
        }
        yyyy.setBounds(302, 275, 70, 20);
        panel.add(yyyy);
        yyyy.addItemListener(this);

        l8 = new JLabel(" Age: ");
        l8.setFont(f1);
        l8.setBounds(15, 290, 50, 50);
        panel.add(l8);

        t8 = new JTextField(10);
        t8.setBounds(165, 305, 50, 20);
        panel.add(t8);

        l9 = new JLabel(" Qualification ");
        l9.setFont(f1);
        l9.setBounds(15, 320, 120, 50);
        panel.add(l9);

        q.addItem(" B.Tech ");
        q.addItem(" M.Tech ");
        q.addItem(" MBA ");
        q.addItem(" MCA ");
        q.addItem(" Intermediate ");
        q.addItem(" SSC ");
        q.addItem(" Others ");
        q.setBounds(165, 335, 100, 20);
        panel.add(q);
        q.addItemListener(this);

        l10 = new JLabel(" Gender ");
        l10.setFont(f1);
        l10.setBounds(15, 360, 80, 50);
        panel.add(l10);

        rb1.setBounds(165, 365, 80, 39);
        rb2.setBounds(250, 365, 80, 39);

        ButtonGroup bg = new ButtonGroup();
        bg.add(rb1);
        bg.add(rb2);
        panel.add(rb1);
        panel.add(rb2);

        l11 = new JLabel(" Courses Intrested: ");
        l11.setFont(f1);
        l11.setBounds(15, 450, 150, 50);
        panel.add(l11);

        cb1.setBounds(165, 390, 100, 50);
        panel.add(cb1);
        cb2.setBounds(285, 390, 100, 50);
        panel.add(cb2);
        cb3.setBounds(165, 425, 100, 50);
        panel.add(cb3);
        cb4.setBounds(285, 425, 100, 50);
        panel.add(cb4);
        cb5.setBounds(165, 460, 100, 50);
        panel.add(cb5);
        cb6.setBounds(285, 460, 100, 50);
        panel.add(cb6);
        cb7.setBounds(165, 495, 100, 50);
        panel.add(cb7);
        cb8.setBounds(285, 495, 100, 50);
        panel.add(cb8);
        cb9.setBounds(15, 630, 200, 50);
        panel.add(cb9);

        l12 = new JLabel(" Contact Address: ");
        l12.setFont(f1);
        l12.setBounds(15, 550, 150, 50);
        panel.add(l12);

        a1 = new JTextArea(5, 20);
        a1.setBounds(165, 545, 250, 80);
        panel.add(a1);

        l13 = new JLabel(" Permenant Address: ");
        l13.setFont(f1);
        l13.setBounds(15, 675, 150, 50);
        panel.add(l13);

        a2 = new JTextArea(5, 20);
        a2.setBounds(165, 680, 250, 80);
        panel.add(a2);

        cb9.addItemListener(this);

        scrollPane = new JScrollPane(panel);
        add(scrollPane);
        //add(panel);
    }

    public void itemStateChanged(ItemEvent ie) {
        if (ie.getSource() == yyyy) {
            int y = (Integer) ie.getItem();
            t8.setText(Integer.toString(2016 - y));
            t8.setEditable(false);
        }
        if (cb9.isSelected()) {
            a2.setText(a1.getText());
            a2.setEditable(false);
        }
    }

    public void actionPerformed(ActionEvent ae) {
    }

    public static void main(String[] args) {

        RegForm rf = new RegForm();
        rf.setTitle("Hai Hello");
        /*JScrollPane panel = new JScrollPane();
        panel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        panel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        panel.setBounds(10, 10, 100, 100);
        JPanel contentPane = new JPanel();
        contentPane.setPreferredSize(new Dimension(1500, 800));
        contentPane.add(panel);
        rf.setContentPane(contentPane);*/
        rf.pack();
        rf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        rf.setVisible(true);
    }
}

Notes:

  1. As mentioned by @camickr, setting the bounds of the components won't work reliably. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.
  2. Please try to solve programming errors before you've got to dozens of components! For simple testing of layouts with many components, just add dummy components in a loop. To put that another way: For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.
  3. Safer to use logical font names that specific one, e.g. Algerian will only work on systems with that font installed!

Upvotes: 2

camickr
camickr

Reputation: 324207

scrollPane.add(l11);

Never add components directly to a scroll pane.

l1.setBounds(10,70,100,50);

Don't use setBounds(...). It is the job of the layout manager to set the size/location of the component.

The basic logic would be:

JPanel panel = new JPanel(); // set your layout manager for the panel.
panel.add( someComponent );
panel.add( anotherComponent );
JScrollPane scrollPane = new JScrollPane( panel );
frame.add( scrollPane );

Read the Swing Tutorial for Swing basics. Every section in the tutorial has working examples. Maybe start with the section on How to Use Scroll Panes.

Upvotes: 3

Related Questions