|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

05-10-2008, 02:18 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 6
|
|
|
[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:
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.
|
|

05-10-2008, 03:34 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 509
|
|
|
What is the purpose of adding textArea to Scrollpane if you just add it directly to JFrame?
remove add(text);
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-10-2008, 03:49 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 6
|
|
|
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.
|
|

05-10-2008, 04:01 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 509
|
|
Ok, you have no containers that holds your component inside the JFrame....
add this to your 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.
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Last edited by sukatoa : 05-10-2008 at 04:09 AM.
|
|

05-10-2008, 04:18 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 6
|
|
If I replace add(text) and add(scroll) with
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.
|
|

05-10-2008, 04:47 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 509
|
|
eg.
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);
}
}
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-10-2008, 04:50 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 509
|
|
|
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-10-2008, 04:56 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 6
|
|
Ohh thanks. I figured out what i did wrong.
text.setBounds(20,20,100,100);
I was setting the bounds of the JTextArea instead of the JScrollPanel, like this:
scroll.setBounds(20,20,100,100);
Thanks for your help!
|
|

05-10-2008, 04:58 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 509
|
|
|
No problem bro....
Just mark this thread as SOLVED...
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|