Results 1 to 5 of 5
Thread: Question about JScrollPane
- 12-10-2010, 08:06 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 36
- Rep Power
- 0
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
myPanel.javaJava 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 ); } }
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); } }
- 12-10-2010, 08:32 AM #2
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.
Also note that by convention, Java class names start with an uppercase letter. MyPanel, not myPanel.Java Code:@Override public Dimension getPreferredSize() { if (image == null) { return super.getPreferredSize(); } return new Dimension(image2.getWidth(), image2.getHeight()); }
db
- 12-10-2010, 10:26 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 36
- Rep Power
- 0
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
MyPanel.javaJava 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 ); } }
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 ); } }
- 12-10-2010, 10:58 AM #4
Member
- Join Date
- Jul 2010
- Posts
- 36
- Rep Power
- 0
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] }
- 12-10-2010, 11:36 AM #5
Similar Threads
-
JScrollPane
By UJJAL DHAR in forum New To JavaReplies: 12Last Post: 08-17-2010, 06:47 PM -
jscrollpane
By kaemonsaionji in forum New To JavaReplies: 3Last Post: 02-25-2009, 08:39 AM -
jscrollpane problem
By monkey04 in forum AWT / SwingReplies: 2Last Post: 01-19-2008, 05:23 AM -
help with JScrollPane
By tommy in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 07:58 PM -
JScrollPane not scrolling
By Riftwalker in forum Advanced JavaReplies: 2Last Post: 07-17-2007, 08:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks