• Post Reply Bookmark Topic Watch Topic
  • New Topic

code not executing  RSS feed

 
Bob Hinderks
Greenhorn
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()
{
createUserInterface();
}

// 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 );
firstNumberJTextField.setHorizontalAlignment(
JTextField.CENTER );
contentPane.add( firstNumberJTextField );

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

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

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

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

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

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 )
{
firstNumberJTextField.setText("");
secondNumberJTextField.setText("");
thirdNumberJTextField.setText("");
fourthNumberJTextField.setText("");
fifthNumberJTextField.setText("");
}
// 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
fifthNumberJTextField.setText(String.valueOf(workingNumber));
number=number/10; //parse off last digit
workingNumber=number%10; //use remainder function to get last digit
fourthNumberJTextField.setText(String.valueOf(workingNumber));
number=number/10; //parse off last digit
workingNumber=number%10; //use remainder function to get last digit
thirdNumberJTextField.setText(String.valueOf(workingNumber));
number=number/10; //parse off last digit
workingNumber=number%10; //use remainder function to get last digit
secondNumberJTextField.setText(String.valueOf(workingNumber));
number=number/10; //parse off last digit
workingNumber=number%10; //use remainder function to get last digit
firstNumberJTextField.setText(String.valueOf(workingNumber));

} // 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
 
Zachary Friedman-Hill
milkman
Greenhorn
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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duh, that was exactly my problem, thank you so much.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!