Solved

Java Swing JTextArea didn't show up???

Posted on 2008-09-29
8
320 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
  • 4
  • 4
8 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
Comment Utility
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
Comment Utility
It works! But it screwed up the other objects location e.g. separatorField.setLocation(250, 530) no longer displayed at (250, 530) :(
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
Here take a look at the other LayoutManager options:
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
0
 

Author Comment

by:tdavidson_9
Comment Utility
Thanks a lot!!!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
No problem.  You are welcome.
0
 

Author Comment

by:tdavidson_9
Comment Utility
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 59

Expert Comment

by:Kevin Cross
Comment Utility
Yes it is possible.

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

Author Comment

by:tdavidson_9
Comment Utility
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now