I'm using Swing to build an application that uses a database. I have a weird problem that I don't understand how to solve.
In a certain panel, I have a comboBox which I want to populate with records using DB query. after finishing constructing the JPanel which hold the comboBox, I'm executing a Worker
private void populateLists(){
GetSimpleRecordsWorker worker = new GetSimpleRecordsWorker(Tables.characters,panelLeftDetails);
worker.execute();
}
Its doInBackground
does a SELECT query and after getting the result - in the Worker's done()
methods - updates the panel's model and calls the panel's refreshFromModel
.
charI.setSimpleModel(simpleModel);
charI.refreshFromModel();
refreshFromModel
does the following :
private void refreshFromSimpleModel(){
charSelectCombo.removeAllItems();
Pair[] pairs = simpleModel.getRecords();
for (int i=0; i<pairs.length; i++){
charSelectCombo.addItem(pairs[i]);
}
charSelectCombo.setSelectedItem(null);
}
Now, the problem is, after it finish refreshFromModel - the GUI stuck/freeze. I HAVE NO IDEA WHY! Weird is, in a different frame, I use the same methodology - and it works with no problems.
Also, if I try removeAll
instead of removeAllItems
it works but the ComboBox loses its arrow and acts weird.
I've taken the frame trace in the debugger - things like removeSourceEvent or Unsafe.park are there.
GUI.GuiHandler at localhost:53547 (Suspended)
Daemon System Thread [Attach Listener] (Suspended)
Daemon System Thread [Signal Dispatcher] (Suspended)
Daemon System Thread [Finalizer] (Suspended)
Object.wait(long) line: not available [native method]
ReferenceQueue<T>.remove(long) line: not available
ReferenceQueue<T>.remove() line: not available
Finalizer$FinalizerThread.run() line: not available
Daemon System Thread [Reference Handler] (Suspended)
Object.wait(long) line: not available [native method]
Reference$Lock(Object).wait() line: 485
Reference$ReferenceHandler.run() line: not available
Thread [AWT-Shutdown] (Suspended)
Object.wait(long) line: not available [native method]
Object.wait() line: 485
AWTAutoShutdown.run() line: not available
Thread.run() line: not available
Daemon System Thread [Java2D Disposer] (Suspended)
Object.wait(long) line: not available [native method]
ReferenceQueue<T>.remove(long) line: not available
ReferenceQueue<T>.remove() line: not available
Disposer.run() line: not available
Thread.run() line: not available
Daemon Thread [AWT-Windows] (Suspended)
WToolkit.eventLoop() line: not available [native method]
WToolkit.run() line: not available
Thread.run() line: not available
Thread [AWT-EventQueue-0] (Suspended)
EventQueue.removeSourceEvents(Object, boolean) line: not available
WindowsComboBoxUI$WindowsComboBoxRenderer(Component).removeNotify() line: not available
WindowsComboBoxUI$WindowsComboBoxRenderer(Container).removeNotify() line: not available
WindowsComboBoxUI$WindowsComboBoxRenderer(JComponent).removeNotify() line: not available
CellRendererPane(Container).remove(int) line: not available
CellRendererPane(Container).remove(Component) line: not available
WindowsComboBoxUI(BasicComboBoxUI).getSizeForComponent(Component) line: not available
WindowsComboBoxUI(BasicComboBoxUI).getDisplaySize() line: not available
WindowsComboBoxUI(BasicComboBoxUI).getMinimumSize(JComponent) line: not available
AutoCompleteComboBox(JComponent).getMinimumSize() line: not available
FlowLayout.minimumLayoutSize(Container) line: not available
JPanel(Container).minimumSize() line: not available
JPanel(Container).getMinimumSize() line: not available
JPanel(JComponent).getMinimumSize() line: not available
BoxLayout.checkRequests() line: not available
BoxLayout.minimumLayoutSize(Container) line: not available
JP开发者_JS百科anel(Container).minimumSize() line: not available
JPanel(Container).getMinimumSize() line: not available
JPanel(JComponent).getMinimumSize() line: not available
BoxLayout.checkRequests() line: not available
BoxLayout.preferredLayoutSize(Container) line: not available
JPanel(Container).preferredSize() line: not available
JPanel(Container).getPreferredSize() line: not available
JPanel(JComponent).getPreferredSize() line: not available
BorderLayout.layoutContainer(Container) line: not available
JPanel(Container).layout() line: not available
JPanel(Container).doLayout() line: not available
JPanel(Container).validateTree() line: not available
JPanel(Container).validateTree() line: not available
JPanel(Container).validateTree() line: not available
JLayeredPane(Container).validateTree() line: not available
JRootPane(Container).validateTree() line: not available
JRootPane(Container).validate() line: not available
RepaintManager.validateInvalidComponents() line: not available
SystemEventQueueUtilities$ComponentWorkRequest.run() line: not available
InvocationEvent.dispatch() line: not available
EventQueue.dispatchEvent(AWTEvent) line: not available
EventDispatchThread.pumpOneEventForFilters(int) line: not available
EventDispatchThread.pumpEventsForFilter(int, Conditional, EventFilter) line: not available
EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: not available
EventDispatchThread.pumpEvents(int, Conditional) line: not available
EventDispatchThread.pumpEvents(Conditional) line: not available
EventDispatchThread.run() line: not available
Thread [DestroyJavaVM] (Suspended)
Daemon System Thread [D3D Screen Updater] (Suspended)
Object.wait(long) line: not available [native method]
D3DScreenUpdateManager.run() line: not available
Thread.run() line: not available
Daemon System Thread [TimerQueue] (Suspended)
Object.wait(long) line: not available [native method]
TimerQueue.run() line: not available
Thread.run() line: not available
Thread [Thread-3] (Suspended)
Thread.sleep(long) line: not available [native method]
JDCConnectionPool$ConnectionReaper.run() line: 93
Daemon Thread [SwingWorker-pool-1-thread-1] (Suspended)
Unsafe.park(boolean, long) line: not available [native method]
LockSupport.park(Object) line: not available
AbstractQueuedSynchronizer$ConditionObject.await() line: not available
LinkedBlockingQueue<E>.take() line: not available
ThreadPoolExecutor.getTask() line: not available
ThreadPoolExecutor$Worker.run() line: not available
Thread.run() line: not available
Lot of thanks in advance.
its doInBackground does a SELECT query and after getting the result - updates the panel's model and calls the panel's refreshFromModel.
The DB query is done in the doInBackground().
The updating of the model need to be done in the process() method.
Read the section from the Swing tutorial on Concurrency for more information.
Adding 100,000 items to your combo box is probably the root of your problem.
You also have to wonder how one user could possibly deal with 100,000 items in a combo box.
JComboBox
is normally not supposed to deal with such a big number of items.
One possible way to optimize your code would be to explicitly call setPrototypeDisplayValue()
, this way, you avoid JComboBox
checking the dimensions of ALL its model items to compute its own dimensions. That's probably the current reason why your system looks frozen.
I understand you are looking for autocomplete functionality with a massive amount of suggestions.
Perhaps you can have a look at the glazedlists library. Its core function is to provide observable lists, but it is very flexible. One of the things you can do is build a ComboBoxModel based on such a list and enable autocompletion for a combobox (through the AutoCompleteSupport
class).
This approach may provide a way to avoid any ComboBox-related performance issues in addItem. That part would be moved into glazedlists.
精彩评论