Results 1 to 20 of 20
  1. #1
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default ButtonListener setText not working (no errors)

    My ButtonListener, when activated (Enter button pressed), is supposed to
    Java Code:
    stupid.setText("1234")
    It does absolutely nothing (the button). The rest of the GUI is working fine.
    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:
    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]
    }
    }
    }
    }
    RiskApp.java:
    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();
                }
            });
        }
    }
    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.
    Thanks for your help,
    cc11rocks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default Yes.

    Yes. enter is the button name. "Enter" is the string that shows up for the button.
    cc11rocks

  4. #4
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default 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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  6. #6
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default 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.

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

    Default

    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)?

  8. #8
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default

    I declared it in two places:
    Java Code:
    stupid.setText("0");
    and
    Java Code:
     stupid.setText("1234");
    I can't do this? If not, how do I get it to work?

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

    Default

    That's not declaring a variable. Declaring a variable is when you do this:

    Java Code:
    JFormattedTextField stupid
    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.

  10. #10
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default

    Risk.java now looks like this:
    Java Code:
    public class Risk extends JPanel {
        public JButton Enter;
    public JFormattedTextField yummy;
    I only wrote the first couple lines because everything is the same. When I remove stupid, I get an (obvious) error.
    Java Code:
    Risk.java:40: cannot find symbol
    symbol : variable stupid
    location: class Risk.ButtonListener
    stupid.setText("1234");
    I'm assuming I have to reference it somehow?

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

    Default

    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.

  12. #12
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default

    old 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");
    }
    }
    }
    }
    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 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) {
    if..." up at the class but it gave errors and i eventually gave up.
    Thanks,
    cc11rocks

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

    Default

    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 Style
    Last edited by Fubarable; 02-07-2011 at 04:04 AM.

  14. #14
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default How do I do this?

    And I do this how?

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  16. #16
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default 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");
    			}
    		}
    	}
    }

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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?

  18. #18
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default

    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

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

    Default

    Quote Originally Posted by cc11rocks View Post
    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.

  20. #20
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default Thank you...YOU ROCK!!!

    OMG!!! I LOVE YOU (as a coder)!!! <3
    final code that works:
    Risk.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");
             }
          }
       }
    }
    RiskApp.java:
    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();
                }
            });
        }
    }
    Thank you very much. YOU ROCK!!!
    With SUPER thanks,
    cc11rocks

Similar Threads

  1. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  2. setText() problem
    By Jozo in forum Java Applets
    Replies: 4
    Last Post: 04-27-2010, 05:29 AM
  3. setText in JTextArea
    By hero in forum AWT / Swing
    Replies: 1
    Last Post: 10-12-2009, 09:38 PM
  4. setText() problem
    By jls7168 in forum New To Java
    Replies: 2
    Last Post: 02-20-2009, 10:34 PM
  5. setText in event
    By sniezna.stopa in forum SWT / JFace
    Replies: 0
    Last Post: 06-20-2008, 02:56 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
  •