Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Dynamically Sized Scrollable JPanels

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Dynamically Sized Scrollable JPanels

    Can anyone help me figure out how to create a dynamically sized scrollable JPanel?

    Here is a snippet of my code:
    JPanel outputPane = new JPanel();
        	outputPane.setPreferredSize(new Dimension(525,1000));
        	...
     
        	JScrollPane scrollOutput = new JScrollPane(outputPane,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        	scrollOutput.setPreferredSize(new Dimension(550,450));

    So I have already figured out that the JScrollPanel will scroll to the size of the JPanel's preferred size. Unfortunately, that doesn't help me when I add a large image to one of the JLabels inside of the JPanel at runtime. So, to achieve what I am wanting to do, I need the Scroll Pane to scroll to the ends of the JPanel, regardless of the size at the time. Any suggestions on how to do this?

    Tell me if I am being unclear.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Dynamically Sized Scrollable JPanels

    When you add the image to the JPanel (or component within), first set the preferredSize of the JPanel (or component within) so that it incorporates the full size of the image (or whichever dimensions you wish - the necessity of this step may depend upon the component you are using and if its preferredsize is set when setting the image). Next, call revalidate() on the JPanel, which should notify the JViewport of the JScrollPane to recalculate the scrollable with and height.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Dynamically Sized Scrollable JPanels

    Quote Originally Posted by copeg View Post
    When you add the image to the JPanel (or component within), first set the preferredSize of the JPanel (or component within) so that it incorporates the full size of the image (or whichever dimensions you wish - the necessity of this step may depend upon the component you are using and if its preferredsize is set when setting the image). Next, call revalidate() on the JPanel, which should notify the JViewport of the JScrollPane to recalculate the scrollable with and height.
    Well, when the JFrame is begins there is not image on the JLabel. It is only after the user's input does an image load. Before then, I have no idea what the size of the image will be.

    So I added outputPane.revalidate() at the end of the code that loads the image, but nothing happened.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Dynamically Sized Scrollable JPanels

    Quote Originally Posted by aussiemcgr View Post
    So I added outputPane.revalidate() at the end of the code that loads the image, but nothing happened.
    Did the preferredSize change? Can you post a small portion of code that reproduces the issue?

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Dynamically Sized Scrollable JPanels

    This is the best I could do to replicate the issue in a way that I can actually show (work restrictions):

    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Dimension;
     
    public class PictureFrame 
    {
    	static JFrame frame;
    	static JPanel panel;
    	static JScrollPane scrollPane;
    	static JLabel pictureLabel;
    	static JButton loadPic;
     
        public static void main(String[] args) 
        {
        	//Build JFrame
        	frame = new JFrame("Picture Frame");
        	frame.setSize(500,500);
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        	//Build JPanel
        	panel = new JPanel();
        	panel.setPreferredSize(new Dimension(300,300));
        	//Build JScrollPane
        	scrollPane = new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        	scrollPane.setPreferredSize(new Dimension(300,300));
     
        	//Create Blank JLabel
        	pictureLabel = new JLabel();
        	//Create JButton
        	loadPic = new JButton("Load Koala");
        	//Add Action Listener
        	loadPic.addActionListener(new ActionListener(){
        		public void actionPerformed(ActionEvent e)
        		{
        			//Set JLabel's Image to the Koala picture
        			pictureLabel.setIcon(new ImageIcon("Koala.jpg"));
        			//Revalidate
        			panel.revalidate();
        		}
        	});
        	//Add JLabel to JPanel
        	panel.add(pictureLabel);
     
        	//Get JFrame's Content Pane
        	Container contentPane = frame.getContentPane();
        	//Set Layout
    		contentPane.setLayout(new FlowLayout());
        	//Add JButton and JPanel
        	contentPane.add(loadPic);
        	contentPane.add(panel);
     
        	//Set Frame Visible
        	frame.setVisible(true);
        }
    }

    The Koala.jpg image is a default image in Windows 7. I'll attach it if you feel like running the code to see what is happening.
    Attached Images Attached Images
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Dynamically Sized Scrollable JPanels

    Cool. The first thing at least with this code, is that you are adding the panel to the frame instead of the JScrollPane. Next, if I run the code setting the preferredsize as follows, it works for me:
    ImageIcon icon = new ImageIcon("Koala.jpg");//create the image icon
    panel.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));//set the preferred size
    pictureLabel.setIcon(icon);
    panel.revalidate();

  7. #7
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Dynamically Sized Scrollable JPanels

    setPreferredSize(...) is rarely needed in client code and this isn't one of those rare needs. Use of an appropriate layout manager along with a call to revalidate() and repaint() after changing the contained components is the correct approach when components are added to or removed from an already visible container.

    db

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Dynamically Sized Scrollable JPanels

    Quote Originally Posted by Darryl.Burke View Post
    setPreferredSize(...) is rarely needed in client code and this isn't one of those rare needs. Use of an appropriate layout manager along with a call to revalidate() and repaint() after changing the contained components is the correct approach when components are added to or removed from an already visible container.
    Isn't this one of those situations? No components are being added to a visible container, only an Icon being set. If I run the above example no resizing occurs unless both setPreferredSize and revalidate are called.

  9. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Dynamically Sized Scrollable JPanels

    Quote Originally Posted by copeg View Post
    Cool. The first thing at least with this code, is that you are adding the panel to the frame instead of the JScrollPane. Next, if I run the code setting the preferredsize as follows, it works for me:
    ImageIcon icon = new ImageIcon("Koala.jpg");//create the image icon
    panel.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));//set the preferred size
    pictureLabel.setIcon(icon);
    panel.revalidate();
    Ok, so I overlooked something very significant when I wrote my code example earlier. The Scrollable Panel does not just contain the JLabel. Specifically, my full program's Scrollable Panel will contain 3 JLabels (each with varying sizing images) and 3 JTextAreas (also scrollable for the time being, but that may change for style reasons).

    To reflect that, I have changed the code a little to include a JTextField in the Scrollable Panel with the JLabel. Just like in my real program, the text in the JTextField (instead of the JTextArea in my real program) is set via a file read (although my example below uses FileReader instead of a stream like in my real program). Here is the changed code:
    import javax.swing.*;
    import java.io.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Dimension;
     
    public class PictureFrame 
    {
    	static JFrame frame;
    	static JPanel panel;
    	static JScrollPane scrollPane;
    	static JLabel pictureLabel;
    	static JButton loadPic;
     
    	//New Addition
    	static JTextField textField;
     
        public static void main(String[] args) 
        {
        	//Build JFrame
        	frame = new JFrame("Picture Frame");
        	frame.setSize(500,500);
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        	//Build JPanel
        	panel = new JPanel();
        	panel.setPreferredSize(new Dimension(300,300));
        	//Build JScrollPane
        	scrollPane = new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        	scrollPane.setPreferredSize(new Dimension(300,300));
     
        	//Create Blank JLabel
        	pictureLabel = new JLabel();
        	//Create JTextField
        	textField = new JTextField(10);
        	//Create JButton
        	loadPic = new JButton("Load Koala");
        	//Add Action Listener
        	loadPic.addActionListener(new ActionListener(){
        		public void actionPerformed(ActionEvent e)
        		{
        			//Set JLabel's Image to the Koala picture
        			pictureLabel.setIcon(new ImageIcon("Koala.jpg"));
        			try{
    	    			BufferedReader in = new BufferedReader(new FileReader("text.txt"));
    	    			textField.setText(in.readLine());
    	    			in.close();
        			}catch(Exception ex){System.out.println("Error: "+ex);	}
     
        			//Revalidate
        			panel.revalidate();
        		}
        	});
        	//Add JLabel to JPanel
        	panel.add(pictureLabel);
        	//Add JTextField
        	panel.add(textField);
     
        	//Get JFrame's Content Pane
        	Container contentPane = frame.getContentPane();
        	//Set Layout
    		contentPane.setLayout(new FlowLayout());
        	//Add JButton and JPanel
        	contentPane.add(loadPic);
        	contentPane.add(panel);
     
        	//Set Frame Visible
        	frame.setVisible(true);
        }
    }

    And here is the content of text.txt for this example:
    This is the text.
    ------------------------------------------------------------------------------------------------------------------------

    Perhaps I am approaching this the wrong way, so I'll just outline the purpose of what I am doing and maybe someone will know a better way.

    Basically, I am creating one of those basic tutorial/content helper things that exist on programs to allow the user to learn how to use certain features. I have a JTree on the left side that contains different categories and subcategories of different features. To the right of that I will have a panel that will update based on the selected item in the JTree to present the information on how to use the selected feature. To make it as editable as possible after production, the images are being stored as basic jpg (to allow changing if necessary) and the text is being stored in basic .txt files (to also allow changing if necessary). For my initial attempt at this and to provide a basic working product, I am assuming all feature details will consist of a maximum of 3 pictures and 3 blocks of text. After I have a basic version working and I get feedback on the design and whatnot, I will make it more dynamic, hopefully so the number of JLabels and JTextAreas depend on what is required for each category.

    Am I approaching this the wrong way?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. #10
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Dynamically Sized Scrollable JPanels


  11. #11
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Dynamically Sized Scrollable JPanels

    Quote Originally Posted by Darryl.Burke View Post
    The company I work for isnt too wild about the idea of external libraries. As is, we are purchasing SmartXLS for this particular project.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Updating GUI dynamically
    By KrisTheSavage in forum AWT / Java Swing
    Replies: 2
    Last Post: August 29th, 2010, 08:23 AM
  2. swing - switching JPanels
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 5th, 2010, 09:18 AM
  3. [SOLVED] Dynamic numbber of JPanels
    By nasi in forum AWT / Java Swing
    Replies: 2
    Last Post: May 14th, 2010, 02:44 AM
  4. repainting a jframe containing two jpanels
    By musasabi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 11th, 2010, 10:31 PM
  5. Add Jmol applet dynimically in JSF(java server faces)
    By megha in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: May 15th, 2009, 06:16 AM