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 3 of 3

Thread: Add JScrollPane to a JPanel

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Add JScrollPane to a JPanel

    Hello,

    I have this interface to create. I have a problem with the JScrollPane:

    I posted in Add JScrollPane to a JPanel - Stack Overflow but I didn't get answer.



    I declared a JPanel with a Gridlayout(8,1,0,2), I want 8 rows appear in this panel. A row is a JPanel to, I set the size to make the 8 row panels appear in the big panel. If the number of rows pass 8, I get two columns ... I added a JScrollPane but it doesn't appear. Testing button at the place of button, the scrollpane appear but returning to panel it disappear..

    How can I do ??


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Add JScrollPane to a JPanel

    If you want help with code, you'll have to provide an SSCCE that demonstrates the problem. Note that this does NOT mean you should post all of your code- boil it down to as few lines as possible.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    alibm (March 7th, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Add JScrollPane to a JPanel

    I have a frame.
    I applyed AbsoluteLayout in this frame Then I added a panel with a GridLayout.
    Clicking with mouse left button and Surround with, I chose JscrollPane .

    Yhis is the code without Sourround with JScrollPane

    package d06.m03;
     
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JScrollPane;
    import java.awt.GridLayout;
    import java.awt.SystemColor;
     
    public class ActionExample4 extends JFrame {
     	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					ActionExample4 frame = new ActionExample4();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the frame.
    	 */
    	public ActionExample4() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 594, 426);
    		getContentPane().setLayout(null);
     
    		JPanel panel = new JPanel();
    		panel.setBackground(SystemColor.activeCaption);
    		panel.setBounds(91, 11, 154, 264);
    		getContentPane().add(panel);
    		panel.setLayout(new GridLayout(8, 1, 1, 2));
     
     
    		  for(int i=0;i<9;i++)
    		{
    			JPanel panel_1 = new JPanel();
    		   panel.add(panel_1);
    		} 
     
     
     
    	}
    }

    and with surround :

     
    package d06.m03;
     
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JScrollPane;
    import java.awt.GridLayout;
    import java.awt.SystemColor;
     
    public class ActionExample4 extends JFrame {
     	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					ActionExample4 frame = new ActionExample4();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the frame.
    	 */
    	public ActionExample4() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 594, 426);
    		getContentPane().setLayout(null);
     
    		JScrollPane scrollPane = new JScrollPane();
    		scrollPane.setBounds(91, 11, 154, 264);
    		getContentPane().add(scrollPane);
     
    		JPanel panel = new JPanel();
    		scrollPane.setViewportView(panel);
    		panel.setBackground(SystemColor.activeCaption);
    		panel.setLayout(new GridLayout(8, 1, 1, 2));
     
     
    		  for(int i=0;i<9;i++)
    		{
    			JPanel panel_1 = new JPanel();
    		   panel.add(panel_1);
    		} 
     
     
     
    	}
    }


    this is the output with both codes:


    I want it like this:



    Thanks in advance.

Similar Threads

  1. JPanel and JScrollPane inheritance
    By ohiggins in forum AWT / Java Swing
    Replies: 2
    Last Post: February 15th, 2011, 09:06 AM
  2. painting Image on JPanel inside a JScrollPane
    By becca23 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 29th, 2010, 07:28 PM
  3. JScrollPane
    By MysticDeath in forum AWT / Java Swing
    Replies: 1
    Last Post: February 17th, 2010, 09:21 PM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 01:36 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM

Tags for this Thread