Scroll JComboBox List with code

Hi all,

I've extended DefaultCellEditor passing in a JComboBox, and I'm using this editor in tables throughout my application.

I have a requirement to scroll the JComboBox's List to the beginning each time the editor is displayed and the combo popup is shown as sometimes the combo model can contain heaps of elements.

I've had a fair look at JavaDocs and JComboBox code, and can't yet see any way of getting a handle on the UI JList via the JComboBox.

I've tried resetting the model on the JComboBox (selects the first item, but still scrolls to the last selected item), and I've tried setSelectedIndex(-1) in a popup listener on the JComboBox, but this prevents normal selection (ie, the popup won't hide after item selection).

Does anyone know how to access the JComboBoxes' List so that I can scroll it back to the start of the list?

Many thanks,

Paul C.

[916 byte] By [Pcasanovaa] at [2007-11-27 3:41:30]
# 1

I don't understand the problem. The combobox will highlight the selected item. If no item is selected then the drop down should open from the first entry.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 8:45:00 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks camickr.

I knew this problem would be hard to explain - and it's hard to put into a SSCCE as I need code for a frame, table and table editor. So please excuse the following code:

package testclasses;

import java.awt.event.MouseEvent;

import java.util.EventObject;

import java.util.Vector;

import javax.swing.DefaultCellEditor;

import javax.swing.JComboBox;

import javax.swing.table.TableCellEditor;

public class ComboEditorTest extends javax.swing.JFrame

{

public ComboEditorTest()

{

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents()

{

scrTestTable = new javax.swing.JScrollPane();

tblTestTable = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

tblTestTable.setModel(new javax.swing.table.DefaultTableModel(

new Object [][]

{

{null},

{null},

{null},

{null}

},

new String []

{

"Title 1"

}

)

{

Class[] types = new Class []

{

java.lang.Integer.class

};

public Class getColumnClass(int columnIndex)

{

return types [columnIndex];

}

});

applyEditor();

scrTestTable.setViewportView(tblTestTable);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(scrTestTable, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(scrTestTable, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)

.addContainerGap())

);

pack();

}// </editor-fold>

public static void main(String args[])

{

java.awt.EventQueue.invokeLater(new Runnable()

{

public void run()

{

new ComboEditorTest().setVisible(true);

}

});

}

private void applyEditor()

{

Vector<Integer> items = new Vector<Integer>();

for (int i = 0; i < 20; i++)

{

items.add(new Integer(i));

}

JComboBox combo = new JComboBox(items);

TableCellEditor editor = new DefaultCellEditor(combo)

{

public boolean isCellEditable(EventObject evt)

{

if (evt instanceof MouseEvent)

{

return ((MouseEvent)evt).getClickCount() >= 2;

}

return true;

}

};

tblTestTable.setDefaultEditor(Integer.class, editor);

}

// Variables declaration - do not modify

private javax.swing.JScrollPane scrTestTable;

private javax.swing.JTable tblTestTable;

// End of variables declaration

}

If you run this and select say 18 as the value of the first cell, when you double click the cell below, the editor will be scrolled down enough to show 18 again - I need it to automatically scroll to the top.

Thanks,

Paul C.

Pcasanovaa at 2007-7-12 8:45:01 > top of Java-index,Desktop,Core GUI APIs...
# 3

Sorry, I see the problem, but have no idea how to fix it.

I would suggest you use some value other than "null" to indicate an unknown value.

For example, you could create a NullInteger class that extends Integer. Then you could override to toString() method to return "Please Select...". Then you set item 0 to be selected by default and it should then work like a normal combo box.

camickra at 2007-7-12 8:45:01 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thanks for the response camickr - yes, it's a curly one alright - and a problem when some of the editors can contain up to 200 government specified health code set options! The complexity with an alternative representation of null is that I have around 130 code sets, of which around 60 are used in JTables.

I was at first thinking I may be able to get a reference to the JComboBox's popup's JList and just scroll that to the top when the popup is about to show (popup event listener) - but I'm lost on how to access the JList.

This may be an issue that I have to tinker with in my spare time (whenever that may be!) after the pilot app is launched.

Thanks again for taking a look,

Paul C.

Pcasanovaa at 2007-7-12 8:45:01 > top of Java-index,Desktop,Core GUI APIs...