java - How to set Combobox value to one of the choices in the list -
i need change value of current jcombobox value.
i got in settings gui class used main.class:
package com.tominocz.cookieclicker; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.flowlayout; import java.awt.toolkit; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.border.lineborder; @suppresswarnings({ "serial", "rawtypes", "unchecked" }) public class settings extends jframe { private string[] choicelist = { "default (arial)", "comic sans ms" }; public class choicecombolistener implements actionlistener { public void actionperformed(actionevent ev) { jcombobox cb = (jcombobox) ev.getsource(); string currentcomboselection = (string) cb.getselecteditem(); if (currentcomboselection.equals(choicelist[0])) { main.selectedfont = "arial"; main.refreshgame(); main.opt.setsize(240, 105); save.savegame(main.save); } if (currentcomboselection.equals(choicelist[1])) { main.selectedfont = "comic sans ms"; main.refreshgame(); main.opt.setsize(240, 107); save.savegame(main.save); } } } public settings() { super("settings"); setlayout(new flowlayout()); dimension screen = toolkit.getdefaulttoolkit().getscreensize(); int x = (int) (((screen.getwidth()) - this.getwidth()) / 2 - 200); int y = (int) (((screen.getheight()) - this.getheight()) / 2 - 100); this.setlocation(x, y); this.setalwaysontop(true); this.seticonimage(((imageicon) main.settingsicon).getimage()); jpanel northpanel = new jpanel(); northpanel.setignorerepaint(true); northpanel.setborder(new lineborder(color.gray)); this.getcontentpane().add(northpanel, borderlayout.north); jcombobox choicecombo = new jcombobox(choicelist); northpanel.add(main.choosefont); northpanel.add(choicecombo); choicecombo.addactionlistener(new choicecombolistener()); // hereeeeeeeeeeeeeeeeeeeeeee if (main.selectedfont.equals("comic sans ms")) { [the current jcombobox value(may default 1 - choicelist[0])] = choicelist[1]; } // hereeeeeeeeeeeeeeeeeeeeeee main.ok.setborder(null); main.ok.setborderpainted(false); main.ok.setcontentareafilled(false); add(main.ok); main.ok.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { object source = e.getsource(); if (source instanceof jbutton) { main.opt.dispose(); } } }); } }
now how change currentcomboselection value if i'm not available use in:
public settings() {
...
?
suggestions:
- you can set jcombobox's selected item 1 of 2 ways: via
setselectedindex(int index)
orsetselecteditem(object item)
first way, choose index of desired selection , 2nd way choose object held jcombobox (here strings) supposed selected.
other issues:
- you getting data main class directly accessing field, possibly static field @ that. don't make code tightly coupled, set hard find bugs, , difficult enhance or change. use tightly controlled getter methods instead. i.e., give main
public font getselectedfont()
method class can call method in non-static way without directly fiddling main's fields, without making main fields static shouldn't static. - the same goes directly changing main's state in settings class -- that's not how java safely same reasons above. oops principles don't thrown out window because you're creating event-driven gui.
- it looks supposed secondary window displayed main gui window. if so, should displayed dialog, such jdialog or joptionpane , not jframe. prevent display icon on os's toolbar, guarantee window on top of main window, , allow window displayed in modal fashion if need be.
- you're setting layout of contentpane
flowlayout
, , yet trying add jpanelborderlayout.north
position. won't work.
for example, simple code similar yours uses joptionpane secondary dialog window:
import java.awt.component; import java.awt.dimension; import java.awt.event.actionevent; import javax.swing.*; public class testsettingspanel { private static void createandshowgui() { mainpanel mainpanel = new mainpanel(); jframe frame = new jframe("test settings"); 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(); } }); } } class mainpanel extends jpanel { private static final long serialversionuid = 1l; public static final string[] choice_list = { "default (arial)", "comic sans ms", "courier", "times new roman" }; private static final int pref_w = 500; private static final int pref_h = 400; private jtextfield fontfield = new jtextfield(10); private settingspanel settingspanel; public mainpanel() { fontfield.setfocusable(false); fontfield.settext(choice_list[0]); add(new jlabel("font: ")); add(fontfield); add(new jbutton(new settingsaction("change settings"))); } @override // let's make bigger public dimension getpreferredsize() { if (ispreferredsizeset()) { return super.getpreferredsize(); } return new dimension(pref_w, pref_h); } private class settingsaction extends abstractaction { private static final long serialversionuid = 1l; public settingsaction(string name) { // give our button / action text super(name); int mnemonic = (int) name.charat(0); putvalue(mnemonic_key, mnemonic); // set alt-key combination short-cut } @override public void actionperformed(actionevent e) { // create our settingspanel in lazy fashion if (settingspanel == null) { settingspanel = new settingspanel(); } // set combo selection calling setsetting method: settingspanel.setsetting(fontfield.gettext()); // create joptionpane , dsiplay settingspanle inside of component parentcomponent = mainpanel.this; string title = "change font settings"; int optiontype = joptionpane.ok_cancel_option; int messagetype = joptionpane.plain_message; // display joptionpane here int result = joptionpane.showconfirmdialog(parentcomponent, settingspanel, title, optiontype, messagetype); // find out button user pressed if (result == joptionpane.ok_option) { // if ok button pressed, extract information settingspanel string fonttype = settingspanel.getfonttype(); fontfield.settext(fonttype); } } } } // note settingspanel ignorant gui displays it. class settingspanel extends jpanel { private static final long serialversionuid = 1l; private jcombobox<string> choicelistcombo = new jcombobox<>(mainpanel.choice_list); public settingspanel() { add(new jlabel("select font:")); add(choicelistcombo); } // allow outside classes set combo's selected item public void setsetting(string text) { if (text != null && !text.trim().isempty()) { choicelistcombo.setselecteditem(text); } } // allow outside classes extract selected item combo public string getfonttype() { return (string) choicelistcombo.getselecteditem(); } }
Comments
Post a Comment