Quick Swing Question

I went off and started messing around and it seems that I messed up somehow. I’m attempting to place a JTextArea onto a JPanel as I usually do but it’s not showing up; does anyone see where I messed up?


import javax.swing.*;
import java.awt.*;

class Window extends JFrame
{
	private Insets insets;
	
	public Window()
	{
		JFrame mainFrame = new JFrame();
		mainFrame.setSize(1024, 786);
		mainFrame.setLocationRelativeTo(null); //Centers the window on the users screen.
		mainFrame.setTitle("Temp Name");
		mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
		mainFrame.setResizable(false);
		insets = mainFrame.getInsets();
		mainFrame.setLayout(null); //Makes it so you can manually positon everything on the JPanel using XY coords.
		JPanel mainPanel = new JPanel(); //Creates a new JPanel to put everything on.
		mainFrame.getContentPane().add(mainPanel);
		
		TextArea gameTextArea = new TextArea();
		gameTextArea.setEditable(false);
		gameTextArea.setBounds(50, 30, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
		mainPanel.add(gameTextArea);
		
		mainFrame.setVisible(true);
	}
}

Example: http://i.imgur.com/RSB3HkV.png

I’ve probably just forgotten a statement or something but I’m not sure.

You set the layout of the JFrame to null, when you probably meant to set the content pane’s layout to null. This works:


public Window() {
      JFrame mainFrame = new JFrame();
      mainFrame.setSize(1024, 786);
      mainFrame.setLocationRelativeTo(null); //Centers the window on the users screen.
      mainFrame.setTitle("Temp Name");
      mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
      mainFrame.setResizable(false);
      insets = mainFrame.getInsets();
      mainFrame.getContentPane().setLayout(null);
      
      JTextArea gameTextArea = new JTextArea();
      gameTextArea.setEditable(false);
      gameTextArea.setBounds(50, 30, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
      mainFrame.getContentPane().add(gameTextArea);
      
      mainFrame.setVisible(true);
}

Standard Swing-fanatic disclaimer: It’s really preferable to use a LayoutManager rather than null layout, as then your UI could be designed to allow the window to be resizable and still look nice, not to mention handling different LookAndFeels having different widget sizes, the user’s desktop settings, etc.

I managed to get it working.

If you create a JPanel and add it to the Window before adding it’s components, it might not go smooth (I don’t know).

The issue here was that you were calling ‘mainFrame.setLayout(null)’, I changed that to ‘mainPanel.setLayout(null)’.


   public Window()
   {
      JFrame mainFrame = new JFrame();
      mainFrame.setSize(1024, 786);
      mainFrame.setLocationRelativeTo(null); //Centers the window on the users screen.
      mainFrame.setTitle("Temp Name");
      mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
      mainFrame.setResizable(false);
      insets = mainFrame.getInsets();

      JPanel mainPanel = new JPanel(); //Creates a new JPanel to put everything on.
      mainPanel.setLayout(null);

      
      TextArea gameTextArea = new TextArea();
      gameTextArea.setEditable(false);
      gameTextArea.setBounds(50, 30, 100, 20);
      mainPanel.add(gameTextArea);
    
      mainFrame.add(mainPanel);
      mainFrame.setVisible(true);
   }

Cheers mate.

EDIT: I was literally 1 second too late :P, either will work.

Thanks, it’s all working now. ^.^