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);
}
}
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);
}
}