Adding Scrollbar to panel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anurag275125
    New Member
    • Aug 2009
    • 79

    #1

    Adding Scrollbar to panel

    Hello,
    I write a swing program in which there are three check boxes(used images in check boxes). When I click any check box then- which check box is clicked is displayed in the text field. When I run the program then I can't see all the images. I wanna use a scrollbar so that I can see the entire output. I use JScrollPane in the program but it isn't working.
    here is the code------

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import javax.swing.*;
    /* <applet
    code=jcb_i.class
    width=600
    height=600
    >
    </applet> */
    public class jcb_i extends JApplet
    {
    	jpanel j;
    	public void init()
    	{
    		Container c=getContentPane();
    		c.setBackground(Color.black);
    		c.setForeground(Color.white);
    		j=new jpanel(new GridLayout(4,1));
    		int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
    		int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
    		JScrollPane jsb=new JScrollPane(j,v,h);
    		c.add(jsb);
    	}
    }
    
    class jpanel extends JPanel implements ItemListener
    {
    	ImageIcon ankita,anurag;
    	JCheckBox b1,b2,b3;
    	JTextField jtf;
    	jpanel(LayoutManager layout)
    	{
    		ankita=new ImageIcon("ankita.jpg");
    		anurag=new ImageIcon("anurag.jpg");
    		b1=new JCheckBox("ankita 1",ankita);
    		b2=new JCheckBox("ankita 2",ankita);
    		b3=new JCheckBox("ankita 3",ankita);
    		b1.setSelectedIcon(anurag);
    		b2.setSelectedIcon(anurag);
    		b3.setSelectedIcon(anurag); 
    		jtf=new JTextField(50);
    		jtf.setFont(new Font("castellar",Font.PLAIN,20));
    		jtf.setHorizontalAlignment(jtf.CENTER);
    		b1.addItemListener(this);
    		b2.addItemListener(this);
    		b3.addItemListener(this);
    		add(b1);
    		add(b2);
    		add(b3);
    		add(jtf);
    	}
    	public void itemStateChanged(ItemEvent e)
    	{
    		jtf.setText("You click : "+((JCheckBox) e.getItemSelectable()).getText());
    	}
    }
    Please help me out...

    thanks....
    Last edited by Markus; Feb 14 '10, 01:15 PM. Reason: Added [code] ... [/code] tags
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    It runs fine when I run it using appletviewer. And also in Chrome/FF (once I remove the references to the images since the applet doesn't have permission to read the local file system).

    Comment

    • anurag275125
      New Member
      • Aug 2009
      • 79

      #3
      When I run this program with appletviewer, it only shows the horizontal scrollbar. I want to see vertical scrollbar not horizontal.
      I changed these lines but nothing happening--
      int v=ScrollPaneCon stants.VERTICAL _SCROLLBAR_ALWA YS;
      int h=ScrollPaneCon stants.HORIZONT AL_SCROLLBAR_NE VER;

      How can I enable vertical scrollbar.???

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        Sorry - I'm away from my computer at the momet. But you don't seem to be packing the gui anywhere. Try pack() at the end of the panel constructor.

        Comment

        • anurag275125
          New Member
          • Aug 2009
          • 79

          #5
          can you please explain pack()???

          Comment

          • pbrockway2
            Recognized Expert New Member
            • Nov 2007
            • 151

            #6
            Sorry, pack() was a mistake. (It's a Window method)

            In fact I see scrollbars fine using appletviewer.

            Comment

            Working...