I am making an assignment and I have to follow a template given to me to finish this application.
I am given 2 arrays, one is JTextField[]
, the other one is JSlider[]
.
And this is where it reports the error:
for (ix = 0; ix < cc.length; ++ix)
{
gbc.gridy = ix;
gbc.gridx = 0;
JLabel label = new JLabel(cc[ix]);
panel.add(label, gbc);
/* create a slider, set its options, and add it to the panel */
csld[ix] = new JSlider(0, 255);
csld[ix].addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(ChangeEvent e) {
txt[ix].setText(Integer.toString(e.getSource())); //ERROR
}
});
panel.add(csld[ix], gbc);
gbc.gridx = 2;
/* create a JTextField, set its options, and add it to the panel */
txt[ix] = new JTextField();
txt[ix].setText(Integer.toString(csld[ix].getValue()));
System.out.println(Integer.toString(csld[ix].getValue()));
panel.add(txt[ix], gbc);
/* add a change listener */
txt[ix].addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
JTextField jtf = (JTextField)e.getSource();
String typed = jtf.getText();
if(!typed.matches("\\d+") || typed.length() > 3) {
return;
}
int value = Integer.parseInt(typed);
csld[ix].setValue(value); //ERROR
}
});`
I am not sure why this error is being shown and no other StackOverflow answers have helped me diagnose it. I'll post the relevant code here:
//imports
public class AssignRGBSliders implements Runnable
{
static int width = 320;
static int height = 128;
static String title = "RGB Sliders";
static int FIELD_WIDTH = 3;
static int RED = 0;
static int GREEN = 1;
static int BLUE = 2;
static int MIN = 0;
static int MAX = 1;
static String[] cc = {"Red", "Green", "Blue"};
static int[][] RGB_MNMX = {{0, 255}, {0, 255}, {0, 255}};
JFrame application;
JTextField[] txt;
JSlider[] csld;
JPanel colorPanel;
int[] curColor = {0, 0, 0};
public void run()
{
JPanel panel, panel2;
int ix;
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setTitle(title);
panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
panel.setLayout(new GridBagLayout());
application.add(panel);
csld = new JSlider[cc.length];
txt = new JTextField[cc.length];
for (ix = 0; ix < cc.length; ++ix)
{
gbc.gridy = ix;
gbc.gridx = 0;
JLabel label = new JLabel(cc[ix]);
panel.add(label, gbc);
gbc.gridx = 1;
/* create a slider, set its options, and add it to the panel */
csld[ix] = new JSlider(0, 255);
csld[ix].addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider)e.getSource();
txt[ix].setText(Integer.toString(e.getSource()));
}
});
panel.add(csld[ix], gbc);
gbc.gridx = 2;
/* create a JTextField, set its options, and add it to the panel */
txt[ix] = new JTextField();
txt[ix].setText(Integer.toString(csld[ix].getValue()));
System.out.println(Integer.toString(csld[ix].getValue()));
panel.add(txt[ix], gbc);
/* add a change listener */
txt[ix].addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
JTextField jtf = (JTextField)e.getSource();
String typed = jtf.getText();
if(!typed.matches("\\d+") || typed.length() > 3) {
return;
}
int value = Integer.parseInt(typed);
csld[ix].setValue(value);
}
});
/* slider.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(ChangeEvent e) {
jtf.setText(Integer.toString(slider.getValue()));
}
}); */
}
colorPanel = new ColorPanel();
colorPanel.setPreferredSize(new Dimension(width / 2, height / 2));
++gbc.gridy;
gbc.gridx = 0;
gbc.gridwidth = 3;
panel.add(colorPanel, gbc);
application.setSize(width, height);
application.setLocationRelativeTo(null);
application.pack();
application.setVisible(true);
}
public static void main(String[] args)
{
AssignRGBSliders dp = new AssignRGBSliders();
SwingUtilities.invokeLater(dp);
}
class SliderChange implements ChangeListener
{
/* instance variables */
/* constructor */
public SliderChange() {
}
/* implement the interface */
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider) e.getSource();
System.out.println("Sukurac: " + source);
}
}
class ColorPanel extends JPanel {
public void paint(Graphics gr) {
super.paint(gr);
gr.setColor(new Color(csld[0].getValue(), csld[1].getValue(), csld[2].getValue()));
System.out.println(csld[0].getValue() + " " + csld[1].getValue() + " " + csld[2].getValue());
gr.fillRect(0, 0, this.getWidth(), this.getHeight());
}
}
}
Thank you in advance!
ix
is a local variable from within the context you're trying to use it, the compiler cannot determine what it's value will be at some point in the future, even if it could, it'd probably be something like cc.length - 1
My general suggestion would be to use a HashMap
to map the JTextField
to the JSlider
Map<JSlider, JTextField> fieldMap;
//...
fieldMap = new HashMap<>();
//...
JSlider slider = new JSlider(0, 255);
JTextField field = new JTextField(10);
fieldMap.put(slider, field);
slider.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider) e.getSource();
JTextField field = fieldMap.get(slider);
field.setText(Integer.toString(slider.getValue()));
}
});
We haven't learnt/aren't allowed to use
Map
/HashMap
Well, in that case I'd suggest using a List
of some kind instead, as it provides useful functionality to ascertain the indexOf
elements
List<JTextField> fieldList;
List<JSlider> sliderList;
//...
JSlider slider = new JSlider(0, 255);
JTextField field = new JTextField(10);
fieldList.add(field);
sliderList.add(slider);
slider.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider) e.getSource();
int index = sliderList.indexOf(slider);
JTextField field = fieldList.get(index);
field.setText(Integer.toString(slider.getValue()));
}
});
The problem with this is it's easy for the List
s to come out of sync, so I prefer Map
for this kind of thing
We haven't learnt/aren't allowed to use
List
/ArrayList
Oookaay. We do it the hard way.
Basically, you have a reference to the JSlider
which triggered the event, we need to know it's index
in the csld
so we can look up the corresponding JTextField
...
csld[ix].addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider) e.getSource();
for (int index = 0; index < csld.length; index++) {
if (csld[index] == slider) {
txt[index].setText(Integer.toString(slider.getValue()));
break;
}
}
}
});