Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

read-only text area

 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen the same behaviour on 1.3. Has anyone checked the 1.3 bug list?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic