File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes read-only text area Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "read-only text area" Watch "read-only text area" New topic
Author

read-only text area

Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Hi all!
I have a class which extends Frame and contains a TextArea, which I would like to be read-only. I attempted to do this by calling:
myTextArea.setEditable(false);
This works, but it also sets the background of my TextArea, which I explicity set to black by calling:
myTextArea.setBackground(java.awt.Color.black);
back to gray. Am I doing something wrong or does anyone know a work-around whereby I can create a read-only TextArea with a black background?
Thanks in advance.
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
hi stephine ,
i tried to do what u r doing & it worked perfectly for me. i got a text area with black background only.
my code is ............

import java.awt.*;
public class Tex extends Frame{
TextArea jf = new TextArea(5,5);
Tex()
{
add(jf);
jf.setBackground(Color.black);
jf.setEditable(false);
}
public static void main(String j[])
{
Tex t = new Tex();
t.setSize(300,300);
t.setVisible(true);

}
}


note : i tested it with swing also and in that case also the textarea was black. i'm using jdk1.2.1 on win NT
if possible do post ur code.
regards
deekasha
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Deekasha,
Thanks for the reply. Here is my code:
I am running it on WindowsNT 4.0 using JDK1.3.
------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
public class ServerCMIS extends Frame
{
TextArea history = new TextArea();
private ServerCMIS(String name, int width, int height)
{
super(name);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
// set up the "history" text area, where message will be displayed to user
add(history, BorderLayout.NORTH);
history.setForeground(java.awt.Color.red);
history.setBackground(java.awt.Color.black);
history.setEditable(false);
history.append("\nthis is a test");
setSize(width, height);
show();
}
public static void main(String args[])
{
ServerCMIS server = new ServerCMIS("CMIS Remote Server - version 2.0", 640, 480);
}
}
------------------------------------------------------
The only real difference I see between this code and your example is that my constructor is private and you call setSize() and show() from main() rather than from the constructor. It doesn't seem like this should make any difference. Do you see something I am missing? Thanks for your help. Stephanie
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Thanks for your help!
The code is below. I am using WindowsNT 4.0 with JDK 1.3.
My TextArea background is definitely gray, not black.
----------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
public class ServerCMIS extends Frame
{
TextArea history = new TextArea();
private ServerCMIS(String name, int width, int height)
{
super(name);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
// set up the "history" text area, where message will be displayed to user
add(history, BorderLayout.NORTH);
history.setEditable(false);
history.setForeground(java.awt.Color.red);
history.setBackground(java.awt.Color.black);
history.append("\nthis is a test");
setSize(width, height);
show();
}
public static void main(String args[])
{
ServerCMIS server = new ServerCMIS("CMIS Remote Server - version 2.0", 640, 480);
}
}
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello St�phanie,
I use a JTextArea in a JDialog and there is no problem:
JTextArea jTextArea2 = new JTextArea();
jTextArea2.setBackground(SystemColor.info); // yellow
jTextArea2.setEditable(false);
see you.
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Olivier,
Thank you for your suggestion. The only problem is, I need to use AWT exclusively, not Swing. Do you have any idea what might be wrong with the code as it is written?
Thanks in advance.
Stephanie
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello St�phanie,
I pasted your code in my JBuilder 3.5 editor (JDK 1.2.2) and compiled it. It works perfectly: the "history.setEditable(false);" statement doesn't change the background color of the history TextArea...
Maybe you need to update your JDK version?
See you, Olivier
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Olivier,
You bring up a good point. I am using JDK version 1.3.
When I tried compiling and running the same code on a machine running JDK 1.2.2, it works fine as you said.
Perhaps it is a bug in 1.3?
Stephanie
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I've seen the same behaviour on 1.3. Has anyone checked the 1.3 bug list?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

I had a similar issue I wanted to make sure the Component would not get focus ever, and be not editable.
I got around it by subclassing the JTextField, and overriding the isFocusTraversible() method to return false. I am not sure if it kept the JTextField gray though.
and it should work for JTextArea too
Here's my code.

import javax.swing.*;
public class NoFocusJTextField extends JTextField{
NoFocusJTextField(){
super();
setEnabled(true);
setEditable(false);
}
public boolean isFocusTraversible(){
return false;
}
}

Hope this helps and isn't too late
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read-only text area
 
Similar Threads
TextArea wrapping
Hibernate and second level cache
changing the color of one char
Khalid Programming Ex. 12.1
BufferedImage.TYPE_INT_ARGB is producing a black background