Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: setText Not Working

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default setText Not Working

    Hi, I'm taking my first Computer Science class currently and have come to a problem with one of my codes.

    // Tutorial 5: Inventory.java
    // Calculates the number of items in a shipment based on the number
    // of cartons received and the number of items per carton.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Inventory extends JFrame
    {
       // JLabel and JTextField for cartons per shipment
       private JLabel cartonsJLabel;
       public JTextField cartonsJTextField;
       // JLabel and JTextField for items per carton
       public JLabel itemsJLabel;
       public JTextField itemsJTextField;
       // JLabel and JTextField for total items per shipment
       private JLabel totalJLabel;
       public JTextField totalResultJTextField;
       // JButton to initiate calculation of total items per shipment
       private JButton calculateJButton;
       // no-argument constructor
       public Inventory()
       {
          createUserInterface();
       }
       // create and position GUI components; register event handlers
       public void createUserInterface()
       {
          // get content pane and set layout to null
          Container contentPane = getContentPane();
          contentPane.setLayout( null );
          // set up cartonsJLabel
          cartonsJLabel = new JLabel();
          cartonsJLabel.setText( "Cartons per shipment:" );
    	  cartonsJLabel.setBounds( 16, 16, 130, 21 );
          contentPane.add( cartonsJLabel );
          // set up itemsJLabel
          itemsJLabel = new JLabel();
          itemsJLabel.setText("Items per Carton:");
    	  itemsJLabel.setBounds(16, 48, 104, 21);
          contentPane.add( itemsJLabel );
          // set up totalJLabel
          totalJLabel = new JLabel();
          totalJLabel.setText("Total:");
    	  totalJLabel.setBounds(204, 16, 40, 21);
          contentPane.add( totalJLabel );
          // set up cartonsJTextField
          cartonsJTextField = new JTextField();
          cartonsJTextField.setText("0");
    	  cartonsJTextField.setBounds( 148, 16, 40, 21 );
    	  cartonsJTextField.setHorizontalAlignment(JTextField.RIGHT);
          contentPane.add( cartonsJTextField );
          // set up itemsJTextField
          itemsJTextField = new JTextField();
          itemsJTextField.setText("0");
    	  itemsJTextField.setBounds( 148, 48, 40, 21 );
    	  itemsJTextField.setHorizontalAlignment(JTextField.RIGHT);
          contentPane.add( itemsJTextField );
          // set up totalResultJTextField
          totalResultJTextField = new JTextField();
          totalResultJTextField.setBounds( 244, 16, 86, 21 );
    	  totalResultJTextField.setHorizontalAlignment(
    		  JTextField.RIGHT);
    	  totalResultJTextField.setEditable( false );
          contentPane.add( totalResultJTextField );
          // set up calculateJButton
          calculateJButton = new JButton();
          calculateJButton.setText("Calculate Total");
    	  calculateJButton.setBounds(204, 48, 126, 24);
          contentPane.add( calculateJButton );
          calculateJButton.addActionListener(
          	   new ActionListener() // anonymous inner class
             {
                // event handler called when calculateJButton is pressed
                public void actionPerformed( ActionEvent event )
                {
                   calculateJButtonActionPerformed( event );
                }
             } // end anonymous inner class
          ); // end call to addActionListener
          // set properties of application’s window
          setTitle( "Inventory" ); // set title bar text
          setSize( 354, 112 );     // set window size
          setVisible( true );      // display window
       } // end method createUserInterface
       //calculate the total items in the shipment
    	public void calculateJButtonActionPerformed( ActionEvent event )
    	{
    		int cartons; // stores number of cartons
    		int items;
    		int result;
    		//retrieve number from JTextFields
    		cartons = Integer.parseInt( cartonsJTextField.getText() );
    		items = Integer.parseInt( itemsJTextField.getText() );
    		result = cartons * items;
    		totalResultJTextField.setText( String.valueOf( result ) );
    	} // end method calculateJButtonActionPerformed
       public void cartonsJTextFieldKeyPressed( KeyEvent event )
       {
       		totalResultJTextField.setText( "" );
     
       }
       public void itemsJTextFieldKeyPressed( KeyEvent event )
       {
       		totalResultJTextField.setText( "" );
     
       }
    	public static void main( String[] args )
       {
          Inventory application = new Inventory();
          application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
       }
    }

    The program runs correctly by calculating, but the setText when KeyPressed doesn't work. Thanks in advance for your help.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: setText Not Working

    Code should be posted in the form of an SSCCE - that's a lot of code to demonstrate a single keyPressed and setText call, and your indentation makes the code pretty hard to read.

    I recommend you get rid of all the extra, then tell us exactly which line you're talking about, as well as what you mean by "doesn't work".

    Recommended reading: http://www.javaprogrammingforums.com...t-println.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: setText Not Working

    Alright, here's the part I'm having trouble with.

       public void cartonsJTextFieldKeyPressed( KeyEvent event )
       {
       		totalResultJTextField.setText( "" );
     
       }
       public void itemsJTextFieldKeyPressed( KeyEvent event )
       {
       		totalResultJTextField.setText( "" );
     
       }

    And when I say it doesn't work, I mean that when I press a key to get rid of what is already in the box, I want it to delete what's in the totalResult box.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: setText Not Working

    An SSCCE should still be runnable simply by copying and pasting it into an editor. For your case, your program should just be a single JTextField with whatever listener you're trying to get to work.

    When are those methods called? When do you add the KeyListener? What do you add it to?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: setText Not Working

    Did you ever specify which key it is?

Similar Threads

  1. .setText() for an int?
    By SilentNite17 in forum AWT / Java Swing
    Replies: 12
    Last Post: April 1st, 2012, 11:09 AM
  2. Jar not WORKING FOR ANYONE BUT ME!!!!!!!! HELP
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 12
    Last Post: February 18th, 2012, 07:32 AM
  3. Why isn't this working?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2011, 04:08 PM
  4. setText() NULL pointer excception problem
    By TempSpectre in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2010, 08:32 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM