?
Solved

JTextArea border that includes text

Posted on 2004-11-04
24
Medium Priority
?
3,323 Views
Last Modified: 2013-11-18
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

0
Comment
Question by:ellandrd
  • 11
  • 7
  • 4
  • +1
24 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 12492898
Can you give the textarea a TitledBorder?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12492909
   TitledBorder titledBorder = new TitledBorder(BorderFactory.createLineBorder(new Color(153, 153, 153),2),"Woo!");
    jTextArea.setBorder(titledBorder);
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12492916
you can use tabbed pane and add text area.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Expert Comment

by:tomboshell
ID: 12492917
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
 
LVL 6

Expert Comment

by:expertmb
ID: 12492970
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
 
LVL 35

Expert Comment

by:TimYates
ID: 12492995
Isn't that a tabbedpane rather than a border with the title written in the top left?

;-)
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12493028
but gives feel of text area :)
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 12493048
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
 
LVL 16

Author Comment

by:ellandrd
ID: 12493898
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
 
LVL 35

Expert Comment

by:TimYates
ID: 12493905
import javax.swing.border.*;
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12493909
> 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
 
LVL 16

Author Comment

by:ellandrd
ID: 12493926
for expertmb

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

sean
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12493936
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
 
LVL 6

Expert Comment

by:expertmb
ID: 12493998
>>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
 
LVL 16

Author Comment

by:ellandrd
ID: 12494056
"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
 
LVL 35

Expert Comment

by:TimYates
ID: 12494080
just do:

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

then...
0
 
LVL 16

Author Comment

by:ellandrd
ID: 12494154
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
 
LVL 16

Author Comment

by:ellandrd
ID: 12494213
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
 
LVL 35

Expert Comment

by:TimYates
ID: 12494225
put the title on the scrollpane like I told you to a few posts ago...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12494237
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
 
LVL 16

Author Comment

by:ellandrd
ID: 12494304
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
 
LVL 35

Accepted Solution

by:
TimYates earned 2000 total points
ID: 12494358
// 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
 
LVL 16

Author Comment

by:ellandrd
ID: 12494390
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
 
LVL 35

Expert Comment

by:TimYates
ID: 12494402
:-)  Good luck!

Tim
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question