javamate06
asked on
Adding JScrolBar to JTextArea
How to add JScrolBar to JTextArea and make it automatically visible if needed?
If I have:
JTextArea textArea = new JTextArea();
If I have:
JTextArea textArea = new JTextArea();
ASKER
I mean JScrolBar beacuse I tried
JScrollPane pane = new JScrollPane(textArea);
but it does not work
JScrollPane pane = new JScrollPane(textArea);
but it does not work
a scroll pane will add scrolls as required.
or u can change the policy if u always want them to appear
or u can change the policy if u always want them to appear
ASKER
When I do this
JScrollPane pane = new JScrollPane(textArea);
The textArea is not visible in the frame.
JScrollPane pane = new JScrollPane(textArea);
The textArea is not visible in the frame.
are you adding the scroll pane to your frame?
and *not* adding the text area.
and *not* adding the text area.
ASKER
Yes,
I did this:
JScrollPane pane = new JScrollPane();
this.getContentPane().add( pane);
pane = new JScrollPane(textArea);
is it correct?
I did this:
JScrollPane pane = new JScrollPane();
this.getContentPane().add(
pane = new JScrollPane(textArea);
is it correct?
On the 3rd line you throw the 1st pane away!
;JOOP!
;JOOP!
Should be:
JScrollPane pane = new JScrollPane(textArea);
this.getContentPane().add( pane);
;JOOP!
JScrollPane pane = new JScrollPane(textArea);
this.getContentPane().add(
;JOOP!
ASKER
The text is visible now,
Still the scroll bar does not working and it is not visible
Still the scroll bar does not working and it is not visible
Of course not when the text area is smaller than the panel.
Just fill it with lots of text and the scroll bars as needed appear.
You can also force the scrollbars into sight by:
pane.setVerticalScrollBarP olicy(Scro llPaneCons tants.VERT ICAL_SCROL LBAR_ALWAY S);
pane.setHorizontalScrollBa rPolicy(Sc rollPaneCo nstants.HO RIZONTAL_S CROLLBAR_A LWAYS);
but you will probably not like that after some time.
;JOOP!
Just fill it with lots of text and the scroll bars as needed appear.
You can also force the scrollbars into sight by:
pane.setVerticalScrollBarP
pane.setHorizontalScrollBa
but you will probably not like that after some time.
;JOOP!
ASKER
"Of course not when the text area is smaller than the panel.
Just fill it with lots of text and the scroll bars as needed appear."
It was needed but it was not appear.
Just fill it with lots of text and the scroll bars as needed appear."
It was needed but it was not appear.
ASKER
OK...
Can some one help me and give me sample exaple:
TextArea with scrollBar.
Can some one help me and give me sample exaple:
TextArea with scrollBar.
I gave you a correct and working example.
To what are you adding this JScrollPane?
Try this to see it work:
JFrame fr = new JFrame();
fr.setSize(400, 300);
fr.getContentpane().add(pa ne, BorderLayout.SOUTH);
And understand why you didn't see it before.
;JOOP!
To what are you adding this JScrollPane?
Try this to see it work:
JFrame fr = new JFrame();
fr.setSize(400, 300);
fr.getContentpane().add(pa
And understand why you didn't see it before.
;JOOP!
ASKER
-------------------------- -
Lets I have this class
I want to show the all strings written in the TextArea by sorollBar movement down and up.
-------------------------- --
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JTextArea;
import java.awt.Rectangle;
public class Frame1 extends JFrame
{
private JTextArea textArea = new JTextArea();
public Frame1()
{
try
{
test();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void test() throws Exception
{
this.getContentPane().setL ayout(null );
this.setSize(new Dimension(263, 103));
this.setTitle("Text Area Test");
textArea.setText("Test\nte st\ntest\n Test\ntest \ntest");
textArea.setBounds(new Rectangle(10, 5, 230, 50));
this.getContentPane().add( textArea, null);
}
}
Lets I have this class
I want to show the all strings written in the TextArea by sorollBar movement down and up.
--------------------------
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JTextArea;
import java.awt.Rectangle;
public class Frame1 extends JFrame
{
private JTextArea textArea = new JTextArea();
public Frame1()
{
try
{
test();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void test() throws Exception
{
this.getContentPane().setL
this.setSize(new Dimension(263, 103));
this.setTitle("Text Area Test");
textArea.setText("Test\nte
textArea.setBounds(new Rectangle(10, 5, 230, 50));
this.getContentPane().add(
}
}
>>> this.getContentPane().setL ayout(null ); // ?????????????????????????? ??????? Nonsense.
>>> textArea.setBounds(new Rectangle(10, 5, 230, 50)); // Why? That spoils the whole thing.
I don't know how you have come to that code. Why fumble in constructors?
Use my code instead.
;JOOP!
>>> textArea.setBounds(new Rectangle(10, 5, 230, 50)); // Why? That spoils the whole thing.
I don't know how you have come to that code. Why fumble in constructors?
Use my code instead.
;JOOP!
ASKER
When I call it from the main method it is working
what is wrong in it?
The problem in the ScrollBar only I cannnot do it
what is wrong in it?
The problem in the ScrollBar only I cannnot do it
Delete that stupid "setLayout(null);"
Do not set the size on the text area! You want it to scroll? Set a size on the JScrollPane().
;JOOP!
Do not set the size on the text area! You want it to scroll? Set a size on the JScrollPane().
;JOOP!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
May be he listens to you .......................... .......... ........
He finally did.
then add the pane to your gui instead of your textArea