Tuesday, 15 May 2012

swing - Java - How to show an input dialog having a dropdown list with an icon for each item? -


I'm using this Java code in my swing application to show an input dialog, in which a drop-down selection list so that a user can select an item from the list:

  string [] carModelsArray = { "Honda", "Mitsubishi", "Toyota"}; String Cynitavlu = (string) Jovpshnpenksoinputdialaog (null, "Choose a car model from the list went down", "car model ...", Jopshnpen. QUESTION_MESSAGE, blank, car Modelare, Karmoles array [0]);  

This code works fine, but I was wondering if I can also add an icon for each item in the selection list, so the drop-down selection list will look like this. :

Enter image details here

I have an item in it Attempted to set list as jlabal item, but jelabell objects were converted to all string values, when the drop-down list is in the list Is given as if it asks the JLabel.toString () method for each item in the list to get its value.

Is there any way to accomplish this?

A short answer, the way you are not doing it, long answer, something else Like ... Enter image details here

  import java.awt .component; Import java.awt.EventQueue; Import javax.swing.DefaultComboBoxModel; Import javax.swing.DefaultListCellRenderer; Import javax.swing.Icon; Import javax.swing.ImageIcon; Import javax.swing.JComboBox; Import javax.swing.JList; Import javax.swing.JOptionPane; Import javax.swing.UIManager; Import javax.swing.UnsupportedLookAndFeelException; Import org.kaizen.icons.AddIcon; Import org.kaizen.icons.DeleteIcon01; Import org.kaizen.icons.DeleteIcon02; Public class test {public static zero main (string [] args) {new test (); } Public Test () {EventQueue.invokeLater {@ Override Public Wired Run () {{UIManager.setLookAndFeel {}} (Clasnostfound Exception | Instantation | Invalid Access Exception | Unsupported Lock and Eiffel Expectation Ex) {ex. PrintStateCress ();} DefaultComboBoxModel Model = New DefaultComboBoxModel & lt; Cars & gt; (); Model AddElement (new car (new AddIcon (16, 16), "Honda"); Modl.adeliment (new car (new Htaiaks 101 (16, 16), "Mitsubishi"); Modl.adeliment (new car (new Htaiink 02 (16 16), "Tayota")); Jesimboboks cb = new Jesimboksboks (model); Sibi.asetrenderr (new Karlist Rilenderr ()); Int result = Jopshnpenkso confirmation dialog (one blank, CB, "the list went down car Select model ", Jopshnpenkokankel_l_ OPTION, JOptionPane.QUESTION_MESSAGE); if (result == Yopshnpenkoke_op Itian) {car = (car) Sibikget selected items ();}}});} public static class CarListCellRenderer default ListCellRenderer {@Override public component getListCellRendererComponent (JList & lt ;? & Gt; list, Object value, Int Index (Boolean cellophobia); {System.out.println (value); Super.getListCellRendererComponent (list, value, index, selected, cellophobia); if (value car for example) {car car = (car) value; SetIcon (car.getIcon ()); setText (car.getText ());} other {set icon (empty);} return it;}} public key Tetik class Car {private icon; Private string text; Public car (icon icon, string text) {this.icon = icon; This.text = text; } Public icon getIcon () {return icon; } Public string getText () {return text; }}}  

In fact, you have to control the combobox and provide your own custom ListCellRedender which can provide the expected output for you ...


No comments:

Post a Comment