Results 1 to 20 of 20
- 02-07-2011, 01:41 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
ButtonListener setText not working (no errors)
My ButtonListener, when activated (Enter button pressed), is supposed to
It does absolutely nothing (the button). The rest of the GUI is working fine.Java Code:stupid.setText("1234")
I am using two files, "Risk.java" and "RiskApp.java". If you tell me what I am doing wrong and how to fix it, that would be great.
Risk.java:
RiskApp.java:Java Code:public class Risk extends JPanel { public JButton enter; public JFormattedTextField stupid, yummy; public JLabel smart, dumb; public NumberFormat yucker = NumberFormat.getNumberInstance(); public NumberFormat nucker = NumberFormat.getNumberInstance(); public Risk() { JLabel smart = new JLabel("First Number:"); add(smart); JFormattedTextField stupid = new JFormattedTextField(yucker); stupid.setColumns(3); stupid.setText("0"); add(stupid); boolean isDigit; JLabel dumb = new JLabel("Second Number:"); add(dumb); JFormattedTextField yummy = new JFormattedTextField(nucker); yummy.setColumns(3); yummy.setText("0"); add(yummy); enter = new JButton("Enter"); //enter.addActionListener(new ActionListener()); add(enter); enter.addActionListener(new ButtonListener()); } class ButtonListener implements ActionListener { ButtonListener() { } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("enter")) { [B][I][U][COLOR="Blue"]stupid.setText("1234");[/COLOR][/U][/I][/B] } } } }
There are no errors when I "java RiskApp". The GUI runs, with two textboxes (with the number 0 in them), text, and an enter button. The numberonly thing works fine. But when I push the enter button, nothing happens.Java Code:import javax.swing.*; import java.text.NumberFormat; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Toolkit; public class RiskApp { private static void createAndShowGUI() { JFrame frame = new JFrame("Risk"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Risk riskPanel = new Risk(); frame.getContentPane().add(riskPanel); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); } catch (UnsupportedLookAndFeelException e) { } catch (ClassNotFoundException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } createAndShowGUI(); } }); } }
Thanks for your help,
cc11rocks
-
"enter" and "Enter" are different, right?
- 02-07-2011, 02:29 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
Yes.
Yes. enter is the button name. "Enter" is the string that shows up for the button.
cc11rocks
- 02-07-2011, 02:34 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
Works, New Problem
Holy crap! I changed everything to "Enter" with a capital "E". It gave me a bunch of errors. I took out the setText blah blah blah and inserted "System.out.println("Button Pressed"). When I pressed the button, it displayed the text. Now i was wondering about how to set the text in the textbox and NOT have errors.
Thanks,
cc11rocks
-
- 02-07-2011, 02:51 AM #6
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
Error message
Java Code:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Risk$ButtonListener.actionPerformed(Risk.java:43) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour ce) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)Last edited by cc11rocks; 02-07-2011 at 02:55 AM.
-
So that tells you that something in the actionPerformed must be null, something that you're trying to call a method or access a field, and that can only be one variable -- stupid. So check the stupid carefully -- are you sure that you have initialized it? Are you sure that you've initialized the stupid variable that is in the class? That you haven't re-declared it in the constructor (hint, hint)?
- 02-07-2011, 03:14 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
I declared it in two places:
andJava Code:stupid.setText("0");
I can't do this? If not, how do I get it to work?Java Code:stupid.setText("1234");
-
That's not declaring a variable. Declaring a variable is when you do this:
How many places do you do this? You should only do it once, in the class itself. If you redeclare it in the constructor than you're creating a new stupid variable in the constructor that is distinct from the class variable and that is only visible in the constructor. Even if you initialize it in the constructor, you're not initializing the important variable, the one declared in the class. This error is so common and so important that it has a name: it's called "shadowing" a variable.Java Code:JFormattedTextField stupid
- 02-07-2011, 03:36 AM #10
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
Risk.java now looks like this:
I only wrote the first couple lines because everything is the same. When I remove stupid, I get an (obvious) error.Java Code:public class Risk extends JPanel { public JButton Enter; public JFormattedTextField yummy;
I'm assuming I have to reference it somehow?Java Code:Risk.java:40: cannot find symbol symbol : variable stupid location: class Risk.ButtonListener stupid.setText("1234");
-
Show the new class code please, but please take care to format it more cleanly including use of proper indentations so that we can read it without struggling.
- 02-07-2011, 03:57 AM #12
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
old Risk.java (don't know how to format properly sorry):
new Risk.java (don't know how to format properly sorry):Java Code:import javax.swing.JFormattedTextField; import javax.swing.*; import java.awt.*; import java.text.NumberFormat; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Risk extends JPanel { public JButton Enter; public JFormattedTextField stupid, yummy; public JLabel smart, dumb; public NumberFormat yucker = NumberFormat.getNumberInstance(); public NumberFormat nucker = NumberFormat.getNumberInstance(); public Risk() { JLabel smart = new JLabel("First Number:"); add(smart); JFormattedTextField stupid = new JFormattedTextField(yucker); stupid.setColumns(3); stupid.setText("0"); add(stupid); boolean isDigit; JLabel dumb = new JLabel("Second Number:"); add(dumb); JFormattedTextField yummy = new JFormattedTextField(nucker); yummy.setColumns(3); yummy.setText("0"); add(yummy); Enter = new JButton("Enter"); //enter.addActionListener(new ActionListener()); add(Enter); Enter.addActionListener(new ButtonListener()); } class ButtonListener implements ActionListener { ButtonListener() { } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Enter")) { stupid.setText("1234"); } } } }
I tried putting the "public void actionPerformed(ActionEvent e) {Java Code:import javax.swing.JFormattedTextField; import javax.swing.*; import java.awt.*; import java.text.NumberFormat; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Risk extends JPanel { public JButton Enter; public JFormattedTextField yummy; public JLabel smart, dumb; public NumberFormat yucker = NumberFormat.getNumberInstance(); public NumberFormat nucker = NumberFormat.getNumberInstance(); public Risk() { JLabel smart = new JLabel("First Number:"); add(smart); JFormattedTextField stupid = new JFormattedTextField(yucker); stupid.setColumns(3); stupid.setText("0"); add(stupid); boolean isDigit; JLabel dumb = new JLabel("Second Number:"); add(dumb); JFormattedTextField yummy = new JFormattedTextField(nucker); yummy.setColumns(3); yummy.setText("0"); add(yummy); Enter = new JButton("Enter"); //enter.addActionListener(new ActionListener()); add(Enter); Enter.addActionListener(new ButtonListener()); } class ButtonListener implements ActionListener { ButtonListener() { } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Enter")) { stupid.setText("1234"); } } } }
if..." up at the class but it gave errors and i eventually gave up.
Thanks,
cc11rocks
-
Please edit your post. All your code is left-justified (all lines start in the left column), making your code hard if not impossible to read. I don't think it's asking you too much to make the effort to post well formatted code with standard indentation if you want us to make the effort to help you. Google Java indentation code style will help you learn how to do this. Some hits include:
Code style
look at the 1TBS style here: Indent StyleLast edited by Fubarable; 02-07-2011 at 04:04 AM.
- 02-07-2011, 04:02 AM #14
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
How do I do this?
And I do this how?
-
see links in the edit to my post above, either that or google as noted above.
- 02-07-2011, 04:10 AM #16
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
Like this?
This Better?
Java Code:import javax.swing.JFormattedTextField; import javax.swing.*; import java.awt.*; import java.text.NumberFormat; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Risk extends JPanel { public JButton Enter; public JFormattedTextField stupid, yummy; public JLabel smart, dumb; public NumberFormat yucker = NumberFormat.getNumberInstance(); public NumberFormat nucker = NumberFormat.getNumberInstance(); public Risk() { JLabel smart = new JLabel("First Number:"); add(smart); JFormattedTextField stupid = new JFormattedTextField(yucker); stupid.setColumns(3); stupid.setText("0"); add(stupid); boolean isDigit; JLabel dumb = new JLabel("Second Number:"); add(dumb); JFormattedTextField yummy = new JFormattedTextField(nucker); yummy.setColumns(3); yummy.setText("0"); add(yummy); Enter = new JButton("Enter"); //enter.addActionListener(new ActionListener()); add(Enter); Enter.addActionListener(new ButtonListener()); } class ButtonListener implements ActionListener { ButtonListener() { } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Enter")) { stupid.setText("1234"); } } } }
-
It's closer. You will want to keep all code in the same block lined up. Note the difference:
Java Code:import javax.swing.JFormattedTextField; import javax.swing.*; import java.awt.*; import java.text.NumberFormat; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Risk extends JPanel { public JButton Enter; public JFormattedTextField stupid, yummy; public JLabel smart, dumb; public NumberFormat yucker = NumberFormat.getNumberInstance(); public NumberFormat nucker = NumberFormat.getNumberInstance(); public Risk() { JLabel smart = new JLabel("First Number:"); add(smart); JFormattedTextField stupid = new JFormattedTextField(yucker); stupid.setColumns(3); stupid.setText("0"); add(stupid); boolean isDigit; JLabel dumb = new JLabel("Second Number:"); add(dumb); JFormattedTextField yummy = new JFormattedTextField(nucker); yummy.setColumns(3); yummy.setText("0"); add(yummy); Enter = new JButton("Enter"); // enter.addActionListener(new ActionListener()); add(Enter); Enter.addActionListener(new ButtonListener()); } class ButtonListener implements ActionListener { ButtonListener() { } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Enter")) { stupid.setText("1234"); } } } }
More importantly, is that your latest incarnation of the class? And what error does this code cause and where?
- 02-07-2011, 04:23 AM #18
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
Sorry I'm dumb, but what does incarnation mean? Does it mean "Is that your final class, the one that caused you errors?"
Thanks and sorry,
Dummy
-
Is this the latest version of your class? The one where you've tried to correct the problem that I mentioned above -- declaring a variable twice, once in the class and a second time in the constructor? If so, you're still declaring the variable twice! Don't do this:
Java Code:class MyClass { private SomeType foo; public MyClass() { SomeType foo = new SomeType(); // foo is declared a second time here } }
but rather declare it once and only once:
Java Code:class MyClass { private SomeType foo; public MyClass() { foo = new SomeType(); // foo is not re-declared in the constructor } }
If you don't understand what it means to declare a variable, please ask or please look it up. It's a very basic concept that you must know to progress.
- 02-07-2011, 04:41 AM #20
Member
- Join Date
- Jan 2011
- Posts
- 49
- Rep Power
- 0
Thank you...YOU ROCK!!!
OMG!!! I LOVE YOU (as a coder)!!! <3
final code that works:
Risk.java:
RiskApp.java:Java Code:import javax.swing.JFormattedTextField; import javax.swing.*; import java.awt.*; import java.text.NumberFormat; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Risk extends JPanel { public JButton Enter; public JFormattedTextField stupid, yummy; public JLabel smart, dumb; public NumberFormat yucker = NumberFormat.getNumberInstance(); public NumberFormat nucker = NumberFormat.getNumberInstance(); public Risk() { smart = new JLabel("First Number:"); add(smart); stupid = new JFormattedTextField(yucker); stupid.setColumns(3); stupid.setText("0"); add(stupid); boolean isDigit; dumb = new JLabel("Second Number:"); add(dumb); yummy = new JFormattedTextField(nucker); yummy.setColumns(3); yummy.setText("0"); add(yummy); Enter = new JButton("Enter"); // enter.addActionListener(new ActionListener()); add(Enter); Enter.addActionListener(new ButtonListener()); } class ButtonListener implements ActionListener { ButtonListener() { } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Enter")) { stupid.setText("1234"); } } } }
Thank you very much. YOU ROCK!!!Java Code:import javax.swing.*; import java.text.NumberFormat; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Toolkit; public class RiskApp { private static void createAndShowGUI() { JFrame frame = new JFrame("Risk"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Risk riskPanel = new Risk(); frame.getContentPane().add(riskPanel); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); } catch (UnsupportedLookAndFeelException e) { } catch (ClassNotFoundException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } createAndShowGUI(); } }); } }
With SUPER thanks,
cc11rocks
Similar Threads
-
First Java Program-Compile Errors (errors are posted)-simple GUI
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 01-04-2011, 12:36 AM -
setText() problem
By Jozo in forum Java AppletsReplies: 4Last Post: 04-27-2010, 05:29 AM -
setText in JTextArea
By hero in forum AWT / SwingReplies: 1Last Post: 10-12-2009, 09:38 PM -
setText() problem
By jls7168 in forum New To JavaReplies: 2Last Post: 02-20-2009, 10:34 PM -
setText in event
By sniezna.stopa in forum SWT / JFaceReplies: 0Last Post: 06-20-2008, 02:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks