Java Container not showing

Alright! So my issue is, the container (cont) is not showing when the program is ran. I tried directly adding all the things that are in the containers to pangui2, but that failed to work, and yieled the same result of it running and only showing the JTextArea. I appreciate the help.

package guiprojj;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.*;

import com.eclipsesource.json.JsonObject;
import com.google.gson.JsonParser;
import com.json.parsers.JSONParser;
import com.json.parsers.JsonParserFactory;


public class gui {
    public static void main(String[] args)
    {
        JFrame maingui = new JFrame("Gui");
        JButton enter = new JButton("Enter");
        final JTextArea movieinfo = new JTextArea(5,20);
        final JTextField movietext = new JTextField(16);
        final JScrollPane scrolll = new JScrollPane(movieinfo);
        final JLabel titlee = new JLabel("Enter movie name here:");
        final Container cont = new Container();
        JPanel pangui = new JPanel();
        JPanel pangui2 = new JPanel();
        maingui.add(pangui2);
        maingui.add(pangui);
        maingui.setResizable(false);
        maingui.setVisible(true);
        pangui.add(scrolll);
        pangui2.add(cont);
        cont.add(titlee);
        cont.add(movietext);
        cont.add(enter);
        movieinfo.setLineWrap(true);
        movieinfo.setWrapStyleWord(true);
        movieinfo.setEditable(false);
        scrolll.getPreferredSize();
        maingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        maingui.pack();
        enter.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)  
            {
                System.out.println(Test.getMovieInfo(movietext.getText()));
                 JsonParserFactory factory=JsonParserFactory.getInstance();
                 JSONParser parser=factory.newJsonParser();
                 Map jsonData=parser.parseJson(Test.getMovieInfo(movietext.getText()));
                 String Title = (String)jsonData.get("Title");
                 String Year = (String)jsonData.get("Year");
                 String Plot = (String)jsonData.get("Plot");
                 movieinfo.setText("Title: "+Title+"nYear: "+ Year +"nPlot: "+Plot);
            }
            });

        }
}

After trying your solution I get nothing, rather than just the TextArea.

package guiprojj;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.*;

import com.eclipsesource.json.JsonObject;
import com.google.gson.JsonParser;
import com.json.parsers.JSONParser;
import com.json.parsers.JsonParserFactory;


public class gui {
    final static String SCROLL = "Card with JButtons";
    final static String BOX = "Card with JTextField";
    public static void main(String[] args)
    {
        JFrame maingui = new JFrame("Gui");
        JButton enter = new JButton("Enter");
        final JTextArea movieinfo = new JTextArea(5,20);
        final JTextField movietext = new JTextField(16);
        final JScrollPane scrolll = new JScrollPane(movieinfo);
        final JLabel titlee = new JLabel("Enter movie name here:");
        final Container cont = new Container();
        JPanel cards = new JPanel(new CardLayout());
        JPanel pangui = new JPanel();
        JPanel pangui2 = new JPanel();
        maingui.add(cards);
        cards.add(pangui2, SCROLL);
        cards.add(pangui2, BOX);
        maingui.setResizable(false);
        maingui.setVisible(true);
        pangui.add(scrolll);
        pangui2.add(cont);
        cont.add(titlee);
        cont.add(movietext);
        cont.add(enter);
        movieinfo.setLineWrap(true);
        movieinfo.setWrapStyleWord(true);
        movieinfo.setEditable(false);
        scrolll.getPreferredSize();
        maingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        maingui.pack();
        enter.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)  
            {
                System.out.println(Test.getMovieInfo(movietext.getText()));
                 JsonParserFactory factory=JsonParserFactory.getInstance();
                 JSONParser parser=factory.newJsonParser();
                 Map jsonData=parser.parseJson(Test.getMovieInfo(movietext.getText()));
                 String Title = (String)jsonData.get("Title");
                 String Year = (String)jsonData.get("Year");
                 String Plot = (String)jsonData.get("Plot");
                 movieinfo.setText("Title: "+Title+"nYear: "+ Year +"nPlot: "+Plot);
            }
            });

        }
}

Answer

Your JFrame uses BorderLayout by default and components added in a default fashion to a BorderLayout-using container go to the BorderLayout.CENTER position. This means that the component you add 2nd to the JFrame covers up that which was added first:

maingui.add(pangui2);
maingui.add(pangui); // this will cover pangui2

Consider giving the JFrame’s contentPane a different layout such as a GridLayout.

  • As an aside: why use AWT Container here rather than a Swing JPanel?
  • Aside #2: I’m not sure what this line is trying to achieve: scrolll.getPreferredSize();
  • Aside #3: Are you using the static main method just to demonstrate your current problem? Or is the code above your current “production” code? If the latter, then you will want to get most of that code out of the main method and into constructors and instance methods. The main method should concern itself only with setting up the program and starting it, and that’s about it.

Edit
You state:

I don’t want to swap anything, I literally just want it to have a label above a textField, which is above a text area.

Then a CardLayout is not what you want to use as it is for swapping and swapping only (as the tut’s will tell you). For a JLabel above a JTextField, consider using a JPanel with a BorderLayout with the JLabel in the BorderLayout.NORTH position and the JTextField in the BorderLayout.CENTER position. Then create another JPanel with a BorderLayout, and placing the JPanel above into the new JPanel’s BorderLayout.NORTH position and the JScrollPane that holds the JTextArea in the BorderLayout.CENTER position.

But most important, you should read the tutorials, as they’ll tell you and show you how to use the layouts. Guessing never works with this.

Leave a Reply

Your email address will not be published. Required fields are marked *