Results 1 to 11 of 11
  1. #1
    sergey is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default How to add scrollbar to jpanel with images

    I drew on jpanel images.
    But cannot add scrollbar on this jpanel which will work.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Have you gone through the Sun Swing tutorial on use of scrollbars? Without seeing your code (please use code-tags -- see my signature below), we have no idea what you're doing wrong.

    best of luck
    Last edited by Fubarable; 10-29-2009 at 03:00 PM.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    828
    Rep Power
    6

    Default

    Post a code and ask for the solution.
    Ramya:cool:

  4. #4
    sergey is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    JPanel jpanel = new DrawPane();
    
    JScrollPane scrollPane = new JScrollPane(jpanel);
    
    add(scrollPane);
    
    //**************************
    class DrawPane extends JPanel {    
        public void paintComponent(Graphics g){       
                
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                double leftX = 100.;
                double topY = 100.;
                double width = 200.;
                double height = 150.;
                Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
                g2.draw(rect);
            } 
            
    }

  5. #5
    sergey is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    I have looked at Sun Swing tutorial, but it seems that all I do is right.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not seeing the problem in the code you've posted, so likely the problem is elsewhere (unless I'm missing it, which is not uncommon). I suggest you have a look at this link on SSCCE, and then create and post an SSCCE. This will allow us to work with your problem directly and come up with a solution more quickly.

    Much luck!

  7. #7
    sergey is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    Full code of application below:


    Java Code:
    import java.awt.*;
    import java.awt.geom.Rectangle2D;
    import javax.swing.*;
    
    
    public class Main {
        public static void main(String[] args) {
           
            JFrame frame = new JFrame();
            JPanel jpanel = new DrawPane();
            JScrollPane scrollPane = new JScrollPane(jpanel);
    
            frame.add(scrollPane);
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(true);
            frame.setLocationByPlatform(true);
            frame.setSize(200,200);
            frame.setVisible(true);
    
        }
    }
    
    class DrawPane extends JPanel {
        @Override
        public void paintComponent(Graphics g){
    
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                double leftX = 100.;
                double topY = 100.;
                double width = 200.;
                double height = 150.;
                Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
                g2.draw(rect);
            }
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    See, SSCCEs work!

    Don't forget to set the preferredSize of your drawing panel (and everything else):
    Java Code:
        public static void main(String[] args) {
           
            JFrame frame = new JFrame();
            JPanel jpanel = new DrawPane();
            jpanel.setPreferredSize(new Dimension(400, 400)); //!! added
            JScrollPane scrollPane = new JScrollPane(jpanel);
    
            frame.add(scrollPane);
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(true);
            frame.setPreferredSize(new Dimension(200, 200)); //!! added
            frame.pack(); //!! added
            frame.setLocationByPlatform(true);
            //frame.setSize(200,200); //!! removed
            frame.setVisible(true);
    
        }

  9. #9
    sergey is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    Thank you very much!!!
    It works!

  10. #10
    hondacodonbk is offline Member
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

    Default

    Hey,I am have the same problem,My code draw a line with a ladge leng on a jpanel,Then I add this jpanel to a frame which smaller leng.I can scroll,but the picture of line has some problem.can You help me?
    this is my main code:
    Java Code:
    /*
     * class vi du tao scroll cho ung dung ve mui ten. Khi panel chua mui ten thay doi ve be rong thi scroll se duoc tao ra de co the keo de xem
     * mui ten
     *
     */
    package vd;
    
    import java.awt.Color;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
        
    class Ve extends JPanel{  // jpanel se duoc add vao scrollpane
        int w,h;
        public Ve(){}
        public Ve(int w,int h){
            this.w = w;
            this.h = h;
            setSize(w,h);
            
            /*
             * In Swing a scrollpane asks its viewport child (the view, here, obj of ve) for its preferred size as
                it gathers information to layout its viewports view. If you want your component to be shown at a size other
                than the scrollpanes viewport view size you must provide the size information to the scrollpane. You can do
                this by calling setPreferredSize on the component or by overriding getPreferredSize in the component and returning the desired size
             */
            //setPreferredSize(new Dimension(w,h));
        }
        public void paintComponent(Graphics g)
        {
            Graphics2D  g1 = (Graphics2D) g;
            g1.setColor(Color.red);
                 // draw a line
            g1.drawLine(getWidth()-20,50);
        }
    }
    
    public class Main
    {
        public JFrame fr;
        public JScrollPane scr;
        public Ve ve;
        public Main()
        {
            int wframe = 600;
            int hframe = 400;
            
            ve = new Ve(); //wframe*2,hframe);  //*** dieu chinh wpanel se thay duoc su thay doi cua scroll
            ve.setPreferredSize(new Dimension(wframe*2,hframe));
    
            fr = new JFrame("Thu nghiem");
            
            // tao scroll de add jpanel vao
            scr = new JScrollPane(ve);//,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            scr.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
    
            
            //Container contentPane = fr.getContentPane();
            //contentPane.add(scr);
    
            fr.add(scr);
            fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE);
            fr.setLocation(100, 100);
            //fr.setSize(wframe,hframe);
            fr.setPreferredSize(new Dimension(wframe,hframe));
            fr.pack();
            fr.setLocationByPlatform(true);
            fr.setVisible(true);
        }
    
        public static void main(String[] args)
        {
            Main v = new Main();
        }
    }

    thank you very much

  11. #11
    hondacodonbk is offline Member
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

    Default

    oh,thank you for your time to my code,I solved my problem
    thank a gain

Similar Threads

  1. need help with JPanel Images
    By Thebigchalupa in forum New To Java
    Replies: 5
    Last Post: 04-23-2009, 07:15 PM
  2. Images in JPanel
    By Thebigchalupa in forum Advanced Java
    Replies: 1
    Last Post: 04-21-2009, 05:11 PM
  3. Resizing Images in JPanel??
    By NoNickName in forum New To Java
    Replies: 1
    Last Post: 04-09-2009, 10:19 PM
  4. Not another Jpanel Scrollbar problem!
    By jiexx in forum AWT / Swing
    Replies: 3
    Last Post: 03-18-2009, 02:09 AM
  5. Moving Images in JPanel
    By killpoppop in forum AWT / Swing
    Replies: 7
    Last Post: 03-08-2009, 02:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •