java - Using JComboBox with ItemListener/ActionListener -
i've begun coding in java , wondering how achieve this. want user able input text text box, select font, color , size display in label @ bottom when ok button clicked. appreciated. thanks.
package textchanger; import java.awt.event.*; import javax.swing.*; import java.awt.*; public class textchanger implements actionlistener { string[] fontstrings = {"arial", "arial black", "helvetica", "impact", "times new roman"}; string[] sizestrings = {"10", "12", "14", "16", "18"}; string[] colorstrings = {"red", "blue", "green", "yellow", "orange"}; string[] bgstrings = {"red", "blue", "green", "yellow", "orange"}; jpanel panel; jlabel labeltext, labelfont, labelsize, labelcolor, labelbg, labeloutput; jtextfield textfield; jcombobox combofont, combosize, combocolor, combobg; jbutton btnok, btncancel; public jpanel contentpane() { //creates gui panel = new jpanel(); panel.setlayout(new gridlayout(8, 8, 10, 10)); labeltext = new jlabel("enter text:"); textfield = new jtextfield(10); labelfont = new jlabel("select font type:"); combofont = new jcombobox(fontstrings); combofont.setselectedindex(0); combofont.addactionlistener(this); labelsize = new jlabel("select font size:"); combosize = new jcombobox(sizestrings); combosize.setselectedindex(0); combosize.addactionlistener(this); labelcolor = new jlabel("select font color:"); combocolor = new jcombobox(colorstrings); combocolor.setselectedindex(0); combocolor.addactionlistener(this); labelbg = new jlabel("select background color:"); combobg = new jcombobox(bgstrings); combobg.setselectedindex(0); combobg.addactionlistener(this); btnok = new jbutton("ok"); btncancel = new jbutton("cancel"); labeloutput = new jlabel(""); panel.add(labeltext); panel.add(textfield); panel.add(labelfont); panel.add(combofont); panel.add(labelsize); panel.add(combosize); panel.add(labelcolor); panel.add(combocolor); panel.add(labelbg); panel.add(combobg); panel.add(btnok); panel.add(btncancel); panel.add(labeloutput); return panel; } public static void main(string[] args) { jframe frame = new jframe("fonts, colors , sizes"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(650, 350); textchanger txtobj = new textchanger(); frame.setcontentpane(txtobj.contentpane()); frame.setvisible(true); } }
for clarity , other reasons, i'd avoid making class implements actionlistener
. add anonymous actionlistener
each jcombobox
. this
jcombobox combofont; jlabel label = new jlabel("label"); string fontstring = "ariel"; int fontweight = font.plain; int fontsize = 16; font font = new font(fontstring, fontweight, fontsize); color textcolor = color.black public jpanel contentpane() { combofont = new jcombobox(fontstrings); combofont.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e) { fontstring = (string)combofont.getselecteditem(); font = new font(fontstring, fontweight, fontsize); label.setfont(font); } }); }
what dynamically change font when new font selected combobox. should have global values font
, fontsize
, fontweight
each different combobox can make use of them , change font accordingly in actionperformed
also, take @ this answer andrewthompson, showing actual rendered font style in jcombobox
, fonts obtained system fonts. here's glimpse. don't forget up-vote answer in link!
give try. revamped code
import java.awt.event.*; import javax.swing.*; import java.awt.*; import javax.swing.event.documentevent; import javax.swing.event.documentlistener; public class textchanger { //string[] fontstrings = {"arial", "arial black", "helvetica", "impact", "times new roman"}; integer[] fontsizes = {10, 12, 14, 16, 18, 20, 22, 24}; string[] colorstrings = {"red", "blue", "green", "yellow", "orange"}; string[] bgstrings = {"red", "blue", "green", "yellow", "orange"}; string[] fontstyle = {"bold", "italic", "plain"}; jpanel panel; jlabel labeltext, labelfont, labelsize, labelcolor, labelbg, labeloutput; jtextfield textfield; jcombobox combofont, combosize, combocolor, combobg; jbutton btnok, btncancel; string fontstring; int fontweight = font.plain; int fontsize; font font = new font(fontstring, font.plain, fontsize); color textcolor; color bgcolor; static string text = "text"; static jlabel textlabel = new jlabel(text); jpanel textlabelpanel = new jpanel(new gridbaglayout()); public jpanel contentpane() { //creates gui panel = new jpanel(); panel.setlayout(new gridlayout(7, 8, 10, 10)); textlabelpanel.setpreferredsize(new dimension(500, 50)); labeltext = new jlabel("enter text:"); textfield = new jtextfield(10); textfield.settext(text); textfield.getdocument().adddocumentlistener(new documentlistener() { @override public void insertupdate(documentevent e) { string newtext = textfield.gettext(); textlabel.settext(newtext); } @override public void removeupdate(documentevent e) { string newtext = textfield.gettext(); textlabel.settext(newtext); } @override public void changedupdate(documentevent e) { } }); labelfont = new jlabel("select font type:"); graphicsenvironment ge = graphicsenvironment. getlocalgraphicsenvironment(); string[] fonts = ge.getavailablefontfamilynames(); combofont = new jcombobox(fonts); fontstring = (string) combofont.getitemat(0); combofont.setselectedindex(0); combofont.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { fontstring = (string) combofont.getselecteditem(); font = new font(fontstring, fontweight, fontsize); textlabel.setfont(font); } }); labelsize = new jlabel("select font size:"); combosize = new jcombobox(fontsizes); combosize.setselectedindex(0); fontsize = (integer) combosize.getitemat(0); combosize.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { fontsize = (integer) combosize.getselecteditem(); font = new font(fontstring, fontweight, fontsize); textlabel.setfont(font); } }); labelcolor = new jlabel("select font color:"); combocolor = new jcombobox(colorstrings); combocolor.setselectedindex(0); textcolor = color.red; textlabel.setforeground(textcolor); combocolor.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { string colorstring = (string) combocolor.getselecteditem(); switch (colorstring) { case "red": textcolor = color.red; break; case "blue": textcolor = color.blue; break; case "green": textcolor = color.green; break; case "yellow": textcolor = color.yellow; break; case "orange": textcolor = color.orange; break; default: textcolor = color.red; } textlabel.setforeground(textcolor); } }); labelbg = new jlabel("select background color:"); combobg = new jcombobox(bgstrings); combobg.setselectedindex(1); bgcolor = color.blue; textlabelpanel.setbackground(bgcolor); combobg.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { string bgcolorstring = (string) combobg.getselecteditem(); switch (bgcolorstring) { case "red": bgcolor = color.red; break; case "blue": bgcolor = color.blue; break; case "green": bgcolor = color.green; break; case "yellow": bgcolor = color.yellow; break; case "orange": bgcolor = color.orange; break; default: bgcolor = color.red; } textlabelpanel.setbackground(bgcolor); } }); btnok = new jbutton("ok"); btncancel = new jbutton("cancel"); labeloutput = new jlabel(""); panel.add(labeltext); panel.add(textfield); panel.add(labelfont); panel.add(combofont); panel.add(labelsize); panel.add(combosize); panel.add(labelcolor); panel.add(combocolor); panel.add(labelbg); panel.add(combobg); panel.add(btnok); panel.add(btncancel); panel.add(labeloutput); jpanel mainpanel = new jpanel(new borderlayout()); mainpanel.add(panel); textlabelpanel.add(textlabel); mainpanel.add(textlabelpanel, borderlayout.south); return mainpanel; } class fontcellrenderer extends defaultlistcellrenderer { public component getlistcellrenderercomponent( jlist list, object value, int index, boolean isselected, boolean cellhasfocus) { jlabel label = (jlabel) super.getlistcellrenderercomponent( list, value, index, isselected, cellhasfocus); font font = new font((string) value, font.plain, 20); label.setfont(font); return label; } } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { jframe frame = new jframe("fonts, colors , sizes"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(650, 350); textchanger txtobj = new textchanger(); frame.setcontentpane(txtobj.contentpane()); frame.setvisible(true); } }); } }
Comments
Post a Comment