0

I'm trying to create a simple JFrame with some scomponents like JTextField and JButton The below code is not showing any error,

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

public class Commform 
{
    static final JFrame common = new JFrame();
    JTextField Farmername = new JTextField();
    Commform()
    {
        common.getContentPane().setLayout(null);  
        Farmername.setBounds(100, 100, 285, 100);      
    }

}

But, after declaring the object for JFrame (common) and JTextField (Farmername), if I try to use setBounds method before the constructor like the bolded line,

public class Commform 
{
    static final JFrame common = new JFrame();
    JTextField Farmername = new JTextField();

    Farmername.setBounds(100, 100, 285, 100);

    Commform()
    {
        common.getContentPane().setLayout(null);    

    }
}

then netbeans underlines in red color and shows the tooltip as "package Farmername does not exist, illegal start or expression" When I am able to use object inside constructor, why can't I use just after creating the object ?

And even using the Farmername inside static, as shown below doesn't give any error.

static
    {        
        System.out.println("Called from main function");
        common.getContentPane().setLayout(null);
        Farmername.setBounds(100, 100, 285, 100);
    }

I'm creating this form in the class without main function because I would like to call this frame and enter credentials to insert them to database, later to display particular person's credentials in their respective text field, I want to call this same frame. But at any level I don't want to use main function to this class.

Please help me.

learner
  • 276
  • 1
  • 3
  • 16
  • 1
    Is this a typo `Formername`vs `**Farmername` ? – PeterMmm Jan 15 '14 at 10:52
  • 1
    You're mixing a method invocation with field declarations. See [*Initializing Fields*](http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html), learn [layouts](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) and use conventional names. – trashgod Jan 15 '14 at 11:03
  • Sorry @PeterMmm I was intended to bold that line. And its Farmername not Formername. – learner Jan 15 '14 at 12:01
  • Thank you very much @trashgod [Initializing Fields](http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html) is the perfect answer I was searching. – learner Jan 15 '14 at 12:12
  • Off-topic: should avoid using `null` layout and use a suitable [LayoutManager](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). Also see [this topic](http://stackoverflow.com/questions/12529200/non-resizable-window-border-and-positioning/12532237#12532237) about using `setBounds()`. – dic19 Jan 15 '14 at 12:36

1 Answers1

0

This

Farmername.setBounds(100, 100, 285, 100);

needs to be inside of a block, whether it be a method or a constructor. Whenever you're trying to invoke a change to property via a method, it can't occur in the data field region of the code. It needs to be withing a block.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720