Link to home
Start Free TrialLog in
Avatar of tdavidson_9
tdavidson_9

asked on

Java Swing JTextArea didn't show up???

Can you help me to debug my simple code please? The JTextArea displayArea didn't show up :(

import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Display implements  ActionListener{

      JPanel GUI;
      JTextArea displayArea;
      JLabel separatorLabel;
      JTextField separatorField;
      JButton enterButton;
      String content = "Hello";

      public JPanel createContentPane (){

            GUI = new JPanel();
            GUI.setLayout(null);

            displayArea = new JTextArea(content);
            displayArea.setEditable(false);
            displayArea.setLineWrap(false);
            displayArea.setWrapStyleWord(true);

            JScrollPane scrollBar = new JScrollPane(displayArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

            scrollBar.setPreferredSize(new Dimension(0, 300));
            GUI.add(scrollBar);

            separatorLabel = new JLabel("Please enter a character ");
            separatorLabel.setForeground(Color.red);
            separatorLabel.setLocation(20, 525);
            separatorLabel.setSize(220, 40);
            GUI.add(separatorLabel);

            separatorField = new JTextField(8);
            separatorField.setLocation(250, 530);
            separatorField.setSize(50, 30);
            GUI.add(separatorField);

            enterButton = new JButton("Enter");
            enterButton.setLocation(310, 530);
            enterButton.setSize(80, 30);
            enterButton.addActionListener(this);
            GUI.add(enterButton);
            GUI.setOpaque(true);    
            return GUI;
      }
      public void actionPerformed(ActionEvent e) {
            if(e.getSource() == enterButton)
            {
                        System.out.println("ok");
                        System.exit(0);
            }
      }
      private static void createAndShowGUI() {
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame frame = new JFrame("Request message cookies");
            Display display = new Display();
            frame.setContentPane(display.createContentPane());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800, 600);
            frame.setVisible(true);
      }
      public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                        createAndShowGUI();
                  }
            });
      }
}
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tdavidson_9
tdavidson_9

ASKER

It works! But it screwed up the other objects location e.g. separatorField.setLocation(250, 530) no longer displayed at (250, 530) :(
Here take a look at the other LayoutManager options:
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
Thanks a lot!!!
No problem.  You are welcome.
Hello mwvisa1,

I am so sorry that can I ask you just one more question? If I want to set the value of the variable content i.e.  String content = "Hello"; equal to the argument of args[0] instead of "hello", is it possible to do it? Thanks!
Yes it is possible.

If you have difficulty implementing you can start a new Q.