Question: Please help explain to me why you need a KeyListener for each JTextField and why it crashes when you type something into one of those

Please help explain to me why you need a KeyListener for each JTextField and why it crashes when you type something into one of those fields (see below). Code also below.

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3 at McCann.AssignRGBSlider$2.keyReleased(AssignRGBSlider.java:139) at java.awt.Component.processKeyEvent(Component.java:6486) at javax.swing.JComponent.processKeyEvent(JComponent.java:2832) at java.awt.Component.processEvent(Component.java:6302) at java.awt.Container.processEvent(Container.java:2234) at java.awt.Component.dispatchEventImpl(Component.java:4881) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

AssignRGBSlider.JAVA

import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class AssignRGBSlider implements Runnable { static int width = 320; static int height = 125; static String title = "AssignRGBSliders"; static int FIELD_WIDTH = 5; static int RED = 0; static int GREEN = 1; static int BLUE = 2; static int MIN = 0; static int MAX = 1; static String[] ts = {"Red", "Green", "Blue"}; static int[][] RGB= {{0, 255}, {0, 255}, {0, 255}}; JFrame application; JTextField[] textf; JSlider[] ColorSlider; JPanel ColorPanel; int[] CurrentCol = {0, 0, 0}; int cs; public void run() { JPanel rgbpanel; GridBagConstraints gridbag = new GridBagConstraints(); gridbag.anchor = GridBagConstraints.FIRST_LINE_START; application = new JFrame(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); application.setTitle(title); rgbpanel = new JPanel(); rgbpanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); rgbpanel.setLayout(new GridBagLayout()); application.add(rgbpanel); ColorSlider = new JSlider[ts.length]; textf = new JTextField[ts.length]; textf = new JTextField[ts.length]; for (cs = 0; cs < ts.length; cs++) { gridbag.gridy = cs; gridbag.gridx = 0; // slider is created with options, and added to the panel   ColorSlider[cs] = new JSlider(0, 255); ColorSlider[cs].addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(ChangeEvent e) { // display the Red, Green,and Blue values in the text box   for(int b=0; b<ts.length; b++) textf[b].setText(Integer.toString(ColorSlider[b].getValue())); //calls the paint method   ColorPanel.repaint(); } }); gridbag.fill = GridBagConstraints.HORIZONTAL; //shows Red, Green and Blue as the labels for sliders   rgbpanel.add(new JLabel(ts[cs]), gridbag); gridbag.gridx =1; rgbpanel.add(ColorSlider[cs], gridbag); gridbag.gridx = 2; // JTextField created with options, then added to the panel   textf[cs] = new JTextField(); textf[cs].setText(Integer.toString(ColorSlider[cs].getValue())); System.out.println(Integer.toString(ColorSlider[cs].getValue())); rgbpanel.add(textf[cs], gridbag); gridbag.fill = GridBagConstraints.VERTICAL; // change listener is added   textf[cs].addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent rk) { JTextField ntf = (JTextField)rk.getSource(); String typed = ntf.getText(); if(!typed.matches("\\d+") || typed.length() > 5) { return; } int value = Integer.parseInt(typed); ColorSlider[cs].setValue(value); } }); } ColorPanel = new ColorPanel(); // sets the width and height of the color panel box  ColorPanel.setPreferredSize(new Dimension(width / 2, height / 2)); ++gridbag.gridy; gridbag.gridx = 1; gridbag.gridwidth = 3; rgbpanel.add(ColorPanel, gridbag); application.setSize(width, height); application.setLocationRelativeTo(null); application.pack(); application.setVisible(true); } public static void main(String[] args) { AssignRGBSlider drwpnl = new AssignRGBSlider(); SwingUtilities.invokeLater(drwpnl); } class SliderChange implements ChangeListener { // constructor   public SliderChange() { } // implements the interface   public void stateChanged(ChangeEvent changeE) { JSlider source = (JSlider) changeE.getSource(); System.out.println("slider" + source); } } class ColorPanel extends JPanel { //paintComponent()   @Override protected void paintComponent(Graphics paintcomp) { paintcomp.setColor(new Color(ColorSlider[0].getValue(), ColorSlider[1].getValue(), ColorSlider[2].getValue())); System.out.println(ColorSlider[0].getValue() + " " + ColorSlider[1].getValue() + " " + ColorSlider[2].getValue()); paintcomp.fillRect(0, 0, this.getWidth(), this.getHeight()); } } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!