Editable JComboBox in a JTable

I posted my problem into the "Java Essentials/Java programming" forum but it would be much better if I'd posted it here into the swing theme :).
Here it is what I posted:
Funky_1321
Posts:8
Registered: 2/22/06
editable JComboBox in a JTable
Oct 21, 2006 1:32 PM
Click to email this message
Hello!
Yesterday I posted a problem, solved it and there's another one, referring the same theme.
So it's about a editable JComboBox with autocomplete function. The combo is a JTables cellEditors component. So it's in a table. So when the user presses the enter key to select an item from the JComboBox drop down menu, I can't get which item is it, not even which index has the item in the list. If for exemple the JComboBox isn't in a table but is added on a JPanel, it works fine. So I want to get the selectedItem in the actionPerformed method implemented in the combo box. I always get null instead of the item.
Oh... if user picks up some item in the JComboBox-s list with the mouse, it's working fine but I want that it could be picked up with the enter key.
Any solutions appreciated!
Thanks, Tilen
YAT_Archivist
Posts:1,321
Registered: 13/08/05
Re: editable JComboBox in a JTable
Oct 21, 2006 1:55 PM (reply 1 of 2)
Click to email this message
I suggest that you distill your current code into a simple class which has the following attributes:
1. It can be copied and pasted and will compile straight out.
2. It has a main method which brings up a simple example frame.
3. It's no more than 50 lines long.
4. It demonstrates accurately what you've currently got working.
Without that it's hard to know exactly what you're currently doing, and requires a significant investment of time for someone who hasn't already solved this problem before to attempt to help. I'm not saying that you won't get help if you don't post a simple demo class, but it will significantly improve your chances.
Funky_1321
Posts:8
Registered: 2/22/06
Re: editable JComboBox in a JTable
Oct 21, 2006 2:11 PM (reply 2 of 2)
Click to email this message
Okay ... I'll write the code in short format:
class acJComboBox extends JComboBox {
  public acJComboBox() {
    this.setEditable(true);
    this.setSelectedItem("");
    this.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) {
     // ... code code code
     // that's tricky ... the method getSelectedItem() always returns null - remember; the acJComboBox is in a JTable!
     if (e.getKeyCode() == 10 && new String((String)getEditor().getItem()).length() != 0) { getEditor().setItem(getSelectedItem()); }
}And something more ... adding the acJComboBox into the JTable:
TableColumn column = someTable.getColumn(0);
column.setCellEditor(new DefaultCellEditor(new acJComboBox());
So if the user presses the enter key to pick up an item in the combo box drop down menu, it should set the editors item to the selected item from the drop down menu (
getEditor().setItem(getSelectedItem());
When the acJComboBox is on a usual JPanel, it does fine, but if it's in the JTable, it doesn't (getSelectedItem() always returns null).
Hope I described the problem well now ;).

Okay look ... I couldn't write a shorter code just for an example. I thought that my problem could be understoodable just in mind. However ... type in the first combo box the letter "i" and then select some item from the list with pressing the enter key. Do the same in the 2nd combo box who's in the JTable ... the user just can't select that way if the same combo is in the JTable. Why? Thanks alot for future help!
the code:
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
class Example extends JFrame {
    private String[] columns = { "1", "2", "3" };
    private String[][] rows = {};
    private DefaultTableModel model = new DefaultTableModel(rows, columns);
    private JTable table = new JTable(model);
    private JScrollPane jsp = new JScrollPane(table);
    private acJComboBox jcb1 = new acJComboBox();
    private acJComboBox jcb2 = new acJComboBox();
    public Example() {
        // initialize JFrame
        this.setLayout(null);
        this.setLocation(150, 30);
        this.setSize(300, 300);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        // initialize JFrame
        // initialize components
        Vector<String> v1 = new Vector<String>();
        v1.add("item1");
        v1.add("item2");
        v1.add("item3");
        jcb1.setData(v1);
        jcb2.setData(v1);
        jcb1.setBounds(30, 30, 120, 20);
        jsp.setBounds(30, 70, 250, 100);
        add(jcb1);
        add(jsp);
        TableColumn column = table.getColumnModel().getColumn(0);
        column.setCellEditor(new DefaultCellEditor(jcb2));
        Object[] data = { "", "", "" };
        model.addRow(data);
        // initialize components
        this.setVisible(true);
    public static void main(String[] args) {
        Example comboIssue = new Example();
class acJComboBox extends JComboBox {
    private Vector<String> data = new Vector<String>();
    private void init() {
        this.setEditable(true);
        this.setSelectedItem("");
        this.setData(this.data);
        this.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) {
                    if (e.getKeyCode() != 38 && e.getKeyCode() != 40) {
                        String a = getEditor().getItem().toString();
                        setModel(new DefaultComboBoxModel());
                        int st = 0;
                        for (int i = 0; i < getData().size(); i++) {
                            String str1 = a.toUpperCase();
                            String tmp = (String)getData().get(i).toUpperCase();
                            if (str1.length() <= tmp.length()) {
                                String str2 = tmp.substring(0, str1.length());
                                if (str1.equals(str2)) {
                                    DefaultComboBoxModel model = (DefaultComboBoxModel)getModel();
                                    model.insertElementAt(getData().get(i), getItemCount());
                                    st++;
                        getEditor().setItem(new String(a));
                        JTextField jtf = (JTextField)e.getSource();
                        jtf.setCaretPosition(jtf.getDocument().getLength());
                        hidePopup();
                        if (st != 0 && e.getKeyCode() != 10 && new String((String)getEditor().getItem()).length() != 0) { showPopup(); }
                        if (e.getKeyCode() == 10 && new String((String)getEditor().getItem()).length() != 0) { getSelectedItem(); }
                        if (new String((String)getEditor().getItem()).length() == 0) { whenEmpty(); }
        this.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) {
            hidePopup();
    // - constructors -
    public acJComboBox() {
        init();
    public acJComboBox(Vector<String> data) {
        this.data = data;
        init();
    // - constructors -
    // - interface -
    public void whenEmpty() { } // implement if needed
    public void setData(Vector<String> data) {
        this.data = data;
        for (int i = 0; i < data.size(); i++) {
            this.addItem(data.get(i));
    public Vector<String> getData() {
        return this.data;
    // - interface -
}

Similar Messages

  • JTable with editable JComboBoxes

    For some reason once I added editable JComboBoxes to a JTable I can no longer tab among the fields in the table. If I just select a cell, I can tab to the other cells in that row, and then to proceeding rows as I reach the end of each. However if I double click the cell and enter data or select from the combobox. I no longer can tab to the next cell. At that point once I do hit tab, I am taken to the next table. Not the next cell in the row, or the next row in the current table.
    I have tried messing with a bunch of listeners, but I am not using the right listener on the right object? What listerner and object should the listener belong to? Table, model, cell editor, combobox, jpanel, jframe ? I hope this is not to vague.

    Well I am starting to think it's an implementation issue. Like I am not doing it how I should. I am basically doing
    JComboBox jcb = new JComboBox();
    jcb.setEditable(true);
    column.setCellEditor(new DefaultCellEditor(jcb));
    When I think I should be inheriting, and creating my own editor and/or renderer? Now my combobox vars are globally available in the class, so I can set and clear their contents. It does not matter that each row in the table has the same content in the combo boxes, that's basically what I want. User to be able to select an existing entry or make a new one.
    What I am trying to do is on tables have a drop down box that shows up when a user types a matching entry. If no entry matches, no popup box. If entry matches, box pops up and shows possible entries starting with what they are typing. Like code completion in IDE's like Netbeans. However I am doing this in a table. I thought a JComboBox would be a good widget to start with?

  • Problem using an editable JComboBox as JTable cell editor

    Hi,
    i have a problem using an editable JComboBox as cell editor in a JTable.
    When i edit the combo and then I press the TAB or ENTER key then all works fine and the value in the TableModel is updated with the edited one, but if i leave the cell with the mouse then the value is not passed to the TableModel. Why ? Is there a way to solve this problem ?
    Regards
    sergio sette

    if (v1.4) [url
    http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTa
    le.html#setSurrendersFocusOnKeystroke(boolean)]go
    hereelse [url
    http://forum.java.sun.com/thread.jsp?forum=57&thread=43
    440]go here
    Thank you. I've also found this one (the first reply): http://forum.java.sun.com/thread.jsp?forum=57&thread=124361 Works fine for me.
    Regards
    sergio sette

  • Editable JComboBox in JTable

    There is a bug in Jdk1.5 (bug#4684090) - When TAB out from Editable Jcombobox in JTable, the focus is moved to outside JTable instead of next cell.
    What is the best workaround for thsi bug.
    Thanks,
    VJ

    I was using java 1.5.0_06 in my application and I had this problem
    When I upgraded to java 1.6.0_01, I no longer had this issue.
    This seems to be a bug in 1.5 version of Java that has been fixed in 1.6
    thanks,

  • Setting color of an item in a Jcombobox in a JTable when editing

    Hello,
    I have a situation where I have a JCombobox in a JTable . The JCombobox is non-editable. The items within the combobox are colored in pink or blue in order to help the user differentiate between the two categories that are in the list.
    But the problem is that when the user is on that cell and is just typing the first letter(s) in the combobox, he does see the item starting with that letter(s) but that item is not shown as colored. However, if the user clicks on the combobox with a mouse, the list pops-up and the items are shown as colored. I want the first part to work where the user can just use keyboard and see the items as colored in that cell itself. By default, currently when the row is selected the foreground is white and background is black. I want that particularly when this combobox has focus and an item is selected, the foreground should be of the same color as the color of the item.
    Can someone help?
    Thanks a lot in advance.

    Additional information on above:
    I associate a customized renderer with the column of my table for which i want to set the colors.
    TableColumn tc6 = jTableDRS.getColumnModel().getColumn(myDRSModel.FINANCECODE_COLUMN);
    tc6.setCellRenderer(new ColorRenderer());
    here is the renderer that I am using...
    public class ColorRenderer extends JLabel implements TableCellRenderer {
    protected Border noFocusBorder;
    public ColorRenderer() {
    noFocusBorder = new EmptyBorder(1, 2, 1, 2);
    setOpaque(true);
    setBorder(noFocusBorder);
    public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus, int row, int column) {
    Color foreground = null;
    Color background = null;
    Font font = null;
    if(column == myDRSModel.FINANCECODE_COLUMN)
    setFont((font != null) ? font : table.getFont());
    String str = (String)value.toString();
    if (!str.equalsIgnoreCase("SELECT ONE")) {
    int y = financedesc.indexOf(str);
    if((payreceiveind.get(y).toString()).equals("R")){
    setForeground(Color.RED);
    } else if((payreceiveind.get(y).toString()).equals("P")){
    setForeground(new Color(147,112,240));
    if(isSelected)
    if((payreceiveind.get(y).toString()).equals("R")){
    setForeground(Color.RED);
    } else if((payreceiveind.get(y).toString()).equals("P")){
    setForeground(new Color(147,112,240));
    setBackground(table.getSelectionBackground());
    else{
    setBackground(table.getBackground());
    else
    if(isSelected)
    setForeground(table.getSelectionForeground());
    setBackground(table.getSelectionBackground());
    else
    setForeground(table.getForeground());
    setBackground(table.getBackground());
    setValue(value);
    return this;
    protected void setValue(Object value) {
    setText((value == null) ? "" : value.toString());
    }

  • Editable JComboBox inside JTable clipping issues

    Here is an example of what I am talking about
    http://img95.imageshack.us/img95/9514/clipping4fw.png
    As you can see, the bottom of the editable JComboBox is being clipped. Is there any way to remove what looks like the invisible border there?
    Thanks
    Daniel

    I am not sure if I understand your question exactly. Your table's row height is not high enough so you may need to add a line of code like:
            table.setRowHeight(table.getRowHeight() + 4);to solve your problem.

  • Editable JCombobox cell editor length limit

    Hi All,
    I have an editable JCombobox as a celleditor to one of the column in JTable, How do I limit the characters in the cell editor?
    Thanks

    A JComboBox uses a JTextField as it's cell editor. You should read up on how you would do this for a normal JTextField by using a custom Document:
    http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html
    Here's an example using JComboBox. I haven't tried it in a JTable but I assume it should work.
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.plaf.basic.*;
    public class ComboBoxNumericEditor extends BasicComboBoxEditor
         public ComboBoxNumericEditor(JComboBox box)
              editor.setDocument( new NumericListDocument(box.getModel()) );
         public static void main(String[] args)
              String[] items = { "one", "two", "three", "four" };
              JComboBox list = new JComboBox( items );
              list.setEditable( true );
              list.setEditor( new ComboBoxNumericEditor( list ) );
              JFrame frame = new JFrame();
              frame.getContentPane().add( list );
              frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
              frame.pack();
              frame.setLocationRelativeTo( null );
              frame.setVisible( true );
         class NumericListDocument extends PlainDocument
              ListModel model;
              public NumericListDocument(ListModel model)
                   this.model = model;
              public void insertString(int offset, String text, AttributeSet a)
                   throws BadLocationException
                   //  Accept all entries in the ListModel
                   for (int i = 0; i < model.getSize(); i++)
                        Object item = model.getElementAt(i);
                        if (text.equals( item.toString() ))
                             super.insertString(offset, text, a);
                             return;
                   //  Build the new text string assuming the insert is successfull
                   String oldText = getText(0, getLength());
                   String newText =
                        oldText.substring(0, offset) + text + oldText.substring(offset);
                   //  Edit the length of the new string
                   if (newText.length() > 2)
                        Toolkit.getDefaultToolkit().beep();
                        return;
                   //  Only numeric characters are allowed in the new string
                   //  (appending the "0" will allow an empty string to be valid)
                   try
                        Integer.parseInt(newText + "0");
                        super.insertString(offset, text, a);
                   catch (NumberFormatException e)
                        Toolkit.getDefaultToolkit().beep();
    }

  • How can I use a FocusEvent to distinguish among editable JComboBoxes?

    Hi,
    I have a Frame with multiple editable JComboBoxes, but I am at a loss as to how to sort them out in the focusGained() method.
    It is easy when they are not editable, because the FocusEvent getSource() Method returns the box that fired the event. That lets me read an instance variable that is set differently for each box.
    But with editable boxes, the FocusEvent is not fired by the box. It is fired by a Component object returned by getEditor().getEditorComponent(). So far I cannot find a way to query that object to find the box it it tied to. (I hope this isn't going to be painfully embarassing.).
    Anyway, the code below produces a frame with four vertical components: a JTextField (textField), a NON-Editable JComboBox (comboBox1) and two Editable JComboBoxes (comboBox2 & comboBox3).
    This is the command screen produced by :
    - Running the class
    - Then tabbing through all the components to return to the text field.
    I am not sure why, but it gives the last component added the foucus on startup.Focus Gained - Begin: *****
       This is the comboBox that Is Not Editable
    Focus Gained - End: *******
    Focus Gained - Begin: *****
       This Is The TextField
    Focus Gained - End: *******
    Focus Gained - Begin: *****
       Class: class javax.swing.plaf.metal.MetalComboBoxEditor$1
       Name: javax.swing.plaf.metal.MetalComboBoxEditor$1
    Focus Gained - End: *******
    Focus Gained - Begin: *****
       Class: class javax.swing.plaf.metal.MetalComboBoxEditor$1
       Name: javax.swing.plaf.metal.MetalComboBoxEditor$1
    Focus Gained - End: *******
    Focus Gained - Begin: *****
       This is the comboBox that Is Not Editable
    Focus Gained - End: *******
    Focus Gained - Begin: *****
       This Is The TextField
    Focus Gained - End: *******As you can see, the FocusEvent source for both editable boxes is a MetalComboBoxEditor. Both have identical names.
    Can anyone help me get from there back to the actual combo box so I can read the instance variable to see which one fired the event?
    The (painfully tedious and inelegant ) code that produced the above output is:import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class TestListeners extends JFrame
      implements ActionListener, DocumentListener,
                             FocusListener,  ItemListener {
      // Constructor
       TestListeners () {
          super ();
          panel.setLayout (new GridLayout (4, 1));
          textField.addActionListener (this);
          textField.getDocument ().addDocumentListener (this);
          textField.addFocusListener (this);
          panel.add(textField);
          comboBox2.addActionListener (this);
          comboBox2.getEditor().getEditorComponent ().addFocusListener (this);
          comboBox2.addItemListener (this);
          comboBox2.setEditable (true);
          panel.add (comboBox2);
          comboBox3.addActionListener (this);
          comboBox3.getEditor().getEditorComponent ().addFocusListener (this);
          comboBox3.addItemListener (this);
          comboBox3.setEditable (true);
          panel.add (comboBox3);
          comboBox1.addActionListener (this);
          comboBox1.addFocusListener (this);
          comboBox1.addItemListener (this);
          comboBox1.setEditable (false);
          panel.add (comboBox1);
          this.getContentPane ().add(panel);
          this.setVisible (true);
          pack ();
       // Nested class
        public class CB extends JComboBox {
           // Nested Constructor
          public CB  (Vector items, String str) {
             super (items);
             this.type = str;
          public String type;
       // Instance Members
       JTextField     textField = new JTextField ("Test Listener TextField");
       JPanel panel  = new JPanel ();
       String[] str = {"one", "two", "three"};
       Vector items = new Vector (Arrays.asList (str));
       CB comboBox1 = new CB (items, "Is Not Editable");
       CB comboBox2 = new CB (items, "Is Editable 2");
       CB comboBox3 = new CB (items, "Is Editable 3");
       // Methods
       public static void main(String args[]) {
          TestListeners frame = new TestListeners ();
       public void actionPerformed (ActionEvent ae) {
          System.out.print ("ActionEvent: This is ");
           if (ae.getSource ().getClass () == CB.class) {
             System.out.print ( ((CB) ae.getSource ()).type + " ");
          System.out.println (" "+ae.getActionCommand() + "\n" );
       public void focusGained (FocusEvent fge) {
          System.out.println ("Focus Gained - Begin: ***** ");
          if (fge.getSource ().getClass () == CB.class) {
          System.out.println ( "   This is the comboBox that "+((CB) fge.getSource ()).type);
          } else if (fge.getSource ().getClass () == JTextField.class) {
             System.out.println ( "   This Is The TextField");
         } else {
             System.out.println ("   Class: "+fge.getSource ().getClass());
             System.out.println ("   Name: "+fge.getSource ().getClass ().getName ());
         System.out.println ("Focus Gained - End: *******\n*\n");
       public void focusLost (FocusEvent fle) { }
       public void changedUpdate (DocumentEvent de) { }
       public void insertUpdate (DocumentEvent de) { }
       public void removeUpdate (DocumentEvent de) { }
       public void itemStateChanged (ItemEvent ie) { }
    }

    I added the following in your focusGained() method and it seemed to work:
    Component c = ((Component)fge.getSource ()).getParent();
    if (c instanceof JComboBox)
         JComboBox cb = (JComboBox)c;
         System.out.println("Selected: " + cb.getSelectedItem());
    }

  • Editable JComboBox -Saving itz new value in the database.

    Hi,
    I have a Combobox say Product , in which some fields (like -> tv , hand blender etc) comes from Database and i have added one more field say others ,
    in others field user can enter new product name and it shd be saved in the same database, i have used editable jcombobox but dnt no how to proceed further.
    Thanks & Regards
    Komal

    And make a provision to delete wrong entries, or you'll end up with all the typos stored separately.
    I speak from experience. I actually have such a combo in several of my VFP applications (not in Java though)
    db

  • Blank values for editable JComboBox

    Hello,
    I have a editable JComboBox that the user can erase its current value and if they simple change focus (like click on a text field on the same panel) the combo box is left blank. i would like to have it so that if the user leaves focus of the box and its blank that current value goes back to what it was before they erased it.
    Many thanks,
    Jonathan

    import java.awt.FlowLayout;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    public class NonBlankCombo {
       JComboBox comboBox;
       Object previousContent;
       void makeUI() {
          Object[] data = {"One", "Two", "Three", "Four", "Five"};
          previousContent = data[0];
          comboBox = new JComboBox(data);
          comboBox.setEditable(true);
          comboBox.addItemListener(new ItemListener() {
             public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED
                      && !((String) comboBox.getSelectedItem()).trim().isEmpty()) {
                   previousContent = comboBox.getSelectedItem();
          comboBox.getEditor().getEditorComponent().addFocusListener(new FocusListener() {
             public void focusGained(FocusEvent e) {
                previousContent = comboBox.getSelectedItem();
             public void focusLost(FocusEvent e) {
                if (((String)comboBox.getSelectedItem()).trim().isEmpty()) {
                   comboBox.setSelectedItem(previousContent);
          JFrame frame = new JFrame("Non-blank Combo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(200, 100);
          frame.setLayout(new FlowLayout());
          frame.add(comboBox);
          frame.add(new JButton("Click"));
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                new NonBlankCombo().makeUI();
    }db

  • How to create full editable JComboBox  ?

    I need to create full editable comboBox -- i want to do it by extends the class JComboBox - but i dont realy know how to collect the chars that the user will be insert and show then on the control.
    Does someone know where can i find some example of editable JComboBox ?
    Thanks.

    JComboBox can already be made editable, so you need to clarify what exactly you mean by "full editable"
    Recommended reading: The Java&#8482; Tutorials: How to use Combo Boxes: [Using an Editable Combo Box|http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html#editable]
    db

  • JComboBox HTML select JTable editor

    I want to use a JComboBox in a JTable cell as editor and renderer. The JComboBox should work just like a HTML select component with a value and a description. When the user changes selection in the JComboBox instead of the description the value should be set as the cell value in the JTable. Can somebody help me with information about how I can do this?
    Thanks.

    Check out this [url http://forum.java.sun.com/thread.jsp?forum=57&thread=478229]example. You store an Object in the cell that contains both the value and description.

  • Can JOptionPane produce an editable JComboBox?

    Can I use JOptionPane for an input dialog component with a data input area represented by an editable JComboBox ?
    For example :
    JOptionPane.showInputDialog( parent,
    � How Much?�,
    �This is the title�,
    JOptionPane.QUESTION_MESSAGE,
    null,
    new Object[]
    { � �, �un peu�,�beaucoup�,�passionnement�},
    �passionnement�);
    This one statement produces the input dialog I want, except that it does not allow an input such as �pas du tout� which is not in the list of selections.
    Does any one knows if somehow I could get to this JComboxBox and make it editable? (still using JOptionPane of course)?
    Thank you to any one who can answer me.

    Hi,
    You could use the JOptionPane.showMessageDialog() in the following way:
    JComboBox comboBox = //Make your own JComboBox here
    Object[] message = new Object[] {
    "Make your choice:",
    comboBox
    JOptionPane.showMessageDialog(parent, message);
    In this way you are able to add every Component to your message dialog you want to.
    Hope that helps, Mathias

  • JCombobox with a JTable as Popup

    Hi!
    I will ask if it's possible create a JCombobox that show a JTable instead the popup at the mouse click event.
    I've drew a JComboBox and a JTable under it but I don't know how can I stop the popup menu.
    Thanks for the help.

    you will have to make JavaBean Component for the same..
    you may have to use of
    - JTextFiend
    - JButton
    - JTable
    - JLayerPane
    with the specific event as you required..
    Timir

  • Update the testfield when setSelectedItem() for editable JComboBox

    Hi,
    I am having problem with setSelectedItem() and setSelectedIndex() methods of editable JComboBox.
    I extended JComboBox with default dataModel. When I enter some string in the textfield of the combobox, I call insertItemAt() method to dynamically grow the list. The application requires to clear the textfield everytime I enter something and press "return" key. To make it work, I insert an empty string to the list at the very beginning and call the setSelectedItem("") method in the actionPerformed().
    Now the problem is that I know the setSelectedItem("") is called since the getActionCommand() returns "comboBoxChanged" everytime I enter something in the textfield, but the testfield is not cleared. And if I select one item from the list and press "enter" key, the textfield is cleared. To make things more interesting, after I enter the first string and press "enter", the textfield really get cleared, but not for all other later input. Is this a bug? Can anyone please help me? Thanks a lot. Below is my code snippet.
    public class MyComboBox extends JComboBox implements ActionListener
    public GsISCommComboBox()
    jbInit();
    private void jbInit() throws Exception
    this.setEditable(true);
    this.addActionListener(this);
    insertItemAt("",0);
    public void actionPerformed(ActionEvent e)
    String str=null;
    String strrr=e.getActionCommand();
    System.out.println("getActionCommand: "+strrr);
    if(strrr.equalsIgnoreCase("comboBoxChanged"))
    str=(String)this.getSelectedItem();
    return;
    else
    str=strrr;
    // other stuff to process the string and do things
    setSelectedItem("");
    repaint();
    hidePopup();
    insertItemAt(str,0);

    Try inserting your string first and then, try clearing the combobox's textfield.
    insertItemAt(str,0);
    setSelectedItem("");
    hidePopup();
    repaint();
    Since you add the "" string at the begining, it will have the index 0. Perhaps, you want to insert your string this way:
    // insert string at the end
    insertItemAt(str, this.getItemCount());
    //Then use the
    setSelectedIndex(0);
    But you need to remember that if you add an item at the begining, index 0 may not be your '' '' string after that.
    I hope that it helps.

Maybe you are looking for