TopMore Panels and GUI ComponentsKeyboard input

Keyboard input

The simplest way to enter data via the keyboard is to enter data in a JTextField GUI component. The program ColorMixer is similar to ColorSlider, but instead of selecting numbers using a JSlider, instead they are entered directly into a JTextField. In this particular case, we want to trigger an event whenever the user types a number and then hits the return key. Hitting the return key when typing in a JTextField triggers an ActionEvent, just like a JButton does.

The method Integer.parseInt(...) takes a string parameter and returns an int. Thus the code

    int mix = Integer.parseInt(stringValue);
converts stringValue to the corresponding int value.

See also the program Interesting, which calculates the interest on an investment.


TopMore Panels and GUI ComponentsKeyboard input