Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java Swing JTextArea didn't show up???

Posted on 2008-09-29
8
Medium Priority
?
332 Views
Last Modified: 2013-11-23
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();
                  }
            });
      }
}
0
Comment
Question by:tdavidson_9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1000 total points
ID: 22601717
Think you have to set a layout manager:
GUI.setLayout(new FlowLayout()); // use something appropriate for your application

scrollBar.setPreferredSize(new Dimension(400, 300)); // add a width to your dimension

The two of these together and you will see JTextArea.
0
 

Author Comment

by:tdavidson_9
ID: 22601862
It works! But it screwed up the other objects location e.g. separatorField.setLocation(250, 530) no longer displayed at (250, 530) :(
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22601898
Here take a look at the other LayoutManager options:
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:tdavidson_9
ID: 22601911
Thanks a lot!!!
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22601922
No problem.  You are welcome.
0
 

Author Comment

by:tdavidson_9
ID: 22602342
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!
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22602420
Yes it is possible.

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

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question