JComboBox question

i am using a JComboBox in a JPanel all goes well and it is doing exactly want iwant it to do,
there is one little thing that is bad though, when the JComboBox closes itselft after a selection has been made, the surface that was beneath the expended JComboBox is just not showing anymore.... instead of the components, the background color is all that is showing...
any one knows how to prevent this or how to make it refresh what it did?
i have tried to put inside the itemStateChanged() the following:
myJFrame.validate();
or
myFrame.doLayout();
myFrame.validate();
or
myFrame.repaint();
but nothing seems to work
any help would be appreciated, thx
PoPo

yes , and the action happens properly
heres the code :
public void itemStateChanged( ItemEvent event )
if(event.getSource() == JComboBoxEssNomComm)
if(test == 0)
JLabelEssCouranteReponse.setText((JComboBoxEssNomComm.getSelectedItem()).toString());
test++;
else
JLabelEssCouranteReponse.setText((JComboBoxEssNomComm.getSelectedItem()).toString());
// insert refresh code here
those actions do happen properly ... but no refresh will work....

Similar Messages

  • JComboBox QUESTION - Listener Methods.

    Dear Everyone,
    At the moment I am working on a UI that utilises four JComboboxes. Each of the four JComboboxes contains an index of 10 numeric values, namely 0 - 9. I intend on using the concatenated values of all four boxes to represent a four digit year value, e.g.: 1583, 1902, 2050, 9504, etc.
    I need to write some code that listens to each of the four JComboboxes and places the user selected number (from the JComboboxes index) into a variable.
    Once the values of all four JComboboxes are held in four variables I then need to concatenate the variables so that they represent a single year, e.g.: 1583.
    Once I have the concatenated value I will then perform relative mathematical operations.
    If anybody has any suggestions as to how I can listen to the JComboboxes and then place the selected index values into variables they will be greatly appreciated.
    Thanks

    How about this:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    public class Jconbo extends JFrame
         Vector v       = new Vector();
         JComboBox jc1  = new     JComboBox(v);
         JComboBox jc2  = new     JComboBox(v);
         JComboBox jc3  = new     JComboBox(v);
         JComboBox jc4  = new     JComboBox(v);
         int       year = 0;
    public Jconbo()
         addWindowListener(new WindowAdapter()
        {     public void windowClosing(WindowEvent ev)
              {     dispose();
                   System.exit(0);}});
         for (int j=0; j < 10; j++) v.add(""+j);     
           setBounds(1,1,500,300);
         addJc(jc1);
         addJc(jc2);
         addJc(jc3);
         addJc(jc4);
         getContentPane().setLayout(new FlowLayout());
         setVisible(true);
    private void addJc(final JComboBox jc)
         getContentPane().add(jc);
         jc.setPreferredSize(new Dimension(100,24));
         jc.setSelectedIndex(0);
         jc.addItemListener( new ItemListener()
         {     public void itemStateChanged(ItemEvent e)
                   year = jc1.getSelectedIndex() * 1000 +
                           jc2.getSelectedIndex() * 100 +
                           jc3.getSelectedIndex() * 10 +
                           jc4.getSelectedIndex();           
                   System.out.println(""+year);
    public static void main (String[] args) 
         new Jconbo();
    Noah
    unformatted
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    public class Jconbo extends JFrame
         Vector v = new Vector();
         JComboBox jc1 = new     JComboBox(v);
         JComboBox jc2 = new     JComboBox(v);
         JComboBox jc3 = new     JComboBox(v);
         JComboBox jc4 = new     JComboBox(v);
         int year = 0;
    public Jconbo()
         addWindowListener(new WindowAdapter()
    {     public void windowClosing(WindowEvent ev)
              {     dispose();
                   System.exit(0);}});
         for (int j=0; j < 10; j++) v.add(""+j);     
         setBounds(1,1,500,300);
         addJc(jc1);
         addJc(jc2);
         addJc(jc3);
         addJc(jc4);
         getContentPane().setLayout(new FlowLayout());
         setVisible(true);
    private void addJc(final JComboBox jc)
         getContentPane().add(jc);
         jc.setPreferredSize(new Dimension(100,24));
         jc.setSelectedIndex(0);
         jc.addItemListener( new ItemListener()
         {     public void itemStateChanged(ItemEvent e)
                   year = jc1.getSelectedIndex() * 1000 +
                        jc2.getSelectedIndex() * 100 +
                        jc3.getSelectedIndex() * 10 +
                        jc4.getSelectedIndex();           
                   System.out.println(""+year);
    public static void main (String[] args)
         new Jconbo();
    }

  • Another JComboBox question

    How do I get the text that has been typed in an editable JComboBox? When I looked inside the source for the class, I didnt even see a JTextField anywhere.
    Thanks

    The same way you'd get it if it wasn't typed in....

  • JComboBox question (size of entry)

    I have a JComboBox that I am populating with a wide varying length of entries and in order not to mess up my GUI I need to constrain it to a certain size. This works but the drop down menu that is shown when the user clicks on the combo box stays the same width... I was hoping (and still holding out) that there is a way for it to be only the part on the GUI be constrained and have it expand so you can read the entire entry when the user opens up the combobox... This is how it is in web environments so does anyoen know a way I can accomplish this?

    I had these results from searching forum on the combobox horizontal scrollbar keywords:
    http://onesearch.sun.com/search/developers/index.jsp?col=devforums&qp_name=Swing&qp=forum%3A57&qt=combobox+horizontal+scrollbar
    Hope that helped,
    Regards.

  • JComboBox Length question!

    When i use JComboBox,it's length is fixed.Perhaps some items is too long to display,so user can't understand it clearly.So i want find JComboBox that can expand automaticly,if item is long,it will be expanded.I tried several function JComboBox provided,but it's remain as usral.I don't know if some master-hand meets same question! Thanks a lot!

    If you want the List wider, look [url http://www2.gol.com/users/tame/swing/examples/SwingExamples.html]here
    If you want the whole combo wider, use
    myCombo.setMinimumSize(new Dimension(200,20));
    It's also possible to calculate the width of string in a certain font (to plug in above), but you can search for that.

  • JComboBox simple question

    I think is a very simple question, I ve got a JComBox with an array of Strings. Something like this:
    JPanel myPanel;
    JComBox box;
    String levels[]={"Level 1","Level 2","Level 3","Level 4","Level 5"};
    box = new JComboBox (levels);
    myPanel.add(box);
    Ok. My question is: How can I let just the level 1 enable at first, or how can I enable or disable just one level of the JCombox, like level 1 and 2 enable but level 3, 4 and 5 disable.
    Thanks a lot!! Hope someone helps me!!

    I don't think this is possible with a JComboBox. I believe it is with a JMenu and JMenuItems.
    You could only display valid options by adding/deleting them. Or you could handle it in the listener code. ie if they select an invalid option do nothing. This would require you keeping track of which options are valid elsewhere in the code.

  • Question about JComboBox

    Hello, I've a little problem with a JComboBox. I'm going to explain what's going wrong:
    I have a MySQL database with a table "Swimmers" (id, name, address,..). I want to show the swimmers in a JComboBox and that's no problem. I just get the id + name of all the swimmers with SQL and put their name into the JComboBox with addItem().
    But when I click on a button "Modify", the user can modify the swimmer he selected in de JComboBox. So it's important that there is a link between the items/swimmers in the JComboBox and the id of the swimmer. And that's my problem...
    With getSelectedIndex(), I can get the index of the selected item. But I don't want to get the index, I want to get the ID of the selected swimmer. So the ID of the selected swimmer in the JComboBox.
    So, a litle example:
    In my database, I have the following items:
    ID                    Name                                Address
    ========================================================
    1                     Jos Vermeulen                   Apenstraat 54, Brussel
    4                     Geert De Greef                   Kerkstraat 1, Antwerpen
    8                      Sandra Geeroms                Blastraat 45, GentThe dropdown likes has the following items:
    Jos Vermeulen      (at index 0)
    Geert De Greef      (at index 1)
    Sandra Geeroms    (at index 2)For example: I select "Geert De Greef" and click on the button "Modify". Than the program has to know that the ID of "Geert De Greef" is 4. So I have to add the ID as a value in the JComboBox.. But how?
    Do you understand what I mean?
    I have to know the ID (in the database) of the selected item.. How can I get it?
    Edited by: geertgreef on Apr 28, 2009 4:17 AM

    How do I have to add the array into the JComboBox?Its no different using an array of Swimmers than it would be using an array of Strings. So just try it.
    An how can I "decide" that only the string that is returned by method toString() has to be visible?Thats the way the default renderer works. Read the Swing tutorial on "How to Use Combo Boxes".
    Again just try it. If you have a problem then you post a question. Don't assume you will have a problem.
    If you need further help then you need to create a [Short, Self Contained, Compilable and Executable, Example Program (SSCCE)|http://homepage1.nifty.com/algafield/sscce.html], that demonstrates the incorrect behaviour.
    Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.

  • JComboBox, ItemListener, ActionListener questions

    Hallo,
    I have some troubles with a JComboBox:
    I add an ActionListener and an ItemListener to it.
    1.ActionListener problems: if the combo is editable then entering value in the editor and clicling Enter fires 2 actionEvents
    with the combo as source. The difference I noticedbetween them is the actionCommand value:
    "comboBoxChanged"(as from normal select) and "comboBoxEdited"(because of changed value in editor)
    2. ItemListener problem: everything is OK selecting with mouse but if I type text/editable again/ and press enter only the
    ActionListener is called(yes, KeyListener in editor will help). The users also want to point a value with the mouse and press enter on it.
    Again only the action is called.
    Now the questions:
    1.Can I rely on the texts in actionCommand? What should I compare them to if "Yes"/are they localized?/
    2. Is there an easier way then adding a KeyListener to the editor and list component of the combo to catch the Enter if ActionListener cannot be used?
    Thanks in advance
    Mike

    About my question 1: in source of JComboBox these text values seem to be hard-coded.
    So I will use them for now. But I still would appreciate other ideas.
    Thanks
    Mike

  • Retrieving values from a JComboBox - Design question.

    I would like some design guidance on a problem that I am hoping has been solved before. Here is my situation:
    I have a JComboBox that I populate with String values from a database table. The exact set of values to be loaded into the JComboBox varies according values specified elsewhere on the GUI.
    When I select an item from the JComboBox, I need to read the database to retrieve more information. The text is not sufficient for me to identify the data I need, so I need to get the table key from somewhere.
    Is there anyway I can associate my table key with the text value inside the JComboBox and retrieve it when the user selects a drop down value from the JComboBox?
    Many thanks in advance.

    when you load the data from the db, try to get ALL the information needed: item label+item value+description. put this data into a map (a hashmap) for example using a unique identifier. For example, use a numeric index. In this case, the item value should be the index that uniquely identifies your items.
    create a simple bean that encapsulates the item contents: index+value+label, description.
    Doing this will avoid the huge db access occurences.
    hth

  • JCombobox Renderer Question

    I am trying to use a custom renderer for a jcombobox in order to show combobox items in different font color. I get a DefaultListCellRenderer and update it according to my needs (setting font color). When I run the application, the combobox is populated properly. When I select one of the items (say "Red") from the combobox, the item gets selected, but the after the selection the font color of the item that is selected defaults to black (instead of red here). What should I do so that after selecting the item, the selected item is shown in the appropriated font color as indicated by custom renderer.
    Here is the code for the custom renderer.
        private class ColorCmbRenderer implements ListCellRenderer {
            protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
            private final Dimension preferredSize = new Dimension(50, 23);
            public Component getListCellRendererComponent(JList list, Object value,
                                                        int index, boolean isSelected,
                                                        boolean cellHasFocus) {
            JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index,
                                                        isSelected, cellHasFocus);
                switch (index) {
                    case 0: renderer.setText("Black");
                            renderer.setForeground(Color.BLACK);
                            break;
                    case 1: renderer.setText("Red");
                            renderer.setForeground(Color.RED);
                            break;
                    case 2: renderer.setText("Blue");
                            renderer.setForeground(Color.BLUE);
                            break;
                    case 3: renderer.setText("Green");
                            renderer.setForeground(Color.GREEN);
                            break;
                    default: break;
                renderer.setPreferredSize(preferredSize);
                return renderer;
        }Another thing that I wanted to know is whether each of the items in the jcombobox can be associated with individual Action objects by using something like setAction(new HTMLEditorKit.BoldAction()).
    regards,
    Nirvan.

    There are 3 foregrounds to be set.import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import javax.swing.*;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;
    public class ComboColors {
       JComboBox comboBox;
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                new ComboColors().makeUI();
       public void makeUI() {
          comboBox = new JComboBox(
                new Object[]{"BLACK", "RED", "BLUE", "GREEN"});
          comboBox.setRenderer(new BasicComboBoxRenderer() {
             private final Color[] colors = {
                Color.BLACK, Color.RED, Color.BLUE, Color.GREEN
             @Override
             public Component getListCellRendererComponent(JList list, Object value,
                   int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value,
                      index, isSelected, cellHasFocus);
                if (index == -1) {
                   index = comboBox.getSelectedIndex();
                setForeground(colors[index]);
                list.setSelectionForeground(colors[index]);
                comboBox.setForeground(colors[index]);
                return this;
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(comboBox);
          frame.add(new JButton("Dummy"), BorderLayout.SOUTH);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
    }db

  • JComboBox CellEditor and setValueAt() question

    I have created a JTable that uses a unique JComboBox cell editor for each cell in a column. This works well visually, but now those cells do not seem to use the setValueAt() method in my table model. I was using this to capture the data for the database. What is the correct way to find what the data within those combo has been changed to?? All help is greatly appreciated.

    I have created a JTable that uses a unique JComboBox cell editor for each cell in a column. This works well visually, but now those cells do not seem to use the setValueAt() method in my table model. I was using this to capture the data for the database. What is the correct way to find what the data within those combo has been changed to?? All help is greatly appreciated.

  • Question for BasicComboBoxEditor use in JComboBox  How can I get right ce

    I use a JComboBox as the editor for JTable.Now I met a problem In use object that BasicComboBoxEditor as the editor for JComboBox that in JTable.
    I can't get the cell data rightly.I can't get the editing row and column when system call getEditorComponent() . Mouse event process is after getEditorComponent().
    How can I get right cell data when then getEditorComponent calling?

    Sorry,I am english is poor.
    I use a component of JComboBox as editor of JTable. then
    I want to konw that how can I get right data in a cell of JTable when table enter editing

  • Question about refreshing values in JComboBox

    i have a JComboBox that points to a static array of Objects.
    I am adding an element to this array at some point, but when i do the JComboBox goes blank! All the entries are blank. If I click inside the blank box it will eventually refresh, but it's really annoying.
    What can i do to make this addition of an element happen without the box going blank?
    thanks!

    my goal is to have a Vector that gives the values for a JList and several JComboBoxes, and to be able to add Objects to it and have each of the aforementioned JComponents update themselves appropriately when those additions are received!
    One <tt>JList</tt> and more than one <tt>JComboBox</tt>?
    -- construct a <tt>DefaultComboBoxModel dataModel</tt> with the <tt>Vector</tt>
    -- construct the <tt>JList</tt> with the model.
    -- construct the <tt>JComboBox</tt>es with the <tt>Vector</tt>
    -- add a <tt>PopupMenuListener</tt> to each <tt>JComboBox</tt> that fires the combo's model's <tt>ListDataListener</tt>s in the <tt>popupMenuWillBecomeVisible(...)</tt> method.
    -- add the elements to the model.
        Vector<String> data = new Vector<String>();
        data.add(...);
        DefaultComboBoxModel dataModel = new DefaultComboBoxModel(data);
        JList list = new JList(dataModel);
        PopupMenuListener listener = new PopupMenuListener() {
          @Override
          public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            JComboBox combo = (JComboBox) e.getSource();
            DefaultComboBoxModel model = (DefaultComboBoxModel) combo.getModel();
            ListDataEvent lde = new ListDataEvent(model,
                    ListDataEvent.CONTENTS_CHANGED, 0, model.getSize());
            for (ListDataListener listener : model.getListDataListeners()) {
              listener.contentsChanged(lde);
          @Override
          public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
          @Override
          public void popupMenuCanceled(PopupMenuEvent e) {
        JComboBox combo1 = new JComboBox(data);
        combo1.addPopupMenuListener(listener);
        JComboBox combo2 = new JComboBox(data);
        combo2.addPopupMenuListener(listener);
        dataModel.addElement(...);db

  • JComboBox listener question

    Hi,
    I was wondering what kind of listener I would use to detect enter being pressed in a JComboBox.
    I tried an actionListener but this sends actionEvents at far too many times, I want to limit what I'm doing to only when the user presses enter in the JComboBox.
    It seems rather odd that a keyListener can't be used with a JComboBox, or at least according to the API specifications...
    All help is very appreciated!!

    The JComboBox itself just has the text field and then the down arrow, correct? Like if I am writing in that text field I am writing in the JComboBox? Basically all i need is a way to do something when the user presses 'enter', but not when all the other various things that trigger ordinary action events are fired. Using a keyListener to find out whether the key pressed was 'enter' seemed like the best way to do this, but if key events aren't even getting sent when the focus is in the text portion of the JComboBox that may not be the case. If there's anyway i can explain the problem better I'd be happy to do so.
    Thanks

  • JComboBox newbie question

    I cant seem to find out how to add items to my jcombobox using Forte.
    I have this :
    String[] dbId = { "579", "459", "789", "098", "297" };
    jComboBox1.addItem(dbId);
    Where do I put this code to fill the combo box with the String array?
    Steve

    Depends on what application you are building.
    If you are building a standalone application, put those codes in the constructor of your application class.
    If you are building an applet, put those codes in the init() method of your applet.

Maybe you are looking for

  • My macbook pro doesn't communicate with video projectors anymore

    Since a while, I cannot use my macbook for video presentations anymore. The video beamer doesn't "see" my laptop, and in the display screen, no other screens are indicated. Anybody out there who can help?

  • Data load taking very long time between cube to cube

    Hi In our system the data loading is taking very long time between cube to cube using DTP in BI7.0, the maximum time consumption is happening at start of extraction step only, can anybody help in decreasing the start of extraction timing please Thank

  • New Bookmarks Screen

    On the new safari update when you go to the Bookmarks screen, the top is a large black area showing the latest pages I visited. I hate it, and want to get rid of it. No use to me. How do I get rid of it?

  • Fluid Grid Layout- HTML5 video buttons become gray squares Android

    I am attempting to use Fluid Grid layouts for a simple smartphone/tablet acessible video page.  I am in Dreamweaver CS6 Cloud (updated).  The buttons for play and full screen mode are shown as gray squares on my Android phone. It works fine in other

  • Re-installation of Leopard

    Hi, I am having some issues with programs like Safari and Firefox not opening and closing properly. It has been suggested to me to do an Archive and Install as I just upgraded from Tiger previously. I have a few questions: 1) Is this the right course