5

I have a swing application, and I've written code to change the background color of the JTextArea. However, it gives me exceptions.

Here is the code:

//1.JtextArea will work after maximize.
//2.on typing text,background  will slowly transform to black line by line.

import java.awt.*;
import javax.swing.*;

public class TextArea {

    JTextArea area;
    JFrame frame;

    public static void main(String args[])                     
    {
        TextArea x = new TextArea();
        x.execute();                                                       
    }               

    void execute()
    {
        frame = new JFrame();
        frame.setVisible(true);
        frame.setSize(600,600);
        frame.setTitle("Temp Area");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        area = new JTextArea();
        frame.add(area,BorderLayout.CENTER);

        Color c = new Color(0,0,0,100);
        area.setBackground(c);
    }
}
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
Shivam Sharma
  • 315
  • 3
  • 5
  • 15
  • What is the question therefore? –  Sep 26 '13 at 13:06
  • what is the problem ??? – Hemant Metalia Sep 26 '13 at 13:07
  • changing background color of JTextArea in JAVA gives unexpected results.why? – Shivam Sharma Sep 26 '13 at 13:09
  • `//1.JtextArea will work after maximize.`, but why? Have you tried to ask yourself this question? Now change the sequence of your code a bit, in the sense, first add all components to the `JFrame` now call `frame.pack()` and now `frame.setVisible(true)`. Now you don't have to maximize the `JFrame` to see the `JTextArea`. Since you are trying to add components to the already visible container, which leads to this obnoxious behaviour. Now with the remedy specified, you first adding components and then setting the container to visible state, after it has realized it's size, so everything is good – nIcE cOw Sep 26 '13 at 13:24
  • @Shivam_Sharma : Moreover, use the constructor of `JTextArea` like `JTextArea tArea = new JTextArea(10, 10)` to specify it's size, with respect to rows and columns. Then you don't even have to resize it, in many situations. – nIcE cOw Sep 26 '13 at 13:45
  • One related [example](http://stackoverflow.com/a/14291502/1057230) and [another](http://stackoverflow.com/q/17759287/1057230) – nIcE cOw Sep 26 '13 at 14:01

2 Answers2

4
  • you need to move code line frame.setVisible(true); as last code in void execute()

  • because you added JTextArea to the already visible Swing GUI, that isn't builded on Initial Thread

  • another important:

    • rename public class TextArea { to public class MyTextArea {, because TextArea is reserved Java word for awt.TextArea

    • TextArea x=new TextArea(); and x.execute(); should be wrapped into invokeLater, more to se in Oracle tutorial Initial Thread

mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

your textarea taken all the space of frame.

add two more lines in the code, then text area taken specfic portion of frame.

area.setBounds(20,20,100,30);
frame.setLayout(null);
Dev
  • 3,410
  • 4
  • 17
  • 16
  • 1
    Very bad advice, you providing in this answer, regarding the use of `null` Layout, which is to be used at the very last, when all other options are dealt with fully. But not in this situation, where a Layout Manager is fully capable of performing it's duties as specified. – nIcE cOw Sep 26 '13 at 13:27