java - Enabling and Disabling JCheckBox's depending on JComboBox.SelectedItem -
ok have panel containing 12 jcheckbox's, upon interface loading of jcheckbox's disabled. when user selects option jcombobox want jcheckbox's enabled depending on item selected in jcombobox.
at moment in jcomboboxactionperformed enabling of jcheckboxes relevant selection prior doing attempting disable buttons enabled (in case selected item in jcombobox changed).
the code have disable enabled buttons follows:
public void disableboxes() { (jcheckbox j : arrayofjcheckbox) { if (j.isenabled()) { j.setenabled(false); } } }
this not anything, if remove call method jcomboboxactionperformed method jcheckbox's enable expect. leading me assume problem lies code.
furthermore creating arrayofjcheckbox
manually, wondering if there way maybe adding getting of jcheckbox's inside panel , adding them list? if possible possible iterate through list attempting do?
thanks help!
dean
you state:
ok have panel containing 12 jcheckbox's, upon interface loading of jcheckbox's disabled. when user selects option jcombobox want jcheckbox's enabled depending on item selected in jcombobox.
we don't know criteria use decide jcheckbox enabled or disabled, that's not super critical moment.
at moment in jcomboboxactionperformed enabling of jcheckboxes relevant selection prior doing attempting disable buttons enabled (in case selected item in jcombobox changed).
this should work.
the code have disable enabled buttons follows:
public void disableboxes() { (jcheckbox j : arrayofjcheckbox) { if (j.isenabled()) { j.setenabled(false); } } }
this not anything, if remove call method jcomboboxactionperformed method jcheckbox's enable expect. leading me assume problem lies code.
this should work, post doesn't show why not work, , given information presented can't solve specific reason why not working yet.
furthermore creating arrayofjcheckbox manually, wondering if there way maybe adding getting of jcheckbox's inside panel , adding them list? if possible it
can't since don't know how creating "arrayofjcheckbox manually" yet you've posted no code, nor can second request since have no idea of how program structured.
having said this, should easy create list of jcheckbox or jtogglebutton (the parent class) , iterate through list enabling , disabling components need arises.
for example:
import java.awt.borderlayout; import java.awt.gridbaglayout; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.arraylist; import java.util.list; import javax.swing.*; @suppresswarnings("serial") public class checkboxlist extends jpanel { private static final int check_box_count = 12; // number of jcheckboxes private list<jcheckbox> checkboxlist = new arraylist<>(); // list of jcheckboxes private defaultcomboboxmodel<integer> cmodel = new defaultcomboboxmodel<>(); // combo box model private jcombobox<integer> combobox = new jcombobox<>(cmodel); public checkboxlist() { // create jpanel hold jcheckboxes jpanel checkboxpanel = new jpanel(new gridlayout(0, 1, 0, 5)); // create jcheckboxes , add both above jpanel , checkboxlist (int = 0; < check_box_count; i++) { string text = "checkbox " + i; jcheckbox checkbox = new jcheckbox(text); checkbox.setenabled(false); // disabled default checkboxpanel.add(checkbox); checkboxlist.add(checkbox); } // fill our combo box's model. example, i'm going use // integers, , enable jcheckboxes multiples of selected int (int = 0; < 5; i++) { cmodel.addelement(i + 1); } combobox.setselectedindex(-1); // set combo @ empty combobox.addactionlistener(new combolistener()); // add actionlistner // jpanel hold jcombobox jpanel centerpanel = new jpanel(new gridbaglayout()); centerpanel.add(combobox); int gap = 35; centerpanel.setborder(borderfactory.createemptyborder(gap, gap, gap, gap)); // add main jpanel (this) gap = 5; setborder(borderfactory.createemptyborder(gap, gap, gap, gap)); setlayout(new borderlayout()); add(centerpanel, borderlayout.center); add(checkboxpanel, borderlayout.line_end); } private class combolistener implements actionlistener { @override public void actionperformed(actionevent e) { // combo's selection int selection = (integer) combobox.getselecteditem(); // use information enable/disable jcheckboxes (int = 0; < checkboxlist.size(); i++) { if (i % selection == 0) { checkboxlist.get(i).setenabled(true); } else { checkboxlist.get(i).setenabled(false); } } } } // create , display gui private static void createandshowgui() { checkboxlist mainpanel = new checkboxlist(); jframe frame = new jframe("checkboxlist"); frame.setdefaultcloseoperation(jframe.dispose_on_close); frame.getcontentpane().add(mainpanel); frame.pack(); frame.setlocationbyplatform(true); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } }
again, if doesn't help, best served creating , posting minimal, complete, , verifiable example program condense code smallest bit still compiles , runs, has no outside dependencies (such need link database or images), has no code that's not relevant problem, still demonstrates problem, similar code above.
Comments
Post a Comment