2

I am trying to display a JPanel in a JFrame. The Jrame works but I can't get the JPanel to display.

My whole class spent an hour on this today. Teacher included. Not luck. Eclipse says there are no errors Can someone please alert me to my mistake?

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

// make a JFrame and bits

public class MySystemGUI extends JFrame implements ActionListener
{
    private static final long serialVersionUID = 1L;
    private JFrame myFrame;
    private JTextField LLName, LLAddress, LLPhone, LLbankDeets;
    private JButton sub1;
    private JLabel LLNameT, LLAddressT, LLPhoneT, LLbankDeetsT;

    private JPanel LLJP()
    {
        JPanel JP1 = new JPanel();

        LLNameT = new JLabel ("Enter Landlord name");
        LLName = new JTextField(30);
        LLAddressT = new JLabel ("Enter Landlord Address ");
        LLAddress = new JTextField(40);
        LLPhoneT = new JLabel ("Enter Landlod Phone No.");
        LLPhone = new JTextField(10);
        LLbankDeetsT = new JLabel ("Enter Landlod Bank details");
        LLbankDeets = new JTextField(10);
        sub1 = new JButton("Submit"); 

        JP1.add(LLNameT);
        JP1.add(LLName); 
        JP1.add(LLAddressT);
        JP1.add(LLAddress); 
        JP1.add(LLPhoneT);
        JP1.add(LLPhone); 
        JP1.add(LLbankDeetsT );
        JP1.add(LLbankDeets); 
        JP1.add(sub1);

        //myFrame.add(JP1 );

        return JP1;
    }

    // Set up frame 

    public MySystemGUI()
    {   
        myFrame = new JFrame ();
        JPanel myPanel = LLJP(); 
        myFrame.add(myPanel,"South");
        this.setLayout(new GridBagLayout());
        this.setSize(700, 500);
        this.setTitle("My System GUI");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBackground(Color.blue);  
    }

    //run this bitch
    public static void main (String[] args)
    { 
        new MySystemGUI();
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {

    }
}
Akshay Soam
  • 1,580
  • 3
  • 21
  • 39

2 Answers2

4

You have an JFrame member

private JFrame myFrame;

which is what you are adding the components to.

myFrame.add(myPanel,"South");

But you setVisible to class frame

public class MySystemGUI extends JFrame
   ...
   this.setVisible(true);   // `this` is class frame, not `myFrame`

Take out all the this.setXxx and do myFrame.setXxx, and take out the extends JFrame

public class MySystemGUI implements ActionListener {
    ...
    public MySystemGUI() {
       ...
       myFrame.setLayout(new GridBagLayout());
       myFrame.setSize(700, 500);
       myFrame.setTitle("My System GUI");
       myFrame.setVisible(true);
       ...
    }
}

Other Notes

  • myFrame.add(myPanel,"South"); - "South" doesn't matter, only for BorderLayout. You set the layout to GridBaglayout.

  • this.setBackground(Color.blue); Wouldn't you want o set the background after the frame is visible?

  • Swing Programs should be run/started on the Event Dispatch Thread. See Initial Threads. Basically, you can wrap the instantiation in a SwingUtilities.invokeLater

    public static void main (String[] args) { 
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new MySystemGUI();
            }
        });
    }
    

UPDATE

You're also going to want to make use of different layout managers for the panel holding the labels and field. With a FlowLayout (the default of JPanel), eveerytyhing is added consecutively in a row. Take some time to learn the layout managers at Laying out Components Withing a Container

Here is a simple fix using a GridLayout

import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class MySystemGUI implements ActionListener {

    private static final long serialVersionUID = 1L;
    private JFrame myFrame;
    private JTextField LLName, LLAddress, LLPhone, LLbankDeets;
    private JButton sub1;
    private JLabel LLNameT, LLAddressT, LLPhoneT, LLbankDeetsT;

    private JPanel LLJP() {

        JPanel JP1 = new JPanel(new GridLayout(0, 2));

        LLNameT = new JLabel("Enter Landlord name");
        LLName = new JTextField(30);
        LLAddressT = new JLabel("Enter Landlord Address ");
        LLAddress = new JTextField(40);
        LLPhoneT = new JLabel("Enter Landlod Phone No.");
        LLPhone = new JTextField(10);
        LLbankDeetsT = new JLabel("Enter Landlod Bank details");
        LLbankDeets = new JTextField(10);
        sub1 = new JButton("Submit");

        JP1.add(LLNameT);
        JP1.add(LLName);
        JP1.add(LLAddressT);
        JP1.add(LLAddress);
        JP1.add(LLPhoneT);
        JP1.add(LLPhone);
        JP1.add(LLbankDeetsT);
        JP1.add(LLbankDeets);
        JP1.add(sub1);

        return JP1;

    }

    public MySystemGUI() {
        myFrame = new JFrame();
        JPanel myPanel = LLJP();
        myFrame.add(myPanel);
        myFrame.setTitle("My System GUI");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setBackground(Color.blue);
        myFrame.pack();
        myFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MySystemGUI();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e){}
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
2

You have a local frame hidding your class which is actually a JFrame:

public class MySystemGUI extends JFrame implements ActionListener {   // class signature
    ...
    public MySystemGUI() {
        ... 
        myFrame = new JFrame (); // local variable
        ...
        myFrame.add(myPanel,"South");
        ...
    }
    ...
}

Your panel is added to this local frame instead to your class so it won't be visible. You can either get rid of this local variable and add panel using this.add(...) or avoid extending JFrame and use local variables instead. This last apporach is preferred:

My whole class spent an hour on this today. Teacher included. Not luck.

Just find another teacher.

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • 1
    I definitely need a new teacher. I have learned much more from all of you that I have at school. still haven't got it working. I will have to get back to you all. Thanks again. Lots of things to learn. Yay. – Forgive me I'm a drummer. Oct 21 '14 at 12:04
  • You are welcome. Don't worry, I've been there too: I've learned more about Java and Swing in 1 month visiting SO than in 1 year in college. Please don't forget to [accept](http://stackoverflow.com/help/accepted-answer) peeskillet's answer :) – dic19 Oct 21 '14 at 12:10
  • 1
    Hey thanks again. I used local JFrames in the end with GridLayout. I have built the greatest GUI ever! – Forgive me I'm a drummer. Oct 22 '14 at 15:18
  • Glad to hear that! :) And kudos for writing your GUI instead of using GUI builders: you'll learn a lot and won't suffer because of builders' limitations. @user3545757 – dic19 Oct 22 '14 at 15:27