0

I have a question. After adding several objects to my JFrame , when I execute the app I need to resize the form's dimensions in order to see everything. I did some research and I have understood that I need to insert this line of code somewhere ( But I don't know where !!)

revalidate(); 

OR

repaint();

I have tried adding these everwhere but i get syntax error....Help ?

package test_area;

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


public class Test_area {

   // private JFrame f;
   // private JPanel p;
    //private JButton b1;
    private JLabel lab;

    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;

    public Test_area()
    {
        gui();

    }


    public void gui()
    {

      JFrame  f = new JFrame("Ma JFrame");
        f.setVisible(true);
        f.setSize(600,400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel p = new JPanel(new GridBagLayout());
        p.setBackground(Color.CYAN);
        //p.setSize(450,250);

       JButton b1 = new JButton("Login");
       JButton b2 = new JButton("Exit_ ");
       JLabel l1 = new JLabel("Username");
       JLabel l2 = new JLabel("Password");
       JLabel validate = new JLabel("Not Validated Yet");
       TextField textField = new TextField(20);
       JPasswordField pwField = new JPasswordField(15);


        GridBagConstraints c = new GridBagConstraints();

        c.insets = new Insets(5,5,5,5);

        c.gridx=0;
        c.gridy=1;

        p.add(l1,c);

        c.gridx = 1;
        c.gridy = 1;
        p.add(textField,c);

        c.gridx = 0;
        c.gridy = 2;
        p.add(l2,c);

        c.gridx= 1;
        c.gridy=2;
        p.add(pwField,c);

        c.gridx=2;
        c.gridy=1;
        p.add(b1,c);


        c.gridx=2;
        c.gridy=2;

        p.add(b2,c);


        c.gridx=3;
        c.gridy=1;
        p.add(validate,c);

        f.add(p,BorderLayout.NORTH);



//        JButton button;
//        JPanel p = new JPanel();
//        p.setLayout(new GridBagLayout());
//        GridBagConstraints c = new GridBagConstraints();
//        
//        if(shouldFill)
//        {
//         c.fill = GridBagConstraints.HORIZONTAL;
//        }
//        
//        button = new JButton("Button1");
//        
//        if(shouldWeightX)
//        {
//            c.weightx = 0.5;
//        }
//        





    }


    public static void main(String[] args) {

       new Test_area();


    }

}
ExtremeSwat
  • 794
  • 1
  • 12
  • 34
  • Have a look at http://www.oracle.com/technetwork/java/painting-140037.html#triggers, http://stackoverflow.com/questions/1097366/java-swing-revalidate-vs-repaint and http://stackoverflow.com/questions/8853397/repaint-in-java – strauberry Apr 26 '14 at 13:24

2 Answers2

2

Using your code, I cannot find any Syntax errors (the code compiles). You should use pack to use the minimal size for your JFrame. So simply add an f.pack(); after you added all your components.

nils
  • 1,362
  • 1
  • 8
  • 15
  • Thanks a lot. Also I have this huge space because I want to make a login, so if I successfully enter the user and password then the secret JPanel becomes visible. :D Thanks a lot, I've been really troubled by this. Also I did manage to make it work using your pack() or f.repaint() and f.revalidate() – ExtremeSwat Apr 26 '14 at 13:29
1

As user3575404 states, yes, call pack() but most important, call setVisible(true) after adding all components to your GUI. You're now calling it first, and so what happens is this -- the GUI is painted, and then you add components which remain invisible, so the result should not surprise you.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373