Java.awt: Second TextArea not showing

I am trying to understand how Java.awt works (we need to create a GUI without a GUI editor)

the following code doesn't show 2 TextAreas:

Frame fr = new Frame("Parser");
Panel buttons = new Panel();
Panel inputText = new Panel();
Panel outputText = new Panel();
String here = new String ("Insert code here...");
TextArea input = new TextArea(here, 9, 96, TextArea.SCROLLBARS_VERTICAL_ONLY);
TextArea output = new TextArea(here, 9,96,TextArea.SCROLLBARS_VERTICAL_ONLY);

public Window(){
    fr.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {

    fr.setSize(700, 400);


    buttons.setBounds(new Rectangle(0,0,700,60));
    buttons.setBackground(new Color(200,200,200));

    inputText.setBounds(new Rectangle(0,60,700,170));
    inputText.setBackground(new Color(255,255,255));

    outputText.setBounds(new Rectangle(0,230,700,170));
    outputText.setBackground(new Color(200,200,200));



The result is:

Window with only 1 text area

Expected Result:

enter image description here


source to share

2 answers

Your code does not follow the layout managers that are used in your containers. I believe AWT Frames use BorderLayout by default (edit: yes, the Frame API does . Suggestions:

  • In general, avoid AWT for Swing, which has much more power and flexibility, although it also shows its age, which is less than AWT.
  • Read and use layout managers in a smart way to make your hard climb for you. Here it looks like BoxLayout can help you.
  • Avoid using the right layouts. While yes, it might offer you a quick and easy fix for your current code, it results in a very inflexible GUI that while they may look good on one platform, look terrible on most other platforms or screen resolutions, and it's very difficult update and maintain.
  • Avoid setting the borders, sizing, or positioning of any components, and again letting the components and their container layout managers sizing for you.

For example:

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

public class MyWindow extends JPanel {
   private static final int ROWS = 10;
   private static final int COLS = 50;
   private static final String[] BUTTON_NAMES = { "Monday", "Tuesday",
         "Wednesday", "Thursday", "Friday" };
   private static final int GAP = 3;
   private JTextArea inputTextArea = new JTextArea(ROWS, COLS);
   private JTextArea outputTextArea = new JTextArea(ROWS, COLS);

   public MyWindow() {
      JPanel buttonPanel = new JPanel(new GridLayout(1, 0, GAP, 0));
      for (String btnName : BUTTON_NAMES) {
         buttonPanel.add(new JButton(btnName));

      setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
      setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
      add(putInTitledScrollPane(inputTextArea, "Input Text"));
      add(putInTitledScrollPane(outputTextArea, "Output Text"));

   private JPanel putInTitledScrollPane(JComponent component,
         String title) {
      JPanel wrapperPanel = new JPanel(new BorderLayout());
      wrapperPanel.add(new JScrollPane(component));
      return wrapperPanel;

   private static void createAndShowGui() {
      MyWindow mainPanel = new MyWindow();

      JFrame frame = new JFrame("MyWindow");

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {


What is displayed as:

enter image description here

Using layout managers gives you much more ease when it comes to changing or improving your GUI. For example, since I set the width of the JTextArea with the COL constant, if I change the COL constant, the entire GUI expands, even the buttons and the JPanel button, as layout managers handle all sizes. With your code, you will have to manually change the width of each component added to the GUI, which is prone to bugs.



Since you are manually putting your components, you need to set the layout to null ( setLayout(null);

so add this line to your code before adding any component.



You will now get this: enter image description here



All Articles