1

Why JTextArea not showing in GUI ?

public class AddMovie extends JTextField {

    static JFrame frame;
    private JLabel description;
    JTextArea movieDescription;

    public JPanel createContentPane() throws IOException
    {

        JPanel totalGUI = new JPanel();
        totalGUI.setLayout(null);
        totalGUI.setBackground(Color.WHITE);

        description = new JLabel("Description  ");
            description.setLocation(15,285);
            description.setSize(120, 25);
            description.setFont(new java.awt.Font("Tahoma", 0, 12));
            movieDescription=new JTextArea();
          movieDescription.setLocation(15,320);
          movieDescription.setSize(420, 110);

        JScrollPane scrollPane = new JScrollPane(movieDescription);    
        totalGUI.add(description);
        totalGUI.add(movieDescription);
        totalGUI.add(cancel);
        totalGUI.add(scrollPane);                   
        return totalGUI;
    }


    static void createAndShowGUI() throws IOException
    {

        JFrame.setDefaultLookAndFeelDecorated(true);
        frame = new JFrame("New Movie");
        //Create and set up the content pane.
        AddMovie demo = new AddMovie();
        frame.setContentPane(demo.createContentPane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(515, 520);
        frame.setLocation(480,120);
        frame.setVisible(true);
    }

    public void setVisible(boolean b) {
        // TODO Auto-generated method stub

    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jul 09 '16 at 07:18
  • @AndrewThompson: In part unwittingly, I implemented most of your suggestions below, but your rationales are better. Please consider adding an answer. – trashgod Jul 09 '16 at 14:46
  • What is supposed to appear in the upper part of the `totalGUI` panel? – Andrew Thompson Jul 09 '16 at 15:18
  • @AndrewThompson others components. –  Jul 10 '16 at 13:52
  • OK.. if you have future problems layout out a group of components.. Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. Once we see what the end effect of the GUI is supposed to be, we can show how to do it with layouts. I was going to do that, but the big blank space in the GUI made me pause. It's pointless trying to show how to do *part* of a layout. – Andrew Thompson Jul 10 '16 at 14:36
  • @AndrewThompson Noted, thanks :) –  Jul 14 '16 at 17:16

3 Answers3

4

As suggested here and here, a null layout invites trouble. Because your display centers on the description, use the JTextArea constructor that lets you specify a size in rows and columns. When you pack() the enclosing Window, it will be resized to fit the text area. I've added some ad hoc text to illustrate the effect. I've also updated the code to allow the text area to grow when resizing the frame.

image

import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * @see https://stackoverflow.com/a/38282886/230513
 */
public class Test {

    private JPanel createPanel() {
        JPanel panel = new JPanel(new GridLayout());
        //panel.setBorder(BorderFactory.createTitledBorder("Description"));
        JTextArea movieDescription = new JTextArea(10, 20);
        panel.add(new JScrollPane(movieDescription));
        movieDescription.setLineWrap(true);
        movieDescription.setWrapStyleWord(true);
        movieDescription.setText(movieDescription.toString());
        return panel;
    }

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(createPanel());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • why after remove `panel.setBorder(BorderFactory.createTitledBorder("Description"));`, the `JTextArea` missing ? –  Jul 09 '16 at 18:07
  • 1
    @Seng: I can't reproduce this; note this [recommendation](https://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#setBorder-javax.swing.border.Border-) set the border on the `JPanel`." – trashgod Jul 10 '16 at 01:31
3
totalGUI.setLayout(null);

There's the 1st problem you should solve: Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.

Besides many other problems, it typically destroys the ability of scroll-panes to do the job they were designed for.

General tips:

  1. There is no attribute named cancel.
  2. Don't override methods like setVisible(boolean) then leave them empty! That is achieving nothing more than breaking functionality that works just fine as it is!
  3. The movieDescription is added to both the totalGUI & the scroll pane. It should only be added to the scroll pane.
  4. A white text area added to a white BG will be invisible except for the caret.
  5. The Tahoma font will only be available in some OS'
  6. Rather than use a JLabel for the description, you might also add the text area to a panel and set the Description text to a TitledBorder used on a panel that the text area is placed in.
  7. Nothing in the two wethods that declare throws IOException could actually cause one.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
-1

In the createAndShowGUI method you are creating again a AddMovie object..

so if you modifiy the coda as following, it will work...

Example:

    public class AddMovie extends JTextField {

    private static final long serialVersionUID = 6180900736631578119L;
    private JFrame frame;
    private JLabel description;
    private JTextArea movieDescription;

    public JPanel createContentPane() {

    JPanel totalGUI = new JPanel();
    
    totalGUI.setBackground(Color.WHITE);

    description = new JLabel("Description  ");
    description.setLocation(15, 285);
    description.setSize(120, 25);
    description.setFont(new java.awt.Font("Tahoma", 0, 12));
    movieDescription = new JTextArea();
    movieDescription.setLocation(15, 320);
    movieDescription.setSize(420, 110);

    JScrollPane scrollPane = new JScrollPane(movieDescription);
    totalGUI.add(description);
    totalGUI.add(movieDescription);
    totalGUI.add(scrollPane);

    return totalGUI;
    }

    public void createAndShowGUI() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    frame = new JFrame("New Movie");
    frame.setContentPane(createContentPane());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(515, 520);
    frame.setLocation(480, 120);
    frame.setVisible(true);
    }

    public static void main(String[] args) {
    AddMovie am = new AddMovie();
    am.createAndShowGUI();
    }

}
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Not your down-voter, but I can't endorse a `null` layout in this context. See also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Jul 09 '16 at 14:54
  • I used your code, but I still getting the same output –  Jul 09 '16 at 18:01