java - Filling a JPanel with JRadioButtons using HashMap -
java - Filling a JPanel with JRadioButtons using HashMap -
i've been stuck on trying figure out how create popout window dynamically created based on array's content , i'm i'm missing vital might help me solve , understand issue.
what trying do?
i have programme loops through directory, collects folder names , stores in arraylist
. problem arises when seek dynamically create window using arraylist
. i'm not sure how tackle this.
what's current through process
i have 3 classes. view, model , command class. array folders stored in model class. retrieve through command class. create new jpanel within actionlistener
along hashmap
. loop through hashmap
adding string
names , jradiobutton
and seek populate window don't know how.
here's piece of code i'm working with:
public void actionperformed(actionevent e) { if (e.getsource() == theview.viewbutton) { system.out.println("view button clicked"); theview.setbottextarea(""); theview.setbottextarea("viewing..."); jpanel radiopanel = new jpanel(); // method gather folder names , stores in array themodel.listallfolders(); // create categories array , store names arraylist<string> categories = themodel.getlistofcategories(); // create hashmap names , jradiobuttons hashmap<string, jradiobutton> buttonmap = new hashmap<string, jradiobutton>(); // loop fill hashmap (int = 0; < categories.size(); i++ ) { buttonmap.put(categories.get(i), new jradiobutton(categories.get(i))); } (entry<string, jradiobutton> entry : buttonmap.entryset()) { // not sure how retrieve hashmap info create , fill window } }
i'm extremly new hashmaps (i'm trying larn it) i'm not sure if it's thought begin with. i've been stuck on task 3 days. in past tried utilize arrays accomplish similar task, i'm it's massive logic error on part prevents me completing it.
i'd appreciate fresh insight on matter.
if text shown in jradiobutton same placed hashmap, don't see need hashmap. create sure set jradiobutton's actioncommand string proper text, add together jradiobuttons same single buttongroup, , when want selection, actioncommand buttonmodel returned buttongroup's getselection() method.
e.g.
for (string text : filelist) { jradiobutton btn = new jradiobutton(text); btn.setactioncommand(text); // radiobuttons don't default buttongroup.add(btn); // buttongroup allow single selection myradiopanel.add(btn); // jpanel uses gridlayout } // if myradiopanel in gui, revalidate , repaint
later selection (if done via button's actionlistener:
buttonmodel model = buttongroup.getselection(); if (model != null) { selectedtext = model.getactioncommand(); }
or if using actionlistener added radio button, actionevent's actioncommand property.
as adding jradiobuttons jpanel, quite sure know how this. if particular step confuses you, you've not told yet.
java swing hashmap
Comments
Post a Comment