JTextArea border that includes text

hello experts

right is a tough one which i dont know if it can be done, but here it goes:

i want to create a JTextArea that has a border around it and a label describing the JTextArea in the upperleft corner of it

e.g when you create a textarea in VB and give that textarea a label, the label appears in the upper left corner!
do you know what i mean?

this is what im trying to do in java for a JTextArea, but dont know if its possible??

if possible i require examples, ideas, links, etc to do this...

cheers
sean

LVL 16
ellandrdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
Can you give the textarea a TitledBorder?
0
TimYatesCommented:
   TitledBorder titledBorder = new TitledBorder(BorderFactory.createLineBorder(new Color(153, 153, 153),2),"Woo!");
    jTextArea.setBorder(titledBorder);
0
expertmbCommented:
you can use tabbed pane and add text area.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

tomboshellCommented:
here is a fun one I use
txtArea.setBorder(new TitledBorder(new LineBorder(Color.LIGHT_GRAY,2, true), "The text",
                                                TitledBorder.CENTER, TitledBorder.TOP,
                                                new Font("SansSerif",Font.BOLD, 12),
                                                Color.BLACK);
0
expertmbCommented:
import java.awt.*;
import javax.swing.*;

class TabbedPaneExample1
            extends       JFrame
{
      private            JTabbedPane tabbedPane;
      private            JTextArea      txtArea;


      public TabbedPaneExample1()
      {
            setTitle( "Text Area" );
            setSize( 300, 200 );
            setBackground( Color.gray );

            JPanel topPanel = new JPanel();
            topPanel.setLayout( new BorderLayout() );
            getContentPane().add( topPanel );

            txtArea = new JTextArea();

            // Create a tabbed pane
            tabbedPane = new JTabbedPane();
            tabbedPane.addTab( "Text Area", txtArea );
            topPanel.add( tabbedPane, BorderLayout.CENTER );
      }

      public static void main( String args[] )
      {

            TabbedPaneExample1 mainFrame      = new TabbedPaneExample1();
            mainFrame.setVisible( true );
      }
}
0
TimYatesCommented:
Isn't that a tabbedpane rather than a border with the title written in the top left?

;-)
0
expertmbCommented:
but gives feel of text area :)
0
tomboshellCommented:
Tim already answered the question, simply use the code I gave as a further example of what you can/could do (round the corners, move the text location, change the font, change the color).  
0
ellandrdAuthor Commented:
right ok, im getting errors in "TitledBorder"


heres my code:

JTextArea textareaEng = new JTextArea("Please enter your English text here...");
JTextArea textareaFre = new JTextArea("Please enter your French text here...");
JTextArea textareaSol = new JTextArea("Please enter your SolReSol text here...");

TitledBorder titledBorderEng = new TitledBorder(BorderFactory.createLineBorder(new Color(0, 0, 255),2),"English");
TitledBorder titledBorderFre = new TitledBorder(BorderFactory.createLineBorder(new Color(0, 0, 255),2),"French");
TitledBorder titledBorderSol = new TitledBorder(BorderFactory.createLineBorder(new Color(0, 0, 255),2),"SolReSol");

textareaEng.setBorder(titledBorderEng);
textareaFre.setBorder(titledBorderFre);
textareaSol.setBorder(titledBorderSol);

textareaEng.setFont(new Font("Trebuchet MS", Font.PLAIN, 14));
textareaFre.setFont(new Font("Trebuchet MS", Font.PLAIN, 14));
textareaSol.setFont(new Font("Trebuchet MS", Font.PLAIN, 14));

this.c.add(new JScrollPane(textareaEng));
this.c.add(new JScrollPane(textareaFre));
this.c.add(new JScrollPane(textareaSol));

centerPanel.add(textareaEng);
centerPanel.add(textareaFre);
centerPanel.add(textareaSol);

this.c.add(centerPanel, FlowLayout.LEFT);

the error message is

H:\ApplicationGUI.java:80: cannot resolve symbol
symbol  : class TitledBorder
location: class ApplicationGUI
TitledBorder titledBorder = new TitledBorder(BorderFactory.createLineBorder(new Color(153, 153, 153),2),"English");
                ^
Also when i run the apllication, i cant see my hscrollbar and vscrollbar if the user enters alot of text....

need this to be fixed too but cant seem to get it working!

any ideas here im going wrong?

cheers
sean
0
TimYatesCommented:
import javax.swing.border.*;
0
TimYatesCommented:
> Also when i run the apllication, i cant see my hscrollbar and vscrollbar if the user enters alot of text....

Have you added the TextArea into a scrollpane?

You need a scrollpane to get the scrollbars...

Then set the border of the scrollpane ;-)
0
ellandrdAuthor Commented:
for expertmb

i cant get your code to work, i get an erro of thread "main" appliction somethingy....

sean
0
TimYatesCommented:
Ahhh...I see you are...but this:

---------
this.c.add(new JScrollPane(textareaEng));
this.c.add(new JScrollPane(textareaFre));
this.c.add(new JScrollPane(textareaSol));

centerPanel.add(textareaEng);
centerPanel.add(textareaFre);
centerPanel.add(textareaSol);

this.c.add(centerPanel, FlowLayout.LEFT);
---------

seems to be a bit of a mess...

What is "this.c"?  why are you adding things to it, then adding them elsewhere?

Components can only have one parent, you can't add them all over the place...

Don't you mean:

centerPanel.add(new JScrollPane(textareaEng));
centerPanel.add(new JScrollPane(textareaFre));
centerPanel.add(new JScrollPane(textareaSol));
this.c.add(centerPanel, FlowLayout.LEFT);

(whatever this.c is)
0
expertmbCommented:
>>i cant get your code to work, i get an erro of thread "main" appliction somethingy....
please post that error
it is simple application. dont know its not working?
is it compiling. or u r facing problem while running app.
0
ellandrdAuthor Commented:
"c" is ContentPane()

i didnt notice i have "this" infront of it...

ill try it and hopefully it will fix it!?

be back in a few mins

sean
0
TimYatesCommented:
just do:

c.add(new JScrollPane(textareaEng));
c.add(new JScrollPane(textareaFre));
c.add(new JScrollPane(textareaSol));

then...
0
ellandrdAuthor Commented:
sorry expertmb i got it working now, i saved it a ex001.java instead of the name you gave it...

silly mistake, but fixed now!

thanks
0
ellandrdAuthor Commented:
right that all worked includes textarea border,

but i want to have the color or the border around the textarea the same color as the actual textarea?,

plus when i enter a few lines of text and i can scroll up and down, my border scrolls too making it not visible anymore!

is there any way of getting around this or having the border to appear on the outside of the textarea so when user scroll, the border and label is stillv visiable??

sean
0
TimYatesCommented:
put the title on the scrollpane like I told you to a few posts ago...
0
TimYatesCommented:
JScrollPane engScroll = new JScrollPane(textareaEng) ;
TitledBorder titledBorderEng = new TitledBorder(BorderFactory.createLineBorder(new Color(0, 0, 255),2),"English");
engScroll.setBorder( titledBorderEng ) ;
c.add( engScroll ) ;
0
ellandrdAuthor Commented:
ok that last part of code has confused me??

this is what ive got:

// center
JPanel centerPanel = new JPanel(new GridLayout(3,1,15,15));
centerPanel.setBackground(bColor);

JTextArea textareaEng = new JTextArea("");
JTextArea textareaFre = new JTextArea("");
JTextArea textareaSol = new JTextArea("");

JScrollPane engScroll = new JScrollPane(textareaEng) ;
TitledBorder titledBorderEng = new TitledBorder(BorderFactory.createLineBorder(new Color(0, 0, 255),2),"English");
engScroll.setBorder( titledBorderEng ) ;
c.add( engScroll ) ;

i commented out this part to add your code above, but now i cant see the border??

//textareaEng.setBorder(new TitledBorder(new LineBorder(new Color(153, 153, 153), 1, true), "English", TitledBorder.LEFT, TitledBorder.TOP, new Font("Trebuchet MS",Font.PLAIN, 14), new Color(153, 153, 153)));
//textareaFre.setBorder(new TitledBorder(new LineBorder(new Color(153, 153, 153), 1, true), "French", TitledBorder.LEFT, TitledBorder.TOP, new Font("Trebuchet MS",Font.PLAIN, 14), new Color(153, 153, 153)));
//textareaSol.setBorder(new TitledBorder(new LineBorder(new Color(153, 153, 153), 1, true), "SolReSol", TitledBorder.LEFT, TitledBorder.TOP, new Font("Trebuchet MS",Font.PLAIN, 14), new Color(153, 153, 153)));

textareaEng.setFont(new Font("Trebuchet MS", Font.PLAIN, 14));
textareaFre.setFont(new Font("Trebuchet MS", Font.PLAIN, 14));
textareaSol.setFont(new Font("Trebuchet MS", Font.PLAIN, 14));

centerPanel.add(new JScrollPane(textareaEng));
centerPanel.add(new JScrollPane(textareaFre));
centerPanel.add(new JScrollPane(textareaSol));
c.add(centerPanel, FlowLayout.LEFT);

sean
0
TimYatesCommented:
// center
JPanel centerPanel = new JPanel(new GridLayout(3,1,15,15));
centerPanel.setBackground(bColor);

JTextArea textareaEng = new JTextArea("");
JTextArea textareaFre = new JTextArea("");
JTextArea textareaSol = new JTextArea("");

JScrollPane engScroll = new JScrollPane(textareaEng) ;
JScrollPane freScroll = new JScrollPane(textareaFre) ;
JScrollPane solScroll = new JScrollPane(textareaSol) ;

engScroll.setBorder(new TitledBorder(new LineBorder(new Color(153, 153, 153), 1, true), "English", TitledBorder.LEFT, TitledBorder.TOP, new Font("Trebuchet MS",Font.PLAIN, 14), new Color(153, 153, 153)));
freScroll.setBorder(new TitledBorder(new LineBorder(new Color(153, 153, 153), 1, true), "French", TitledBorder.LEFT, TitledBorder.TOP, new Font("Trebuchet MS",Font.PLAIN, 14), new Color(153, 153, 153)));
solScroll.setBorder(new TitledBorder(new LineBorder(new Color(153, 153, 153), 1, true), "SolReSol", TitledBorder.LEFT, TitledBorder.TOP, new Font("Trebuchet MS",Font.PLAIN, 14), new Color(153, 153, 153)));

textareaEng.setFont(new Font("Trebuchet MS", Font.PLAIN, 14));
textareaFre.setFont(new Font("Trebuchet MS", Font.PLAIN, 14));
textareaSol.setFont(new Font("Trebuchet MS", Font.PLAIN, 14));

centerPanel.add( engScroll );
centerPanel.add( freScroll );
centerPanel.add( solScroll );
c.add(centerPanel, FlowLayout.LEFT);

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ellandrdAuthor Commented:
excellant work!

thank you very much!!

500 points and grade a awarded...

ive another question too and ill post it in a min as i wanna give you the points for this first...

be back in a min, and keep a eye out for my next question in bout 5 mins..

sean
0
TimYatesCommented:
:-)  Good luck!

Tim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.