Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

JPanel in a JScrollPane problem

z_idanez_idane Posts: 1Member
i m trying to put a JPanel into a Jscrollpane.
the JPanel contains componentts like canvas and images.
the problem is when i use the scrollbar,the panel comes out of the scrollpane and overlaps the main panel.

i want the panel stay inside the the scrollPane and work normally.

here is the code

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

public class scrollPaneS extends JFrame
{
Container c;
JPanel jp;
JPanel jp2;
JTextField jtf , jtfTwo;
//JLabel one,two,three;
JButton jb;
JScrollPane jsp;
Graphics gfs;
Image img;
testcan ek;
public scrollPaneS()
{
super("Text Threads");

setSize(400,300);
setResizable(true);
c = getContentPane();
c.setLayout(null);

jp = new JPanel();
jp.setLayout(null);
jp.setBounds(0,0,800,600);
c.add(jp);




jsp = new JScrollPane();

jsp.setBounds(20,20,100,400);
jsp.setVerticalScrollBarPolicy(JScrollPane. VERTICAL_SCROLLBAR_ALWAYS);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setBorder(null);
jp.add(jsp,BorderLayout.CENTER);

jp2 = new JPanel();
jp2.setLayout(new GridLayout());
jp2.setBackground(Color.black);
jp2.setSize(400,300);

ek = new testcan();
ek.setSize(200,100);
jp2.add(ek);

jsp.setViewportView(jp2);
setVisible(true);

}

public static void main(String args[])
{
scrollPaneS app = new scrollPaneS();
app.addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{

System.exit(0);
}
});
}
class testcan extends Canvas
{
public testcan()
{
img = Toolkit.getDefaultToolkit().createImage("Images\10.gif");
MediaTracker mt = new MediaTracker(this);
mt.addImage(img,0);
try
{
mt.waitForID(0);
}
catch (Exception e)
{
}
}
public void paint(Graphics gfs)
{
gfs.drawImage(img,50,100,this);
}

};



}

Comments

  • LireeLiree Posts: 33Member
    Hi,
    Try the following:
    1. Let testcan extend from JPanel
    2. in the paint(Graphics) method call super.paint(..).
    Then the drawImage Method.

    Ah, and if you really want some scrolling in the given code,
    add "jp2.setPreferredSize(new Dimension(400,300));" behind the
    line:
    "jp2.setSize(400, 300);"
Sign In or Register to comment.