[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java Swing JTextArea didn't show up???

Posted on 2008-09-29
8
Medium Priority
?
334 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Technology Partners: 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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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 …
Suggested Courses
Course of the Month20 days, 10 hours left to enroll

868 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