java - Is there a way to add the value chosen from all combobox into 1 textfield? -


is there way me add value of combobox choosen 1 textfield? i.e noofbks.

for example, have 2 comboboxes, quantitiescb1 , ebquantitiescb1. quantitiescb1 hardcopies, ebquantitiescb1 ebook

every of code working fine. @ end of program page, have text field stating total number of books.

am able add first actionlistener do

totalnoofbks += currenquantity; 

for both actionlistener quantitiescb1 , ebquantitiescb2?

inside ebquantitiescb2 have same above code additional

totalnoofbks += currenquantity; noofbks.settext(totalnoofbks); 

below coding working fine.

public class cataloguepanel extends jpanel {         jpanel catalogue = new jpanel(new gridbaglayout());          //batman cost , fields         string value1 = "15";         string ebvalue1 = "12";         final jtextfield result1 = new jtextfield();         final jtextfield ebresult1 = new jtextfield();         //total no. of books field         final jtextfield noofbks = new jtextfield();          final int totalbks;          public cataloguepanel(){         jpanel catalogue = new jpanel(new gridbaglayout());          //combobox batman textfield         jcombobox quantitiescb1 = new jcombobox(quantities1);         quantitiescb1.setpreferredsize(new dimension(125,20));         quantitiescb1.addactionlistener(                 new actionlistener(){                     public void actionperformed(actionevent e){                         jcombobox combo = (jcombobox)e.getsource();                         string currentquantity = (string)combo.getselecteditem();                         int finalvalue1 = integer.valueof(value2);                         int finalvalue2 = integer.valueof(currentquantity);                          string resulttext = string.valueof(finalvalue1*finalvalue2);                         result2.settext("$" + resulttext);                     }                 }                     );                    jcombobox ebquantitiescb1 = new jcombobox(quantities1);         ebquantitiescb1.setpreferredsize(new dimension(125,20));         ebquantitiescb1.addactionlistener(                 new actionlistener(){                     public void actionperformed(actionevent e){                         jcombobox combo = (jcombobox)e.getsource();                         string currentquantity = (string)combo.getselecteditem();                         int finalvalue1 = integer.valueof(ebvalue2);                         int finalvalue2 = integer.valueof(currentquantity);                          string resulttext = string.valueof(finalvalue1*finalvalue2);                         ebresult2.settext("$" + resulttext);                     }                 }                     );     } } 

  • add integer or double value directly comboboxmodel

enter image description here

import java.awt.gridlayout; import java.util.vector; import javax.swing.icon; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.swingutilities; import javax.swing.uimanager;  public class comboboxintegermodel {      private jcombobox comboboxdouble;     private jcombobox comboboxinteger;     private jcombobox comboboxboolean;     private jcombobox comboboxicon;     private vector<double> doublevector = new vector<double>();     private vector<integer> integervector = new vector<integer>();     private vector<boolean> booleanvector = new vector<boolean>();     private vector<icon> iconvector = new vector<icon>();     private icon icon1 = ((uimanager.geticon("optionpane.erroricon")));     private icon icon2 = (uimanager.geticon("optionpane.informationicon"));     private icon icon3 = (uimanager.geticon("optionpane.warningicon"));     private icon icon4 = (uimanager.geticon("optionpane.questionicon"));      public comboboxintegermodel() {         doublevector.addelement(1.001);         doublevector.addelement(10.00);         doublevector.addelement(0.95);         doublevector.addelement(4.2);         comboboxdouble = new jcombobox(doublevector);         integervector.addelement(1);         integervector.addelement(2);         integervector.addelement(3);         integervector.addelement(4);         comboboxinteger = new jcombobox(integervector);         booleanvector.add(boolean.true);         booleanvector.add(boolean.false);         comboboxboolean = new jcombobox(booleanvector);         iconvector.addelement(icon1);         iconvector.addelement(icon2);         iconvector.addelement(icon3);         iconvector.addelement(icon4);         comboboxicon = new jcombobox(iconvector);         jframe frame = new jframe("");         frame.setlayout(new gridlayout(2,2,5,5));         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.add(comboboxdouble);         frame.add(comboboxinteger);         frame.add(comboboxboolean);         frame.add(comboboxicon);         frame.setlocationrelativeto(null);         frame.pack();         frame.setvisible(true);     }      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             @override             public void run() {                 comboboxintegermodel cbmodel = new comboboxintegermodel();             }         });     } } 
  • then avoiding parseint jcombobox

  • use jformattedtextfield number formatter, same reason mentioned jcombobox

enter image description here

import java.awt.*; import java.awt.font.textattribute; import java.math.*; import java.text.*; import java.util.map; import javax.swing.*; import javax.swing.jformattedtextfield.*; import javax.swing.event.*; import javax.swing.text.internationalformatter;  public class documentlisteneradapter {      public static void main(string args[]) {         jframe frame = new jframe("abstracttextfield test");         final map attributes = (new font("serif", font.bold, 16)).getattributes();         attributes.put(textattribute.strikethrough, textattribute.strikethrough_on);                 final jformattedtextfield textfield1 = new jformattedtextfield(new float(10.01));         textfield1.setformatterfactory(new abstractformatterfactory() {             @override             public abstractformatter getformatter(jformattedtextfield tf) {                 numberformat format = decimalformat.getinstance();                 format.setminimumfractiondigits(2);                 format.setmaximumfractiondigits(2);                 format.setroundingmode(roundingmode.half_up);                 internationalformatter formatter = new internationalformatter(format);                 formatter.setallowsinvalid(false);                 formatter.setminimum(0.0);                 formatter.setmaximum(1000.00);                 return formatter;             }         });         final jformattedtextfield textfield2 = new jformattedtextfield(new float(10.01));         textfield2.setformatterfactory(new abstractformatterfactory() {             @override             public abstractformatter getformatter(jformattedtextfield tf) {                 numberformat format = decimalformat.getinstance();                 format.setminimumfractiondigits(2);                 format.setmaximumfractiondigits(2);                 format.setroundingmode(roundingmode.half_up);                 internationalformatter formatter = new internationalformatter(format);                 formatter.setallowsinvalid(false);                 //formatter.setminimum(0.0);                 //formatter.setmaximum(1000.00);                 return formatter;             }         });         textfield2.getdocument().adddocumentlistener(new documentlistener() {             @override             public void changedupdate(documentevent documentevent) {                 printit(documentevent);             }              @override             public void insertupdate(documentevent documentevent) {                 printit(documentevent);             }              @override             public void removeupdate(documentevent documentevent) {                 printit(documentevent);             }              private void printit(documentevent documentevent) {                 documentevent.eventtype type = documentevent.gettype();                 double t1a1 = (((number) textfield2.getvalue()).doublevalue());                 if (t1a1 > 1000) {                     runnable dorun = new runnable() {                         @override                         public void run() {                             textfield2.setfont(new font(attributes));                             textfield2.setforeground(color.red);                         }                     };                     swingutilities.invokelater(dorun);                 } else {                     runnable dorun = new runnable() {                         @override                         public void run() {                             textfield2.setfont(new font("serif", font.bold, 16));                             textfield2.setforeground(color.black);                         }                     };                     swingutilities.invokelater(dorun);                 }             }         });         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.add(textfield1, borderlayout.north);         frame.add(textfield2, borderlayout.south);         frame.setvisible(true);         frame.pack();     }      private documentlisteneradapter() {     } } 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -