java - Vertical alignment of checkboxes -
could me understand how set left vertical alignment checkboxes. mean each checkbox on own row.
tried imagine , have come end of wit.
public class displayframe extends jframe { public displayframe(arraylist<string> contents){ displaypanel panel = new displaypanel(contents, whitelist); add(panel); } private void displayall(){ displayframe frame = new displayframe(contents, whitelist); gridbaglayout gbl = new gridbaglayout(); frame.setlayout(gbl); frame.setvisible(true); ... } public class displaypanel extends jpanel { arraylist<jcheckbox> cbarraylist = new arraylist<jcheckbox>(); arraylist<string> contents; ... public displaypanel(arraylist<string> contents){ ... createlistofcheckboxes(); } private void createlistofcheckboxes() { gridbagconstraints gbc = new gridbagconstraints(); gbc.gridwidth = contents.size() + 1; gbc.gridheight = contents.size() + 1; (int = 0; < contents.size(); i++){ gbc.gridx = 0; gbc.gridy = i; string configuration = contents.get(i); jcheckbox currentcheckbox = new jcheckbox(configuration); if (whitelist.contains(configuration)){ currentcheckbox.setselected(true); } currentcheckbox.setvisible(true); add(currentcheckbox, gbc); } }
"if able proceed without gridbaglayout, suit me"
you can use boxlayout
. box
convenience class boxlayout
. can do
box box = box.createverticalbox(); (int = 0; < contents.size(); i++){ string configuration = contents.get(i); jcheckbox currentcheckbox = new jcheckbox(configuration); if (whitelist.contains(configuration)){ currentcheckbox.setselected(true); } box.add(currentcheckbox); }
since box
jcomponent
, can add box
container.
you can see more @ how use boxlayout
simple example
import javax.swing.*; public class boxdemo { public static void main(string[] args) { box box = box.createverticalbox(); jcheckbox cbox1 = new jcheckbox("check me once"); jcheckbox cbox2 = new jcheckbox("check me twice"); jcheckbox cbox3 = new jcheckbox("check me thrice"); box.add(cbox1); box.add(cbox2); box.add(cbox3); joptionpane.showmessagedialog(null, box); } }
Comments
Post a Comment