Results 1 to 13 of 13
  1. #1
    juxta is offline Member
    Join Date
    Nov 2011
    Posts
    30
    Rep Power
    0

    Default Need help with this minor problem.

    Hi guys, hope you are well. I have a question I would like to ask about my GUI java program. Basically, I want my JButtons that inputs numbers and displays them on to a JTextField. However, I have this one error, and I have spent 3 hours on this, and can't figure it out. I have declared it in different ways and I still get that error.
    Heres my code: testing - Pastebin.com

    Anyone know why? Am I missing something??
    Thanks.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Need help with this minor problem.

    I have this one error,
    Care to tell us the error, or shall we guess? And please post you code here (use the code tags)

  3. #3
    juxta is offline Member
    Join Date
    Nov 2011
    Posts
    30
    Rep Power
    0

    Default Re: Need help with this minor problem.

    Java Code:
    	import java.awt.*;
    	import java.awt.AWTEvent;
    	import java.awt.event.ActionEvent;
    	import java.awt.event.ActionListener;
    	import javax.swing.*;
    	import javax.swing.border.*;
    
    	class Password extends JFrame 
    	{
    	Password()
    	{
    		super("testing");
    
    		buildGUI();
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		pack();
    		setVisible(true);
    		
    		setSize(750,350);
    		
    		
    	}
    	
    	private void buildGUI()
    	{
    
    	
    	//--------------------north----------------------//
    		JPanel north = new JPanel();
    		setLayout(new BorderLayout());
    		north.setLayout(new BorderLayout());
    		add(north,BorderLayout.NORTH);
    		north.add(new JTextArea());
    	
    	//--------------------top----------------------//
    		JPanel top = new JPanel();
    		setLayout(new BorderLayout());
    		top.setLayout(new BorderLayout());
    		top.setBorder(new TitledBorder(new EtchedBorder(), 
    		"Instruction"));
    		add(top,BorderLayout.WEST);
    		top.add(new JTextArea("Welcome", 10,5), BorderLayout.NORTH);
    		
    		
    		
    		//-----------------Right Panel---------------------//
    		JPanel right = new JPanel();
    
    		GridLayout rightGrid = new GridLayout(4,3);
    		
    		//--------------------User output-------------------//
    		JPanel output = new JPanel();
    		GridLayout out = new GridLayout(0,1);
    		output.setLayout(new BorderLayout());
    		JTextField field = new JTextField(20);
    		field.setPreferredSize(new Dimension(170, 30));
    		field.setBackground(Color.yellow);
    		field.addActionListener(new ListenToOne());
    		output.add(field);
    		
    		right.add(output);	
    		add(right);
    		
    
    		
    		
    		
    		//--------------------Keypad-------------------//
    		JPanel passbtns = new JPanel();
    
    		passbtns.setLayout(rightGrid);
    		JButton one = new JButton ("1");
    		one.setPreferredSize(new Dimension(40, 40));
    		passbtns.add(one);
    		one.addActionListener(new ListenToOne());
    		
    		
    		JButton two = new JButton ("2");
    		passbtns.add(two);
    		
    		JButton three = new JButton ("3");
    		passbtns.add(three);
    		
    		JButton four = new JButton ("4");
    		passbtns.add(four);
    		
    		JButton five = new JButton ("5");
    		passbtns.add(five);
    		
    		JButton six = new JButton ("6");
    		passbtns.add(six);
    		
    		JButton seven = new JButton ("7");
    		passbtns.add(seven);
    		
    		JButton eight = new JButton ("8");
    		passbtns.add(eight);
    		
    		JButton nine = new JButton ("9");
    		passbtns.add(nine);
    		
    		JButton clear = new JButton ("Clear");
    		passbtns.add(clear);
    		
    		JButton zero = new JButton ("0");
    		passbtns.add(zero);
    		
    		JButton enter = new JButton ("Enter");
    		passbtns.add(enter);
    
    		passbtns.setBorder(new TitledBorder(new EtchedBorder(), "Password Input"));
    		right.add(passbtns);	
    		add(right);
    		
    		
    	
    		//-------------------------------------------
    		JPanel armbtns = new JPanel();
    		GridLayout armgrid = new GridLayout(2,2);
    		
    		armbtns.setLayout(armgrid);
    		
    		JButton save = new JButton ("Save Password");
    		armbtns.add(save);
    
    		
    		JButton change = new JButton ("Change Password");
    		armbtns.add(change);
    		
    		right.add(armbtns);	
    		add(right);
    
    		}
    		
    		
    		class ListenToOne implements ActionListener {
    		
            public void actionPerformed(ActionEvent e) {
    		 JTextField field;
               String display = field.getText();
                field.setText("1");
            }
        }
    	
    	
    	class QuitListener implements ActionListener
    		{
    		public void actionPerformed(ActionEvent e)
    		{
    		System.exit(0);
    		}
    		}
    
    		
    	public static void main(String[] args)
    	{
    		Password f = new Password();
    	}
    }
    I get this error saying:
    password.java:140: error: variable field might not have been initialized
    String display = field.getText();
    ^
    1 error

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Need help with this minor problem.

    No 'might not' about it. Where did you think you initialized the variable before you tried to call getText() in line 140?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Need help with this minor problem.

    Moreover, what would you gain by querying the text of a JTextField that isn't the one shown in your GUI? That's the one you need to have a reference to, either by declaring it as an instance field or by obtaining it from teh source of the ActionEvent.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    juxta is offline Member
    Join Date
    Nov 2011
    Posts
    30
    Rep Power
    0

    Default Re: Need help with this minor problem.

    Quote Originally Posted by DarrylBurke View Post
    No 'might not' about it. Where did you think you initialized the variable before you tried to call getText() in line 140?

    db
    Hi, perhaps Line 55?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with this minor problem.

    Quote Originally Posted by juxta View Post
    Hi, perhaps Line 55?
    Is line 55 initializing the field class variable or some local variable that has been declared in the buildGUI() method and is thus local to only that method?

  8. #8
    juxta is offline Member
    Join Date
    Nov 2011
    Posts
    30
    Rep Power
    0

    Default Re: Need help with this minor problem.

    Quote Originally Posted by Fubarable View Post
    Is line 55 initializing the field class variable or some local variable that has been declared in the buildGUI() method and is thus local to only that method?
    hmm, I think it is only declared inside the buildGUI method and I think it is local.

    Would I need to declare the JTexTField as public or private???

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with this minor problem.

    Quote Originally Posted by juxta View Post
    hmm, I think it is only declared inside the buildGUI method and I think it is local.

    Would I need to declare the JTexTField as public or private???
    This has nothing to do with Swing. If you want to use an object in multiple methods of a class, it *has* to be declared in the class. Either that or passed around in parameters -- but you don't want to do that here.

    This "field" local variable declared but not initialized here:
    Java Code:
             JTextField field;
               String display = field.getText();
                field.setText("1");
    Has nothing to do with the one that is being displayed.

    Solution: make it a private class field and read up on variable scope.

  10. #10
    juxta is offline Member
    Join Date
    Nov 2011
    Posts
    30
    Rep Power
    0

    Default Re: Need help with this minor problem.

    Quote Originally Posted by Fubarable View Post
    This has nothing to do with Swing. If you want to use an object in multiple methods of a class, it *has* to be declared in the class. Either that or passed around in parameters -- but you don't want to do that here.

    This "field" local variable declared but not initialized here:
    Java Code:
             JTextField field;
               String display = field.getText();
                field.setText("1");
    Has nothing to do with the one that is being displayed.

    Solution: make it a private class field and read up on variable scope.
    I tried to do something like that, but I can't seem to get it to work

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with this minor problem.

    Quote Originally Posted by juxta View Post
    I tried to do something like that, but I can't seem to get it to work
    Can't really help you unless you show and tell us more. We have no idea what you tried nor how it didn't work.

  12. #12
    juxta is offline Member
    Join Date
    Nov 2011
    Posts
    30
    Rep Power
    0

    Default Re: Need help with this minor problem.

    Quote Originally Posted by Fubarable View Post
    Can't really help you unless you show and tell us more. We have no idea what you tried nor how it didn't work.
    I tried making a private class for Field like

    private class field {
    JTextField field = new JTextField(20);
    }

    but it still get the same error. I put private for JTextField field inside the ActionListener class, but still get the same problem. I've seen working examples on the internet, but its quite different to the layout of my code.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with this minor problem.

    Quote Originally Posted by juxta View Post
    I tried making a private class for Field like

    private class field {
    JTextField field = new JTextField(20);
    }

    but it still get the same error. I put private for JTextField field inside the ActionListener class, but still get the same problem. I've seen working examples on the internet, but its quite different to the layout of my code.
    If you're seeing examples like that in a tutorial, then discard that tutorial, for it's worthless and that's not how you make a private class field. Simply declare and initialize a JTextField variable in the Password class before the constructor, that's it:

    Java Code:
        import java.awt.*;
        import java.awt.AWTEvent;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        import javax.swing.*;
        import javax.swing.border.*;
     
        class Password extends JFrame 
        {
           private JTextField fooField = new JTextField(10);
    
           public Password() {
               // .....
           }
    
           private void buildGUI() {
               // .....
    
               // here you can add the fooField JTextField created above into the GUI
           }

Similar Threads

  1. Minor AWT drawing issue
    By snaquetime in forum New To Java
    Replies: 6
    Last Post: 06-02-2011, 01:01 AM
  2. Some minor doubts about JAVA
    By naveenpaul1987 in forum New To Java
    Replies: 9
    Last Post: 03-16-2011, 12:53 PM
  3. Minor of a matrix (LinkedListArray)
    By sehudson in forum New To Java
    Replies: 1
    Last Post: 03-08-2011, 08:27 AM
  4. Minor of a matrix
    By sehudson in forum New To Java
    Replies: 3
    Last Post: 02-21-2011, 09:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •