0

I am studying Java AWT to create GUI applications. I am working on the below code where I cannot make the panel visible inside the frame. Here is my code:

        import java.awt.*;
        import java.awt.event.*;

        /**
         *
         * @author kiran
         */
        public class UserInterface
        {
            Frame UI;
            private static double UIWidth, UIHeight;



            /**
             * Constructs User Interface
             */
            public UserInterface()
            {
                UI = new Frame("frame");
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                UIWidth = screenSize.getWidth();
                UIHeight = screenSize.getHeight();
                buildFrame();
                buildMessageInputArea();
            }
            /**
             * returns the width of the UI
             * @return returns the width of the UI
             */
            public static double getUIWidth()
            {
                return UIWidth;
            }

            /**
             * returns the width of the UI
             * @return returns the width of the UI
             */
            public static double getUIHeight()
            {
                return UIHeight;
            }

            /**
             * Builds the frame
             */
            private void buildFrame()
            {
                UI.setSize((int)UIWidth,(int)UIHeight*96/100);
                UI.setVisible(true);
                UI.setLayout(new FlowLayout());
                UI.addWindowListener(new Actions());
            }

            private void buildMessageInputArea()
            {
                Panel current = new TextAreaPanel().getPanel();
                current.setVisible(true);
                UI.add(current);

            }
        }

        class TextAreaPanel extends Frame
        {
            private Panel textAreaPanel;
            TextArea msgInputArea;

            public TextAreaPanel()
            {
                textAreaPanel = new Panel();
                msgInputArea = new TextArea(1000,(int)UserInterface.getUIWidth() * 80/100);
            }

            private void addTextArea()
            {
                textAreaPanel.add(msgInputArea);
            }

            public Panel getPanel()
            {
                return textAreaPanel;
            }

    }

    class Actions extends WindowAdapter
    {
        @Override
        public void windowClosing(WindowEvent c)
        {
            System.exit(0);
        }
    }

How can I make the panel visible inside the frame?

Kiran C K
  • 61
  • 7
  • 4
    extends Frame or extends JFrame, aaand TextArea or JTextArea, Panel or JPanel, please to use Swing rather than old AWT – mKorbel May 07 '17 at 16:27
  • 2
    Read the section from the Swing tutorial on [How to Use Text Areas](http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html). The `TextDemo` code will show you how to better structure your code. There is no need for all you extended classes or static methods. Start with the working code in the tutorial and make changes.. – camickr May 07 '17 at 17:53
  • 1
    @camickr He is not asking about Swing, He wants to know how to do it with AWT. –  May 07 '17 at 18:52
  • 2
    @theProgrammer101, The logic is the same whether you use AWT or Swing, just the component name changes. My comment was made because the design of the current code is terrible. There is no reason you can't use the structure of the code found in the Swing tutorial. Just because somebody asks as question about something doesn't mean they know what they are talking about. Why did the OP include the "Swing tag" when the question was asked? In any case it for the OP to reply and clarify the question if the advice is not appropriate. – camickr May 07 '17 at 19:01
  • 2
    @theProgrammer101 Is it because the OP doesn't understand the difference between AWT and Swing? Would it be unadvisable to recommend that the OP reconsider their choice of API for something that is at least still been used? Is there a reason the OP has to use AWT? Since Swing sits on top of AWT and the complete lack of AWT tutorials, there's nothing wrong with referencing Swing as feasible source of information – MadProgrammer May 07 '17 at 20:39

1 Answers1

2

How do I make a panel visible inside frame in Java AWT?

There were two fundamental problems with the code as seen, which can be fixed by changing the following:

  1. Add the panel/text area to the GUI before setVisible(true) is called on the top level container.

    While it is possible to add components to a container after it has been made visible, they require special handling, and it is not necessary in this case.

  2. Add the text area to the panel!

Here is the code, turned into a Minimal, Complete, and Verifiable example by adding a main(String[]) method, with those two changes implemented, as well as more explanatory comments on other aspects of the code.

import java.awt.*;
import java.awt.event.*;

public class UserInterface {

    Frame UI;
    private static double UIWidth, UIHeight;

    public static void main(String[] args) {
        Runnable r = () -> {
            new UserInterface();
        };
        EventQueue.invokeLater(r);
    }

    /**
     * Constructs User Interface
     */
    public UserInterface() {
        UI = new Frame("frame");
        // setting a GUI to full screen while accounting for the task
        // bar can be achieved in a single line of code.
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        UIWidth = screenSize.getWidth();
        UIHeight = screenSize.getHeight();
        // these need to be called in the reverse order to ensure the 
        // components are added before the GUI is set visible.
        buildMessageInputArea();
        buildFrame();
    }

    /**
     * returns the width of the UI
     *
     * @return returns the width of the UI
     */
    public static double getUIWidth() {
        return UIWidth;
    }

    /**
     * returns the width of the UI
     *
     * @return returns the width of the UI
     */
    public static double getUIHeight() {
        return UIHeight;
    }

    /**
     * Builds the frame
     */
    private void buildFrame() {
        UI.setSize((int) UIWidth, (int) UIHeight * 96 / 100);
        UI.setVisible(true); 
        UI.setLayout(new FlowLayout());
        UI.addWindowListener(new Actions());
    }

    private void buildMessageInputArea() {
        Panel current = new TextAreaPanel().getPanel();
        current.setVisible(true);
        UI.add(current);
    }
}

// does not need to be a fram
//class TextAreaPanel extends Frame {
class TextAreaPanel {

    private Panel textAreaPanel;
    TextArea msgInputArea;

    public TextAreaPanel() {
        textAreaPanel = new Panel();
        // these number represent columns and rows, not pixels!
        //msgInputArea = new TextArea(1000, (int) UserInterface.getUIWidth() * 80 / 100);
        msgInputArea = new TextArea(40, 60);
        // add the text area to the panel!
        textAreaPanel.add(msgInputArea);
    }

    /** not called by anything else
    private void addTextArea() {
        textAreaPanel.add(msgInputArea);
    }
    **/

    public Panel getPanel() {
        return textAreaPanel;
    }
}

// This can be achieved in a single line of code
class Actions extends WindowAdapter {

    @Override
    public void windowClosing(WindowEvent c) {
        System.exit(0);
    }
}

To add to / expand on the comments of @mKorbel & @camickr:

  1. Why use AWT? See this answer for many good reasons to abandon AWT components in favor of Swing.
  2. See Composition over inheritance.
  3. The use of static in GUIs more commonly causes problems, than fixes them. Most (if not all) of the methods marked as static should be reduced to non-static with the code using an instance of the object to call the method.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433