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]

# 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.