Solved

Java Swing JTextArea didn't show up???

Posted on 2008-09-29
8
327 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 250 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

690 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