How to make JComboBox display an item not in list?

hi,
i'd like a gui component which is basically a jcombobox that presents a list A,B,C,D.
The combobox is paired with another gui component which allows choosing sets of items. A etc.. represent preselected sets of items, so the user can click on A and a particular set would be selected.
My problem is what to have the JComboBox do when the user selects a set of items that is not in the preselected defaults list. It would be nice to have the JComboBox just say "Other" at the top or something, but I can't figure out a way of doing this without putting the String "Other" in the list of items..
does anyone have any idea of how to approach this? (I looked at making the JComboBox editable but this seems a little complicated?)
thanks,
asjf

Your best option and cleanest design is to have a
dummy data item in your list for "none of the above". thanks for the reply - this is what I'm doing at the moment. The problem is that I'm expecting testers to legitimately complain that selecting "None of the above" doesn't do anything, and doesn't really make sense ( it could make a random selection :o) )
i think i agree that adding functionality to do this "properly" is likely to be more work/pain than its worth..
Only an editable combobox can display something not in the list.this might help - if I could block mouse clicks to the editable area, and write to it programmatically but think this is probably quite difficult (?)
asjf

Similar Messages

  • How to make jcombobox display all by itlself?

    Hello all -
    Is there a way to make a jcombobox object display its column of items implicitly?
    For example, suppose a program has a combobox filled up with names. And, suppose the first item says "toggle sort", which affects how the items are sorted in the combobox (eg, pressing it should cause the list to sort the displayed names based on first name, another press bases the sort on the last name...). The behavior I am aiming for is that when the user presses the "toggle sort" item, the combobox displays its items again awaiting further selection from the user (but not the first item). Know how to fire such an event?
    private void namesComboBoxActionPerformed(ActionEvent evt) {
       int selected = namesComboBox.getSelectedIndex();
       if (selected == 0) {
          // some code should result in the namesComboBox displaying its items again. What is it please?
    }

    Here is a simple code, I don't know if its the correct way to do this sort of thing but it works:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Collections;
    import javax.swing.*;
    * Sorting Combobox....
    * @author talha
    public class ComboSort extends JFrame{
        ArrayList<ComboData> data;
        JComboBox combo;
        public ComboSort() {
            super("Combo Sorting");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            populateData();
            Collections.sort(data);
            // I don't know how much of the combobox model should be overridden!
            combo=new JComboBox(new DefaultComboBoxModel() {
                @Override
                public Object getElementAt(int index) {
                    return (index==0)?"Toggle Sort":data.get(index-1);
                @Override
                public int getSize() {
                    return data.size()+1;
                @Override
                public int getIndexOf(Object anObject) {
                    return (anObject instanceof String)?0:data.indexOf(anObject)+1;
            combo.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if(combo.getSelectedIndex()==0){
                        ComboData.toggleSort();
                        Collections.sort(data);
                        //edited to make the pop up visible....
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                combo.setPopupVisible(true);
            combo.setRenderer(new DefaultListCellRenderer() {
                @Override
                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                    Component renderer=super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                    if(value instanceof String){
                        renderer.setForeground(Color.BLUE);
                        renderer.setBackground(Color.YELLOW);
                    return renderer;
            // you can remove following....
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    combo.setSelectedIndex(0);
            getContentPane().setLayout(new FlowLayout());
            getContentPane().add(new JLabel("Heads of State:"));
            getContentPane().add(combo);
            setSize(300, 300);
            setLocationRelativeTo(null);
        private void populateData() {
            data=new ArrayList<ComboData>();
            data.add(new ComboData("Barak", "Obama"));
            data.add(new ComboData("Gordon", "Brown"));
            data.add(new ComboData("Nicolas", "Sarkozy"));
            data.add(new ComboData("Angela", "Merkel"));
            data.add(new ComboData("Wen", "Jiabao"));
            data.add(new ComboData("Taro", "Aso"));
            data.add(new ComboData("Manmohan", "Singh"));
            data.add(new ComboData("Asif", "Zardari"));
            data.add(new ComboData("Mahmoud", "Ahmedinejad"));
            data.add(new ComboData("Hugo", "Chavez"));
            data.add(new ComboData("Raul", "Castro"));
        public static void main(String args[]){
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new ComboSort().setVisible(true);
    class ComboData implements Comparable{
        String firstname;
        String lastname;
        static boolean sortFirstName;
        public ComboData(String firstname, String lastname) {
            this.firstname = firstname;
            this.lastname = lastname;
        @Override
        public String toString() {
            return firstname+" "+lastname;
        public static void toggleSort(){
            sortFirstName=!sortFirstName;
        public int compareTo(Object o) {
            ComboData other=(ComboData)o;
            if(sortFirstName)return firstname.compareTo(other.firstname);
            else return lastname.compareTo(other.lastname);
    }Thanks!
    Edit: Reread your original post and made few editions... but I still don't think that this is what you need (but now its much better) :-)
    Edited by: T.B.M on Apr 19, 2009 1:16 PM

  • Itunes - how do i select multiple line items not in sequence ?

    itunes - how do i select multiple line items not in sequence ?

    Hold down the Command key and click each in succession.
    To extend the selection across items that ARE contiguous, use the Shift key.

  • Hello I have a problem related to synchronization. When you synchronize ipad Section 3 "Other" takes as much memory up to 8 gigobayt. How to make the section "Other" did not increase? Thank you.

    Hello
    I have a problem related to synchronization.
    When you synchronize ipad Section 3 "Other" takes as much memory up to 8 gigobayt.
    How to make the section "Other" did not increase?
    Thank you.

    DOCUMENTS AND DATA IS ALSO WHERE YOUR MAIL INFORMATION IS STORED SO IF YOU ARE A "HOARDER" IN EMAIL, GO INTO THE GMAIL, HOTMAIL, ICLOUD ETC WHATEVER YOU USE, AND DELETE YOUR TRASH, SENT, DRAFTS, ETC.
    THAT SHOULD HELP A LOT.

  • How to make google come up and not yahoo when i click anew tab

    how to make google come up and not yahoo when i click a new tab
    == This happened ==
    Every time Firefox opened
    == today

    You can use 1 of these add-ons
    [https://addons.mozilla.org/en-us/firefox/addon/newtaburl/?src=search
    NewTabURL ]
    [https://addons.mozilla.org/en-us/firefox/addon/new-tab-homepage/?src=search New Tab Homepage]
    with NewTabUrl you have more options
    thank you
    Please mark "Solved" the answer that really solve the problem, to help others with a similar problem.

  • How to make selection parameter gray out, not changeble mode

    Hi Experts,
      Selection screen having parameters, I am putting default values in that parametrs.
    How to make this parameter gray out, not changeable mode.
    Thanks in advance
    mahahe.
    See comments below
    Edited by: Rob Burbank on Jul 1, 2009 12:43 PM

    Hi,
    For the parameters required, while declaring them use the MODIF ID, lets say you have choosen the modif id as SC1.
    i.e. parametes: p_mat type matnr DEFAULT '100' MODIF ID SC1.
    Then in the initialization event, code as below.
        LOOP AT SCREEN.
          IF screen-group1 = 'SC1'.
            screen-input = '0'.
            MODIFY SCREEN.
          ENDIF.
        ENDLOOP.
    It will work.
    Regards,
    Nangunoori.

  • How to make oracle is case insentive ( not case sensitive)

    SqlServer by default is NOT case sensitive (data content) while Oracle by default is case sensitive (data content).
    please help me to give the solution
    How to make oracle is case insentive ( not case sensitive)
    if there is anyway to make Oracle behave like SqlServer changing NLS settings or any other Oracle settings?
    I have solution for application level . it is working fine
    Possible solutions:
    1. You want to store data case insensitive: do an 'insert...
    values(upper('Abcd'))...'.
    This will put your data in upper case into the tables.
    2. Your data will be mixed-case and you want to select them case
    insensitive:
    create 'function based indexes' on every column you want to use in
    your where-clauses.
    Then 'select ... where <column_name> = 'ABCD'.

    What version of Oracle? Oracle 10g has a capability that can toggle this feature on and off at the session level. Prior versions do not.
    For prior versions you usually have to make use of the upper and/or lower functions in your queries. Sometimes a function based index, FBI, is used to handle this.
    HTH -- Mark D Powell --

  • How to limit the number of items that a list control can hold?

    Hi,
    I am using a Flex3 List control for one of my projects.
    I add drag & drop functionality to it, so that i can drag & drop elements from one control to another.
    How to limit the number of items that a list control can hold / can be dropped in a list control ?
    - Sen.

    1. Listen for the dragdrop event on the control you are dragging onto.
    2. check the length of the items in the dataprovider - if it exceeds your maximum override the default drop action with e.preventDefault(); (assuming you used e for the event on the handler).
    Regards,
    Mark.

  • How can i show the first item in the list as selected item

    Aslam o Alikum (Hi)
    Dear All
    How can i show the first item in the list as selected item when user click on the list. Right now when user click the list the list shows the last item in the list as selected or highlighted. Furthermore if the list item have large no of value and a scroll bar along with it then the list scroll to last item when user click it with mouse. I want that when user click the list item with mouse list should show the first item as highlighted.
    Take Care
    Allah Hafiz

    Hi!
    You can set list "initial value" using When-Create-Record trigger.
    I.g.
    :<Block_name>.<list_item_name> := Get_List_Element_Value('<Block_name>.<list_item_name>', 1);

  • How to have a JComboBox display an item different from the one selected

    Hello,
    I have a JTable (single column). I have set this column cell editor to be a JComboBox...
    JComboBox stdComboBox = new JComboBox();
    TableColumn column_zero = stdTablewCombo.getColumnModel().getColumn(0);
            column_zero.setCellEditor( new DefaultCellEditor(stdComboBox) );I have added a lovString array say {"A", "B", "C") to this stdComboBox. However, when the user selects a value from this list, I need to display a value from another lovString array say {"1", "2", "3"}. So if the user selects A then 1 needs to be displayed.
    I have a TableModelListener on this table that is getting me the correct selected index for any of the JComboBoxes on any of the rows. But I am little lost on how to display the value from the 2nd list.
    Thanks.

    Yes...but do I do that in the TableModelListener ?
    This is what I've tried in the TableModelListener:
    public void tableChanged(TableModelEvent e)
             System.out.println(e.getSource());
               if(e.getType() == TableModelEvent.UPDATE)
                    TableModel model = stdTablewCombo.getModel();
                    int row = e.getFirstRow();
                    this.cellValue = ((String)model.getValueAt(row, 0));
                    if (cellValue != null)
                         this.selectedIndex = this.stdComboBox.getSelectedIndex();
                          System.out.println("Selected Index:" + this.selectedIndex);
                          if (this.selectedIndex >= 0)
                             String valueToSet = this.lovStringB[this.selectedIndex];
                             model.setValueAt(valueToSet, row, 0);
                          else
                               System.out.println("Value in cell is not from List");
                          }It returns the selectedIndex value allright.
    It sets the value of valueToSet string allright.
    it also does the model.setValueAt allright....but this triggers a tableChanged event and it goes back up - this is also fine. However, when it now comes back to the this.selectedIndex = , this again returns the index of the original selection and not a -1 (for values not in the ComboBox list). This makes it go into an infinite loop and gives me a StackOverflowError.
    Edited by: 799076 on Oct 10, 2010 9:41 AM
    Edited by: 799076 on Oct 10, 2010 9:49 AM

  • The window displaying bookmarks is too large (suddenly happened with the new version). I can't figure out how to make the display smaller. Zooming out does not work on the bookmark menu display.

    I have version 4, just downloaded, and a Mac w. OS 10.6. 7.
    The bookmarks display (when I pull down "bookmarks") is much too wide. I can't figure out how to reduce the display size. The window is not persistent so I can't even get you a screen shot.
    Thanks, Sara

    It seems there was a change in Firefox 4 where the width of the drop-down is now determined by the length the longest bookmark name in the folder being displayed. Previous versions of Firefox would shorten a long name like that and add an ellipsis to signify the name was truncated.
    Sorry, I don't know how to change that other than to advise you to edit the name of that long bookmark name and make it less verbose. Just scroll down the list of bookmarks in that wide folder until you see the culprit, then right-click that bookmark and open Properties & edit the Name line at the top of that dialog.

  • How to make the display of custom column names in UWL(not bother abt values

    Hi all
    i want to make the display of custom column names in UWL ( for example GP)
    i dont want or not looking about values for those custom columns . i know that thru custom connector we can achieve to retrieve the values for those custom columns. but currently i am looking only just to display the custom column names( for example GP custom column names, just i want to display the name of the columns ) in UWL .
    Thanks
    Sunil

    Hello Prashant,
    You can add the control to your custom pagelayout, the following article has an example:
    http://spandps.com/2012/02/22/showing-the-audience-target-field-in-an-editmodepanel-sharepoint-sp2010-in-projectserver-ps2010/
    Btw, the SPFieldTargetToControl has a required property:
    http://msdn.microsoft.com/en-us/library/microsoft.office.server.webcontrols.fieldtypes.spfieldtargettocontrol.required(v=office.14).aspx
    - Dennis | Netherlands | Blog |
    Twitter

  • How to make disable/enable single item recovery effectively Immediately?

    for the following command , "it may take up to one hour to disable single item recovery",my question is how to make the disable/enable  single item recovery
    effectively Immediately by some command?
    SingleItemRecoveryEnabled $false/true
    http://technet.microsoft.com/en-us/library/ff678798(v=exchg.141).aspx
    Please click the Mark as Answer button if a post solves your problem!

    other options:
    Move the mailbox to another DB or if in a DAG, activate the DB on another server. ( the activating seems to work as far as I have seen at least).
    Twitter!: Please Note: My Posts are provided “AS IS” without warranty of any kind, either expressed or implied.

  • How to make a listbox selected item highlight color from code the same as if user click on item?

    Hi all,
    I have a listbox that gets filled with data when my program starts. I save the last selectedItem value so I set it when the program start again. But the color that the selected item has is not the same as if the user click on it (pictures below).
    The color when I set the selectedIndex=someItem is black on light grey (background)
    The color when the user clicks on the item is white on blue (background)
    Any way to make them both white on blue (background)?
    Thanks
    This is the user clicking on it:
    This is the code:

    I think the difference is focus.  When you use the mouse to select it also sets focus to the listbox so you would have to set focus to the listbox when you set the selected item.  How are you setting your selected item?  I think that by
    using a property to hold the selected item rather than setting the index that might also fix the problem.
    Lloyd Sheen

  • Display line items not possible!

    Hello
    I've a very strange situation on the productive system:
    I have a company code that registers and display account line items, while the second cannot display them, even if I've configurated the same aspects in account fields for the two company codes. That means they have the same
    - flag on display line items  AND
    - sort key  001 - POSTING DATE
    why??
    thanks a lot in advance,
    Chiara

    No, the answer was not that one, but thanks anyway.
    Finally I found the answer in
    Forum:  Message MSITEM033 in New GL - Cannot Display GL Line Items
    note OSS:
    -  1323960   Incorrect entry '/' in SKB1-XLGCLR and BSEG-XLGCLR
    -  1149500  Clearing open items by batch input selection
    -  1437341   Note 1437341 - Correction of BSEG/Index tables after note 1323960
    regards,
    Chiara

Maybe you are looking for

  • Using Runtime.exec() on Unix (AIX)

    I am attempting to use exec() on the server side to move files to a a windows share via Samba. I'm getting 2 resutls: If I run the code with exec(string) the server/share infrormation is not passed and Samba attempts to connect to ' '. I'd imagine th

  • In ME2L transaction, Still to be delivered Qty showing after Short Closed

    Hi, We have a PO with 50 qty pending, which is showing in ME2L transaction. Now we want to short close this PO. Through ME22N transaction, we has short closed this PO. But till now, in ME2L Still to be delivered qty showing 50. How can we see the Ope

  • MQ Transport In OSB

    Hi Experts, I am basically trying to configure MQ in OSB. Below link was used to configure MQ in TCP mode. http://docs.oracle.com/cd/E17904_01/doc.1111/e15866/mq.htm There are actually 2 modes. viz. 1) TCP Mode  2) Binding Mode. I prefer to connect t

  • What version of code will prevent Roaming Issues?

    I have three 1230G AP's in an Emergency Room environment and have wireless carts that move throughout the area. The Three APs are running 12.3(2)JA2, are on channels 1, 6, & 11, and the clients are using the Cisco 350 cards. Only WEP is enabled. We s

  • RRI warnings

    Hi, I looked at the forums on SDN and SAP library for my issue but I still have questions. I created a sender/receiver assignment for a query. The receiver is transaction va03 display sls order in our R/3 production system, and the infosrc is 2LIS_11