Results 1 to 5 of 5
  1. #1
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default Question about JScrollPane

    I don't know why I cannot create a scroll baron a JPanel when I inport a image which is larger than the frame and the panel.

    image2 is a image with size ( 700, 525 ) which is larger than the frame( 400, 300 ) and panel. So, I expect the scroll bar will automatically created but it did't.

    What wrong in my code?
    Thank you. :)

    Test.java
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Test extends JFrame
    {
    	private myPanel panel;
    	private JScrollPane spanel;
    	private JButton button1;
    	private JButton button2;
    	private JPanel topPanel;
    	
    	public Test()
    	{
    		super( "Test" );
    		setLayout( new BorderLayout() );
    		
    		button1 = new JButton( "Button1" );
    		button2 = new JButton( "Button2" );
    		panel = new myPanel();
    
    		topPanel = new JPanel();
    		topPanel.add( button1 );
    		topPanel.add( button2 );
    
    		add( topPanel, BorderLayout.NORTH );
    		add( new JScrollPane( panel ), BorderLayout.CENTER );
    		
    		setMinimumSize( new Dimension( 400, 300 ) );
    	}
    	
    	public static void main( String args[] )
    	{
    		Test myTest = new Test();
    		myTest.setVisible( true );
    	}
    }
    myPanel.java
    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class myPanel extends JPanel
    {
    	private BufferedImage image1;
    	private BufferedImage image2;
    	
    	public myPanel()
    	{
    	     try 
    	     {                
    			image1 = ImageIO.read( new File( "doc/resources/inherit.gif"   ) );
    			image2 = ImageIO.read( new File( "doc/resources/Car.jpg" ) );
    	     } 
    	     catch ( IOException ex ) 
    	     {
    		   // do something
    	     }
    	}
    	
    	public void paintComponent( Graphics g )
    	{
    		super.paintComponent( g );
    		
    		g.drawImage( image2, 0, 0, null );
    		g.drawLine( 0, 0, 100, 100);
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Your panel doesn't have a preferredSize, so the scroll pane doesn't know it should be sized to accommodate the image. One approach would be to override getPreferredSize() to return the size of the image.
    Java Code:
    @Override
    public Dimension getPreferredSize() {
       if (image == null) {
          return super.getPreferredSize();
       }
       return new Dimension(image2.getWidth(), image2.getHeight());
    }
    Also note that by convention, Java class names start with an uppercase letter. MyPanel, not myPanel.

    db

  3. #3
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default

    it works, thank you.
    but when I modify my program, another problem comes out.

    I add a method called changePanelPreferredSize() to change the preferred size of the panel.
    Then, I add an actionListener to the button to execute the changePanelPreferredSize().

    I expected the scroll bar will be created after I press the button. However, it didn't unless I resize the frame manually.

    what is the problem now?
    Thank you for your help.

    P.S: Actually, what I am going to do is:
    import a image by pressing a button to select a image file( using fileChooser that I made it already but I didn't show it out for more readable ) and display the image on the panel, also the scroll bar will be created if the image size is larger than the panel.

    my new code:
    I highlighted the new parts

    Test.java
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Test extends JFrame implements ActionListener
    {
    	private myPanel panel;
    	private JButton buttonChange;
    	private JButton button2;
    	private JPanel topPanel;
    	
    	public Test() 
    	{
    		super( "Test" );
    		setLayout( new BorderLayout() );
    		
    		buttonChange = new JButton( "Change" );
    		button2 = new JButton( "Button2" );
    		panel = new MyPanel();
    
    		[COLOR="Red"]buttonChange.addActionListener( this );[/COLOR]
    		
    		topPanel = new JPanel();
    		topPanel.add( buttonChange );
    		topPanel.add( button2 );
    
    		add( topPanel, BorderLayout.NORTH );
    		add( new JScrollPane( panel ), BorderLayout.CENTER );
    		
    		setMinimumSize( new Dimension( 400, 300 ) );
    	}
    	
    	[COLOR="red"]@Override
    	public void actionPerformed( ActionEvent actionEvent )
    	
    	{
    		panel.changePanelPreferredSize();
    	}[/COLOR]
    	
    	public static void main( String args[] )
    	{
    		Test myTest = new Test();
    		myTest.setVisible( true );
    	}
    
    }
    MyPanel.java
    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class MyPanel extends JPanel
    {
    	private BufferedImage image1;
    	private BufferedImage image2;
    	
    	public myPanel()
    	{
    		try 
    		{                
    			image1 = ImageIO.read( new File( "doc/resources/inherit.gif" ) );
    			image2 = ImageIO.read( new File( "doc/resources/Car.jpg" ) );
    			
    		} 
    		catch ( IOException ex ) 
    		{
    			// do something
    		}
    	}
    	
    	[COLOR="red"]public void changePanelPreferredSize()
    	{
    		setPreferredSize( new Dimension( image2.getWidth(), image2.getHeight() ) );
    	}[/COLOR]
    	
    	public void paintComponent( Graphics g )
    	{
    		super.paintComponent( g );
    		
    		g.drawImage( image2, 0, 0, null );
    	}
    }

  4. #4
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default

    I solved it by adding setSize()
    However, I don't know the reason.....

    Java Code:
    public void changePanelPreferredSize()
    	{
    		setPreferredSize( new Dimension( image2.getWidth(), image2.getHeight() ) );
                   [COLOR="Red"] setSize( new Dimension( image2.getWidth(), image2.getHeight() ) );[/COLOR]
    	}

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. JScrollPane
    By UJJAL DHAR in forum New To Java
    Replies: 12
    Last Post: 08-17-2010, 06:47 PM
  2. jscrollpane
    By kaemonsaionji in forum New To Java
    Replies: 3
    Last Post: 02-25-2009, 08:39 AM
  3. jscrollpane problem
    By monkey04 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2008, 05:23 AM
  4. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 07:58 PM
  5. JScrollPane not scrolling
    By Riftwalker in forum Advanced Java
    Replies: 2
    Last Post: 07-17-2007, 08:16 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
  •