Search code examples
javaswinglayout-managerborder-layoutflowlayout

Any JPanel restrictions in Java?


In the code below, when a JPanel is not added to the JFrame extention, the programme works fine but then, I commented out the JPanel and used the GridLayout, everything is properly and nicely placed on the JFrame. Could it be some restrictions to how JPanel display objects? In the code I have commented out the following statements:

  this.setLayout(new GridLayout(3,2));
   this.add(nameLabel);
   this.add(name);
   this.add(urlLabel);
   this.add(url);
   this.add(typeLabel);
   this.add(type);

and the programme is displayed wrongly but if the statements above are uncommented and the one below commented:

   //add some panes..
    JPanel panel = new JPanel();
    panel.add(nameLabel);
    panel.add(name);
    panel.add(urlLabel);
    panel.add(url);
    panel.add(typeLabel);
    panel.add(type);
    this.add(panel);

, then the programme works fine.. Below is the full code excerpt.

[CODE]

public class FeedInfo extends JFrame {
  private JLabel nameLabel = new JLabel("Name:", SwingConstants.RIGHT);
  private JTextField name;
  private JLabel urlLabel = new JLabel("URL",SwingConstants.RIGHT);
  private JTextField url;
  private JLabel typeLabel = new JLabel("Type",SwingConstants.RIGHT);
  private JTextField type;


   public FeedInfo()
      {
         super("Feed Information");
         this.setSize(400,105);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         String response1 = JOptionPane.showInputDialog(null,"Enter the site name:");
         name = new JTextField(response1,20);
         String response2 = JOptionPane.showInputDialog(null,"Enter the site address:");
         url = new JTextField(response2, 20);

         String[] choices ={"Personal","Commercial","Unkown"};
         int response3 = JOptionPane.showOptionDialog(null, "what type of site is it?",
           "site Type",0, JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);

        type = new JTextField(choices[response3],20);
        //add some panes..
        JPanel panel = new JPanel();
        panel.add(nameLabel);
        panel.add(name);
        panel.add(urlLabel);
        panel.add(url);
        panel.add(typeLabel);
        panel.add(type);
        this.add(panel);


        /*this.setLayout(new GridLayout(3,2));
       this.add(nameLabel);
       this.add(name);
       this.add(urlLabel);
       this.add(url);
       this.add(typeLabel);
       this.add(type);
       */

       this.setVisible(true);

    }
[/CODE]

Solution

  • JPanel panel = new JPanel();
            panel.add(nameLabel);
            panel.add(name);
            panel.add(urlLabel);
            panel.add(url);
            panel.add(typeLabel);
            panel.add(type);
            this.add(panel);
    

    When you are doing this, you are adding the JPanel onto the JFrame, but the JFrame's layout isn't changed which means it has BorderLayout by default. Try change the last line to: add(panel, BorderLayout.CENTER); You can read more about JFrame here

    Also, a JPanel has FlowLayout as default, which means objects will be added in a "flowing line" You should set the layout on your JPanel to get desired result