Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
  • salvin francis
  • Piet Souris

code not executing

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working my way through a java tutorial book and I had to modify an application to extract digits from a 5 digit number. That part works(its not elegant, but works) I added a method that should clear the values of the display fields when a key is pressed in the "enter number" JTextField, for some reason that portion of code never gets executed. Please let me know why and how to fix. The name of the method that seems not to run is enterNumberJTextFieldKeyPressed
The code follows.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DigitExtractor extends JFrame
// JLabel and JTextField for user to input number
private JLabel enterNumberJLabel;
private JTextField enterNumberJTextField;

// JLabel and JTextFields for displaying extracted digits
private JLabel extractedDigitsJLabel;
private JTextField firstNumberJTextField;
private JTextField secondNumberJTextField;
private JTextField thirdNumberJTextField;
private JTextField fourthNumberJTextField;
private JTextField fifthNumberJTextField;

// JButton to initiate extraction of digits
private JButton enterJButton;

// no-argument constructor
public DigitExtractor()

// create and position GUI components; register event handlers
private void createUserInterface()
// get content pane for attaching GUI components
Container contentPane = getContentPane();

// enable explicit positioning of GUI components
contentPane.setLayout( null );

// set up enterNumberJLabel
enterNumberJLabel = new JLabel();
enterNumberJLabel.setText( "Enter five-digit number:" );
enterNumberJLabel.setBounds( 10, 10, 150, 24 );
contentPane.add( enterNumberJLabel );

// set up enterNumberJTextField
enterNumberJTextField = new JTextField();
enterNumberJTextField.setBounds( 160, 10, 110, 24 );
contentPane.add( enterNumberJTextField );

// set up extractedDigitsJLabel
extractedDigitsJLabel = new JLabel();
extractedDigitsJLabel.setText( "Extracted digits:" );
extractedDigitsJLabel.setBounds( 10, 45, 150, 24 );
contentPane.add( extractedDigitsJLabel );

// set up firstNumberJTextField
firstNumberJTextField = new JTextField();
firstNumberJTextField.setEditable( false );
firstNumberJTextField.setBounds( 160, 45, 15, 24 );
JTextField.CENTER );
contentPane.add( firstNumberJTextField );

// set up secondNumberJTextField
secondNumberJTextField = new JTextField();
secondNumberJTextField.setEditable( false );
secondNumberJTextField.setBounds( 180, 45, 15, 24 );
JTextField.CENTER );
contentPane.add( secondNumberJTextField );

// set up thirdNumberJTextField
thirdNumberJTextField = new JTextField();
thirdNumberJTextField.setEditable( false );
thirdNumberJTextField.setBounds( 200, 45, 15, 24 );
JTextField.CENTER );
contentPane.add( thirdNumberJTextField );

// set up fourthNumberJTextField
fourthNumberJTextField = new JTextField();
fourthNumberJTextField.setEditable( false );
fourthNumberJTextField.setBounds( 220, 45, 15, 24 );
JTextField.CENTER );
contentPane.add( fourthNumberJTextField );

// set up fifthNumberJTextField
fifthNumberJTextField = new JTextField();
fifthNumberJTextField.setEditable( false );
fifthNumberJTextField.setBounds( 240, 45, 15, 24 );
JTextField.CENTER );
contentPane.add( fifthNumberJTextField );

// set up enterJButton
enterJButton = new JButton ();
enterJButton.setText( "Enter" );
enterJButton.setBounds( 280, 10, 70, 24 );

new ActionListener() // anonymous inner class
// called when enterJButton is pressed
public void actionPerformed( ActionEvent event )
enterJButtonActionPerformed( event );

} // end anonymous inner class

); // end call to addActionListener

contentPane.add( enterJButton );

// set properties of application's window
setTitle( "Digit Extractor" ); // set title bar text
setSize( 370, 115 ); // set window size
setVisible( true ); // display window

} // end method createUserInterface

// clear extracted number JTextfields because the value is now invalid
private void enterNumberJTextFieldKeyPressed( KeyEvent event )
// end method enterNumberJTextFieldKeyPressed

// extract and display individual digits
private void enterJButtonActionPerformed( ActionEvent event )
//get user input and store it in int variable called number
int number= Integer.parseInt(enterNumberJTextField.getText());
int workingNumber=number%10; //use remainder function to get last digit
//display the results
number=number/10; //parse off last digit
workingNumber=number%10; //use remainder function to get last digit
number=number/10; //parse off last digit
workingNumber=number%10; //use remainder function to get last digit
number=number/10; //parse off last digit
workingNumber=number%10; //use remainder function to get last digit
number=number/10; //parse off last digit
workingNumber=number%10; //use remainder function to get last digit

} // end method enterJButtonActionPerformed

// main method
public static void main( String[] args )
DigitExtractor application = new DigitExtractor();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

} // end method main

} // end class DigitExtractor
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bob,

Welcome to JavaRanch!

I don't actually see anyplace that method is called. You've added an ActionListener to the JButton; you need to similarly add an ActionListener to the JTextField, and from it, call the enterNumberJTextFieldKeyPressed() method.
Bob Hinderks
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duh, that was exactly my problem, thank you so much.
Java Cowboy
Posts: 16084
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: Please use code tags when you post source code on JavaRanch, so that the forum software can nicely format your source code, which makes it easier to read for people.
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!