java - How to change the (displayed) selected item on a disabled JComboBox? -


i'd change displayed selected item on disabled jcombobox programmatically. tried

  • enabling before invoking setselecteditem , disabling right after
  • the former , invoking updateui before disabling

it might isn't intended, save me work replace combobox jlabel, dirty hack answer appreciated well.

well, seems work okay me...

import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class test {      public static void main(string[] args) {         new test();     }      public test() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                     ex.printstacktrace();                 }                  jframe frame = new jframe("testing");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.add(new testpane());                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);             }         });     }      public class testpane extends jpanel {          public testpane() {             setlayout(new gridbaglayout());             gridbagconstraints gbc = new gridbagconstraints();             gbc.gridwidth = gridbagconstraints.remainder;              jcombobox cb = new jcombobox(new string[]{                 "one", "two", "three", "four", "five"             });             cb.setenabled(false);             add(cb, gbc);             cb.setenabled(false);              jbutton btn = new jbutton("update");             add(btn, gbc);              btn.addactionlistener(new actionlistener() {                 @override                 public void actionperformed(actionevent e) {                     cb.setselecteditem("five");                 }             });         }      }  } 

make sure object using equal value in jcombobox


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -