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

Thread: Adding Scrollpane to jPanel

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding Scrollpane to jPanel

    I have trouble with adding scrollpane to jpanel. I can't find what is the problem, when i run the program scrollpane is not showing.


    import java.awt.Dimension;
    import java.awt.GridLayout;  
    import javax.swing.BorderFactory;  
    import javax.swing.ButtonGroup;  
    import javax.swing.JFrame;  
    import javax.swing.JPanel;  
    import javax.swing.JRadioButton;  
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
     
    class FD  
    {  
        private JPanel mainPanel = new JPanel(); 
     
     
        private JRadioButton[] verticalRadioBtns = new JRadioButton[40];  
        private JRadioButton[] horizontalRadioBtns = new JRadioButton[6];  
        private ButtonGroup btngrpVertical = new ButtonGroup();  
        private ButtonGroup btngrpHorizontal = new ButtonGroup();  
     
        public FD()  
        {  
            JPanel panelVertical = new JPanel(new GridLayout(0, 1, 5, 5));  
            for (int i = 0; i < verticalRadioBtns.length; i++)  
            {  
                verticalRadioBtns[i] = new JRadioButton("radioArrayVertical Button " + i);  
                btngrpVertical.add(verticalRadioBtns[i]);  
                panelVertical.add(verticalRadioBtns[i]);  
            }  
     
            JPanel panelHorizontal = new JPanel(new GridLayout(1, 0, 5, 5));  
            for (int i = 0; i < horizontalRadioBtns.length; i++)  
            {  
                horizontalRadioBtns[i] = new JRadioButton("radioArray2 Button " + i);  
                btngrpHorizontal.add(horizontalRadioBtns[i]);  
                panelHorizontal.add(horizontalRadioBtns[i]);  
            }  
     
            panelVertical.setBorder(BorderFactory.createTitledBorder("Vertical Layout"));  
            panelHorizontal.setBorder(BorderFactory.createTitledBorder("Horizontal Layout"));
     
     
            //HERE IS MY PROBLEM !!!!
            JScrollPane scroll = new JScrollPane(panelVertical,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
     
            mainPanel.add(panelVertical);  
            mainPanel.add(panelHorizontal);
     
        }  
     
        public JPanel getMainPanel()  
        {  
            return mainPanel;  
        }  
     
        private static void createAndShowUI()  
        {  
            JFrame frame = new JFrame("Abbas");  
            frame.getContentPane().add(new FD().getMainPanel());  
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            frame.pack();  
            //frame.setLocationRelativeTo(null);
            //frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            frame.setVisible(true);
        }  
     
        public static void main(String[] args)  
        {  
            java.awt.EventQueue.invokeLater(new Runnable()  
            {  
                public void run()  
                {  
                    createAndShowUI();
     
                }  
            });  
        }  
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding Scrollpane to jPanel

    Where do you add the JScrollPane object to any container?

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding Scrollpane to jPanel

    Aren't these codes adding ?

    JScrollPane scroll = new JScrollPane(panelVertical,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    mainPanel.add(panelVertical);

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding Scrollpane to jPanel

    The first statement creates a JScrollPane object.
    The second statement adds panelVertical to the mainPanel container?

    Where do you add scroll to anything?

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Adding Scrollpane to jPanel

    Void - Norm beat me to it.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding Scrollpane to jPanel

    Apparently i understood this example all wrong. I will research on it, thank you norm.

    Edit: I read so many articles and i saw that if you want to create an advanced GUI, you have to use hard coding. Up to this time, i have been using netbeans GUI builder, every problem occured because of netbeans standarts. So, now i started over to my project. I want to say that maybe it helps another people.
    Last edited by Onur; August 30th, 2011 at 09:14 AM.

Similar Threads

  1. Adding two matrix together
    By papated21 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 2nd, 2011, 01:52 PM
  2. adding up odd and even numbers
    By darlinho in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 30th, 2010, 03:28 PM
  3. [SOLVED] What is not right here? adding JPanel in JFrame
    By Asido in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2010, 08:16 AM
  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, 02: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