Results 1 to 9 of 9
  1. #1
    terox13 is offline Member
    Join Date
    May 2008
    Posts
    6
    Rep Power
    0

    Default [SOLVED] JScrollPane - HELP!

    I've spent hours surfing the web trying to get a simple JScrollPane to work.
    I've tried many different things but basically this is what I have:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class Main extends JFrame {
    
        JTextArea text;
        JScrollPane scroll;
        
        public Main() {
    	super("");
    	setLayout(null);
    	setSize(200,200);
    	
    	text = new JTextArea();
    	text.setBounds(20,20,100,100);
    	
    	scroll = new JScrollPane(text);
    	
    	add(text);
    	add(scroll);
        }
    
        public static void main(String[] args) {
            JFrame frame = new Main();
            frame.setVisible(true);
        }
    
    }
    If I go past the limits of the JTextArea no scrollbar appears! Any help would be greatly appreciated.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    What is the purpose of adding textArea to Scrollpane if you just add it directly to JFrame?

    remove add(text);
    freedom exists in the world of ideas

  3. #3
    terox13 is offline Member
    Join Date
    May 2008
    Posts
    6
    Rep Power
    0

    Default

    I'm not sure I quite understand. If I remove add(text) then the JTextArea won't appear on the JFrame. I am really confused on how to use the scrollbar.

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Ok, you have no containers that holds your component inside the JFrame....

    add this to your code,
    Java Code:
    getContentPane().add(scroll);
    	this.setDefaultCloseOperation(3);
    Replacement for add(text) and add(scroll);

    3 means Exit on close.....
    getContentPane returns a container that will hold the ScrollPane that holds the textArea.
    Last edited by sukatoa; 05-10-2008 at 04:09 AM.
    freedom exists in the world of ideas

  5. #5
    terox13 is offline Member
    Join Date
    May 2008
    Posts
    6
    Rep Power
    0

    Default

    If I replace add(text) and add(scroll) with
    Java Code:
    getContentPane().add(scroll);
    	this.setDefaultCloseOperation(3);
    the JTextArea doesn't appear in the frame. I just want to have a JTextArea that I can put anywhere inside the frame with a scrollbar. I'm not very familiar with the concepts of constructing a GUI, such as the container. I would appreciate any help such as code on how to do this or a site where I can learn. I've been searching and searching and haven't found one that works for me. Thanks for you're help.

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    eg.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class test extends JFrame {
    
        JTextArea text;
        JScrollPane scroll;
        
        public test() {
            super("TESTING");
            setSize(200,200);
    	
            text = new JTextArea();
            scroll = new JScrollPane(text);
    	
            getContentPane().add(scroll);
            setDefaultCloseOperation(3);
        }
    
        public static void main(String[] args) {
            new test().setVisible(true);
        }
    }
    freedom exists in the world of ideas

  7. #7
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    freedom exists in the world of ideas

  8. #8
    terox13 is offline Member
    Join Date
    May 2008
    Posts
    6
    Rep Power
    0

    Default

    Ohh thanks. I figured out what i did wrong.
    Java Code:
    text.setBounds(20,20,100,100);
    I was setting the bounds of the JTextArea instead of the JScrollPanel, like this:
    Java Code:
    scroll.setBounds(20,20,100,100);
    Thanks for your help!

  9. #9
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    No problem bro....

    Just mark this thread as SOLVED...
    freedom exists in the world of ideas

Similar Threads

  1. JScrollPane with HTML
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 12:32 PM
  2. jscrollpane problem
    By monkey04 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2008, 06:23 AM
  3. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:58 PM
  4. how to draw an image inside of jscrollpane
    By paty in forum Java Applets
    Replies: 1
    Last Post: 07-24-2007, 01:44 AM
  5. JScrollPane not scrolling
    By Riftwalker in forum Advanced Java
    Replies: 2
    Last Post: 07-17-2007, 09: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
  •