Results 1 to 5 of 5
  1. #1
    loony is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Smile Problem with JScrollPane

    so im making this program that displays circles connected with lines, but i have a problem with the JScrollPane, the scrollbars arent working. Im creatin a Jpanel pBtn where i put 6 buttons that will decide which circles and lines are supposed to be drawn, the other JPanel, pDraw, is the panel where im drawing the circles and lines, this panel is inside a JScrollPane, so in case the image is too large it would allow to scroll left-right and up-down to view the full image, but when i tested it (i tested it drawing an oval with a size of width=500 and height=750) the scrollbars didnt work, here i put my code:

    Java Code:
    import javax.swing.*;
    import javax.swing.event.MouseInputAdapter;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class AFNGUI extends JFrame implements ActionListener{
    	JButton btns[];
    	JPanel pBtn, pDraw;
    	JScrollPane sDraw;
    	
    	public AFNGUI(){
    		getContentPane().setLayout(null);
    		setVisible(true);
    		setSize(650,550);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		btns=new JButton[6];
    		pBtn=new JPanel();
    		pBtn.setLayout(null);
    		pBtn.setSize(600,50);
    		pBtn.setAlignmentY(Component.TOP_ALIGNMENT);
    		for(int i=0;i<6;i++){
    			btns[i]=new JButton(new ImageIcon(i + ".PNG"));
    			btns[i].setName("" + i );
    			btns[i].setBounds(100*(i),0,50,50);
    			btns[i].setVisible(true);
    			pBtn.add(btns[i]);
    		}
    		btns[0].addActionListener(new ActionListener(){
    				public void actionPerformed(ActionEvent e)   {boton1(e);}
    			});
    
    		pBtn.setBorder(BorderFactory.createLineBorder(Color.black));
    		getContentPane().add(pBtn);
    		pDraw=new JPanel();
    		pDraw.setLayout(null);
    		pDraw.setBackground(Color.white);
    		pDraw.setSize(1000,1000);
    		sDraw=new JScrollPane(pDraw,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    		sDraw.setBounds(0,50,600,450);
    		getContentPane().add(sDraw);
    		repaint();
    	}
    	public void boton1(ActionEvent e){
    		int w=500,h=750;
    		//pDraw.setSize(w,h);
    		Graphics g=pDraw.getGraphics();
    		g.setColor(Color.blue);
    		g.drawOval(100,150,w,h);
    		
    	}
    	public void actionPerformed(ActionEvent e){}
    	public static void main(String args[]){
    		AFNGUI miafn=new AFNGUI();
    		
    	}
    }
    so if anyone can help me see my error i would be so glad, the complete program will be drawing bigger structures, im planning to save different structures of circles and lines drawn and then connect them, but that i'll do it later, first i wanna know what's the error im making with the scrollbars.

    thanks
    Last edited by loony; 03-29-2011 at 11:21 PM. Reason: made a mistake in the code

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

    Default

    Don't get your Graphics object by calling getGraphics on a JPanel or other component. Instead do it as the Swing tutorials suggest: in the paintComponent override method using the Graphics object that is passed in as a parameter to this method.

    Please start here

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

    Default

    Also, you want to avoid using null layout and setting the absolute size and position of your components, but rather using appropriate layout managers and preferredSizes of your components. This is the main reason that your scrollbars aren't working.

  4. #4
    loony is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    how do i override the method? can you give me an example?

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

Similar Threads

  1. Problem with JScrollPane and VK_TAB
    By hitesh in forum AWT / Swing
    Replies: 3
    Last Post: 09-10-2010, 12:27 PM
  2. JScrollPane problem
    By KArelVH in forum AWT / Swing
    Replies: 6
    Last Post: 04-27-2009, 09:40 PM
  3. problem with Jscrollpane
    By ravrajesh.ap in forum AWT / Swing
    Replies: 5
    Last Post: 01-03-2009, 10:38 PM
  4. JScrollPane updation Problem
    By goodwillwins in forum AWT / Swing
    Replies: 22
    Last Post: 09-28-2008, 09:11 AM
  5. jscrollpane problem
    By monkey04 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2008, 05:23 AM

Tags for this Thread

Posting Permissions

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