use of javax.swing.SwingWorker in project jgnash by ccavanaugh.
the class BudgetPanel method initBudgetCombo.
private void initBudgetCombo() {
budgetCombo = new BudgetComboBox();
SwingWorker<StoredObject, Void> worker = new SwingWorker<StoredObject, Void>() {
@Override
protected StoredObject doInBackground() throws Exception {
Preferences preferences = Preferences.userNodeForPackage(BudgetPanel.class);
String lastBudgetUUID = preferences.get(LAST_BUDGET, null);
StoredObject o = null;
if (lastBudgetUUID != null) {
o = engine.getBudgetByUuid(lastBudgetUUID);
}
return o;
}
@Override
protected void done() {
try {
StoredObject o = get();
if (o != null && o instanceof Budget) {
budgetCombo.setSelectedBudget((Budget) o);
activeBudget = (Budget) o;
}
if (activeBudget == null) {
List<Budget> budgets = engine.getBudgetList();
if (!budgets.isEmpty()) {
budgetCombo.setSelectedBudget(budgets.get(0));
activeBudget = budgets.get(0);
}
}
// the combo takes the full toolbar space unless limited
budgetCombo.setMaximumSize(new Dimension(COMBO_BOX_WIDTH, budgetCombo.getPreferredSize().height * 3));
budgetCombo.addActionListener(e -> {
if (activeBudget != budgetCombo.getSelectedBudget()) {
refreshDisplay();
}
});
} catch (final InterruptedException | ExecutionException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}
};
worker.execute();
}
Aggregations