JCheckBox Array - isSelected()... help please

R

Russell

I have this code block setup within my program (extracte to this test class)

I want to be able to disable the input JTextField and the JButton if no
JCheckboxes are selected,
though if one is selected i want to enable the input JTextField and start
JButton.

What am I doing stupidly wrong here..... I feel such an idiot for having to
ask this question in this NG

thankyou for pointing out my stupidity (error)

ps: this was also double posted to comp.lang.java.help but so far i haven't
recieved any responses
########
CODE
########

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

public class Test extends JFrame{
protected JPanel cbPan, inPan;
protected JCheckBox [] chqBox = new JCheckBox[6];
protected String [] chqBxStr = {"CK1","CK2","CK3","CK4","CK5","CK6"};
protected JTextField input;
protected JButton start;

public Test() {
setSize(300,200);
cbPan = new JPanel(new GridLayout(3,2));
cbPan.setBorder(new CompoundBorder(new EtchedBorder(),
new EmptyBorder(2, 0, 2, 5)));
for (int i = 0; i < 6; i++) {
final int j = i;
chqBox[j] = new JCheckBox(chqBxStr[j]);
chqBox[j].setActionCommand(chqBxStr[j]);
ActionListener act = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == chqBxStr[j]) {
if (chqBox[j].isSelected() == true) {
input.setEnabled(true);
start.setEnabled(true);
}
}
for (int i = 0; i < 6; i++) {
if (chqBox.isSelected() == false) {
input.setEnabled(false);
start.setEnabled(false);
}
}
}
};
chqBox[j].addActionListener(act);
cbPan.add(chqBox[j]);
}

inPan = new JPanel(new GridLayout(0,1));
inPan.setBorder(new CompoundBorder(new EtchedBorder(),
new EmptyBorder(5,25,10,25)));
input = new JTextField("1000");
start = new JButton("Start");
ActionListener act = new ActionListener() {
public void actionPerformed(ActionEvent e) {
//ACTIONS HERE
}
};
input.addActionListener(act);
start.addActionListener(act);
inPan.add(input);
inPan.add(start);
input.setEnabled(false);
start.setEnabled(false);
getContentPane().add( BorderLayout.NORTH,cbPan);
getContentPane().add( BorderLayout.SOUTH,inPan);
}


public static void main(String[] args) {
Test frame = new Test();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top