Results 1 to 13 of 13
- 04-29-2012, 05:56 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
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.
- 04-29-2012, 07:00 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Need help with this minor problem.
Care to tell us the error, or shall we guess? And please post you code here (use the code tags)I have this one error,
- 04-29-2012, 07:05 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Re: Need help with this minor problem.
I get this error saying: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(); } }
password.java:140: error: variable field might not have been initialized
String display = field.getText();
^
1 error
- 04-29-2012, 07:30 PM #4
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?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-29-2012, 07:33 PM #5
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-29-2012, 07:34 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
-
Re: Need help with this minor problem.
- 04-29-2012, 07:48 PM #8
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
-
Re: Need help with this minor problem.
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:
Has nothing to do with the one that is being displayed.Java Code:JTextField field; String display = field.getText(); field.setText("1");
Solution: make it a private class field and read up on variable scope.
- 04-29-2012, 11:41 PM #10
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
-
Re: Need help with this minor problem.
- 04-30-2012, 06:52 PM #12
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Re: Need help with this minor problem.
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.
-
Re: Need help with this minor problem.
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
-
Minor AWT drawing issue
By snaquetime in forum New To JavaReplies: 6Last Post: 06-02-2011, 12:01 AM -
Some minor doubts about JAVA
By naveenpaul1987 in forum New To JavaReplies: 9Last Post: 03-16-2011, 11:53 AM -
Minor of a matrix (LinkedListArray)
By sehudson in forum New To JavaReplies: 1Last Post: 03-08-2011, 07:27 AM -
Minor of a matrix
By sehudson in forum New To JavaReplies: 3Last Post: 02-21-2011, 08:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks