vendredi 24 avril 2015

JComboBox auffristen nach Listenänderung

Hallo,

ich habe ein Programm, dass in einer JComboBox eine Liste sortierter Städte anzeigt. Die Städte werden aus einer XML-Datei ausgelesen. Ein Unterprogramm kann Städte hinzufügen oder löschen.
Mein Problem ist, dass ich nicht hinbekomme, dass die JComboBox neugeladen wird, wenn das Unterprogramm geschlossen wird und das Hauptfenster wieder den Focus hat / aktiviert ist.
Java Code:

  1. @Override
  2. public void windowActivated(WindowEvent e) {
  3. myComboBoxModel.reload();
  4. targetCityComboBox.setModel(myComboBoxModel);
  5. }


Als Alternative habe ich im Reset-Listener
Java Code:

  1. myComboBoxModel.reload();
  2. targetCityComboBox.setModel(myComboBoxModel);

einfügt. Der Focus oder die Aktivierung des Hauptfenster erzeugt überhaupt keine Reaktion. Und der Resetbutton wirft diese Exception:
Code:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
        at java.util.ArrayList.rangeCheck(ArrayList.java:653)
        at java.util.ArrayList.get(ArrayList.java:429)
        at com.example.calculator.gui.MainGui$1.actionPerformed(MainGui.java:521)

Und ich muss ihn zweimal drücken, bis dann die JComboBox die aktuelle Städteliste anzeigt.
Ich hätte gerne das Ergebnis mit der Fensteraktivierung und vor allem ohne Fehler realisiert.
Hier die Klasse für das ComboBoxModel, wobei XMLCreator eine Klasse zum Bearbeiten, Lesen der XML-Dateien ist.
Java Code:

  1. package com.example.calculator;
  2.  
  3. import java.util.Collections;
  4. import java.util.Vector;
  5.  
  6. import javax.swing.DefaultComboBoxModel;
  7.  
  8.  
  9. public class ComboBoxModel extends DefaultComboBoxModel<String> {
  10.  
  11. private static final long serialVersionUID = 1L;
  12. private XMLCreator cityList = new XMLCreator();
  13.  
  14.  
  15. public void reload() {
  16. removeAllElements();
  17.  
  18. Vector<String> sortedCityList = new Vector<String>();
  19. for (int i = 0; i < cityList.CityList().size(); i++) {
  20. sortedCityList.add(cityList.CityList().get(i));
  21. }
  22. Collections.sort(sortedCityList);
  23.  
  24. for (int i = 0; i < sortedCityList.size(); i++) {
  25. addElement("" + sortedCityList.get(i));
  26. }
  27. }
  28.  
  29. public int setText(String cityName) {
  30. int cityId = 0;
  31. for (int i = 0; i < cityList.CityList().size(); i++) {
  32. if (cityList.CityList().get(i).equals(cityName)) {
  33. cityId = cityList.CityList().indexOf(cityName);
  34. }
  35. }
  36. return cityId;
  37. }
  38.  
  39. }


Java Code:

  1.  
  2. package com.example.calculator.gui;
  3.  
  4. import com.linuxmaker.calculator.*;
  5. import com.linuxmaker.calculator.ComboBoxModel;
  6. import com.toedter.calendar.JDateChooser;
  7. import org.w3c.dom.DOMException;
  8. import org.xml.sax.SAXException;
  9.  
  10. import javax.swing.*;
  11. import javax.swing.border.EmptyBorder;
  12. import javax.swing.border.TitledBorder;
  13. import javax.xml.transform.TransformerException;
  14. import javax.xml.xpath.XPathExpressionException;
  15. import java.awt.*;
  16. import java.awt.Container;
  17. import java.awt.event.*;
  18. import java.beans.PropertyChangeEvent;
  19. import java.beans.PropertyChangeListener;
  20. import java.io.IOException;
  21. import java.text.DecimalFormat;
  22.  
  23. import java.io.File;
  24. import java.text.SimpleDateFormat;
  25.  
  26.  
  27. public class MainGui extends JFrame implements WindowListener {
  28. // Variables declaration
  29.  
  30. private JComboBox targetCityComboBox;
  31. private JComboBox stateComboBox;
  32. private JLabel feeLabel;
  33.  
  34. private ComboBoxModel myComboBoxModel;
  35. private Double fee;
  36. private int projektdays;
  37. // End of variables declaration
  38.  
  39. /**
  40.   * Creates new form MainGui
  41.   */
  42. public MainGui() {
  43. initComponents();
  44. }
  45.  
  46. /**
  47.   * Components of the form
  48.   */
  49. private void initComponents() {
  50.  
  51. targetCityLabel.setText("Projektstadt");
  52. targetCityLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
  53. targetCityComboBox = new JComboBox<>();
  54. targetCityComboBox.setFont(new Font("Dialog", Font.PLAIN, 11));
  55. targetCityComboBox.setEditable(false);
  56. myComboBoxModel = new ComboBoxModel();
  57. // Sets ComboBoxModel
  58. myComboBoxModel.reload();
  59. targetCityComboBox.setModel(myComboBoxModel);
  60.  
  61.  
  62. /**
  63.   * Creates the GUI-Layout
  64.   */
  65. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  66. setResizable(false);
  67. setIconImage(new ImageIcon(getClass().getResource("/resources/images16x16/currency_euro_yellow.png")).getImage());
  68. setTitle("Freelancer - Honorarrechner");
  69. setLocationRelativeTo(null);
  70. BorderLayout layout = new BorderLayout();
  71. getContentPane().setLayout(layout);
  72. getContentPane().setBackground(new Color(250, 219, 108));
  73.  
  74. //======== dialogPane ========
  75. {
  76. dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
  77. dialogPane.setLayout(new BorderLayout());
  78.  
  79. //======== contentPanel ========
  80. {
  81. contentPanel.setLayout(new GridBagLayout());
  82. ((GridBagLayout)contentPanel.getLayout()).columnWidths = new int[] {60, 35, 0, 63, 94, 0};
  83. ((GridBagLayout)contentPanel.getLayout()).rowHeights = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  84. ((GridBagLayout)contentPanel.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
  85. ((GridBagLayout)contentPanel.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
  86.  
  87. contentPanel.add(originCityLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
  88. new Insets(0, 0, 5, 5), 0, 0));
  89. contentPanel.add(originCityTextField, new GridBagConstraints(2, 0, 3, 1, 0.0, 0.0,
  90. new Insets(0, 0, 5, 0), 0, 0));
  91. contentPanel.add(targetCityLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
  92. new Insets(0, 0, 5, 5), 0, 0));
  93. contentPanel.add(targetCityComboBox, new GridBagConstraints(2, 1, 3, 1, 0.0, 0.0,
  94. new Insets(0, 0, 5, 0), 0, 0));
  95. contentPanel.add(feeLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
  96. new Insets(0, 0, 5, 5), 0, 0));
  97. contentPanel.add(feeTextField, new GridBagConstraints(2, 2, 2, 1, 0.0, 0.0,
  98. new Insets(0, 0, 5, 5), 0, 0));
  99. contentPanel.add(cur1Label, new GridBagConstraints(4, 2, 1, 1, 0.0, 0.0,
  100. new Insets(0, 0, 5, 0), 0, 0));
  101. contentPanel.add(scontoComboBox, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0,
  102. new Insets(0, 0, 5, 5), 0, 0));
  103. contentPanel.add(scontoLabel, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0,
  104. new Insets(0, 0, 5, 5), 0, 0));
  105. contentPanel.add(projectDaysLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0,
  106. new Insets(0, 0, 5, 5), 0, 0));
  107. contentPanel.add(projectStartChooser, new GridBagConstraints(2, 4, 2, 1, 0.0, 0.0,
  108. new Insets(0, 0, 5, 5), 0, 0));
  109. contentPanel.add(stateComboBox, new GridBagConstraints(4, 4, 1, 1, 0.0, 0.0,
  110. new Insets(0, 0, 5, 5), 0, 0));
  111. contentPanel.add(cur2Label, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0,
  112. new Insets(0, 0, 5, 5), 0, 0));
  113. contentPanel.add(projectEndChooser, new GridBagConstraints(2, 5, 2, 1, 0.0, 0.0,
  114. new Insets(0, 0, 5, 5), 0, 0));
  115. contentPanel.add(projectDaysTextField, new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0,
  116. new Insets(0, 0, 5, 5), 0, 0));
  117. contentPanel.add(cur3Label, new GridBagConstraints(3, 6, 1, 1, 0.0, 0.0,
  118. new Insets(0, 0, 5, 5), 0, 0));
  119. {
  120. settingsPanel.setBorder(new TitledBorder(null, "Weitere Einstellungen", TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION,
  121. new Font("Dialog", Font.PLAIN, 11), Color.blue));
  122. settingsPanel.setLayout(new GridBagLayout());
  123. ((GridBagLayout)settingsPanel.getLayout()).columnWidths = new int[] {150, 27, 169, 0};
  124. ((GridBagLayout)settingsPanel.getLayout()).rowHeights = new int[] {0, 0, 0};
  125. ((GridBagLayout)settingsPanel.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 1.0};
  126. ((GridBagLayout)settingsPanel.getLayout()).rowWeights = new double[] {0.0, 0.0, 1.0};
  127.  
  128. settingsPanel.add(overnightCheckBox, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
  129. new Insets(0, 0, 5, 5), 0, 0));
  130. settingsPanel.add(carCheckBox, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
  131. new Insets(0, 0, 5, 0), 0, 0));
  132. settingsPanel.add(saturdayWork, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
  133. new Insets(0, 0, 5, 5), 0, 0));
  134. settingsPanel.add(hoursPerDayTextField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
  135. new Insets(5, 0, 5, 0), 0, 0));
  136. settingsPanel.add(hoursPerDayLabel, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
  137. new Insets(0, 5, 0, 0), 0, 0));
  138. buttonGroup.add(netFeeRadioButton);
  139. settingsPanel.add(netFeeRadioButton, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
  140. new Insets(0, 0, 0, 5), 0, 0));
  141. buttonGroup.add(allinFeeRadioButton);
  142. settingsPanel.add(allinFeeRadioButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0,
  143. new Insets(0, 0, 0, 0), 0, 0));
  144. }
  145. contentPanel.add(settingsPanel, new GridBagConstraints(0, 7, 5, 1, 0.0, 0.0,
  146. new Insets(0, 0, 5, 0), 0, 0));
  147. {
  148. resultPanel.setLayout(new GridBagLayout());
  149. ((GridBagLayout)resultPanel.getLayout()).columnWidths = new int[] {108, 115, 89, 0};
  150. ((GridBagLayout)resultPanel.getLayout()).rowHeights = new int[] {0, 0, 0, 0};
  151. ((GridBagLayout)resultPanel.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 1.0};
  152. ((GridBagLayout)resultPanel.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 1.0};
  153.  
  154. resultPanel.add(dayHonorarLabel, new GridBagConstraints(0, 9, 1, 1, 0.0, 0.0,
  155. new Insets(0, 0, 5, 5), 0, 0));
  156. resultPanel.add(dayValueLabel, new GridBagConstraints(2, 9, 1, 1, 0.0, 0.0,
  157. new Insets(0, 0, 5, 5), 0, 0));
  158. resultPanel.add(cur4Label, new GridBagConstraints(3, 9, 1, 1, 0.0, 0.0,
  159. new Insets(0, 0, 5, 0), 0, 0));
  160. resultPanel.add(hourHonorarLabel, new GridBagConstraints(0, 10, 1, 1, 0.0, 0.0,
  161. new Insets(0, 0, 5, 5), 0, 0));
  162. resultPanel.add(hourValueLabel, new GridBagConstraints(2, 10, 1, 1, 0.0, 0.0,
  163. new Insets(0, 0, 5, 5), 0, 0));
  164. resultPanel.add(cur5Label, new GridBagConstraints(3, 10, 1, 1, 0.0, 0.0,
  165. new Insets(0, 0, 5, 0), 0, 0));
  166. resultPanel.add(turnoverLabel, new GridBagConstraints(0, 11, 1, 1, 0.0, 0.0,
  167. new Insets(0, 0, 0, 5), 0, 0));
  168. resultPanel.add(turnoverValueLabel, new GridBagConstraints(2, 11, 1, 1, 0.0, 0.0,
  169. new Insets(0, 0, 0, 5), 0, 0));
  170. resultPanel.add(cur6Label, new GridBagConstraints(3, 11, 1, 1, 0.0, 0.0,
  171. new Insets(0, 0, 0, 0), 0, 0));
  172. }
  173. contentPanel.add(resultPanel, new GridBagConstraints(0, 8, 5, 1, 0.0, 0.0,
  174. new Insets(0, 0, 0, 0), 0, 0));
  175. }
  176. dialogPane.add(contentPanel, BorderLayout.CENTER);
  177. }
  178. add(dialogPane, BorderLayout.CENTER);
  179.  
  180. //======== buttonBar ========
  181. {
  182. buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
  183. buttonBar.setLayout(new GridBagLayout());
  184. ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 85, 80};
  185. ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0, 0.0};
  186.  
  187. buttonBar.add(calculateButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
  188. new Insets(0, 0, 12, 5), 0, 0));
  189. buttonBar.add(resetButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
  190. new Insets(0, 0, 12, 5), 0, 0));
  191. buttonBar.add(endButton, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
  192. new Insets(0, 0, 12, 12), 0, 0));
  193. }
  194. add(buttonBar, BorderLayout.SOUTH);
  195.  
  196. // Create the menuBar
  197. {
  198. fileMenu.add(exitMenuItem);
  199. menuBar.add(fileMenu);
  200. editMenu.add(addMenuItem);
  201. editMenu.add(changeMenuItem);
  202. editMenu.add(settingsMenuItem);
  203. menuBar.add(editMenu);
  204. infoMenu.add(helpMenuItem);
  205. infoMenu.add(aboutMenuItem);
  206. menuBar.add(infoMenu);
  207. }
  208. setJMenuBar(menuBar);
  209. pack();
  210. /** End of Creates the GUI-Layout **/
  211.  
  212. /**
  213.   * Button Listeners and Action Listeners
  214.   */
  215. targetCityComboBox.addActionListener(new ActionListener() {
  216. @Override
  217. public void actionPerformed(ActionEvent e) {
  218. dayValueLabel.setVisible(false);
  219. hourValueLabel.setVisible(false);
  220. turnoverValueLabel.setVisible(false);
  221. cur2Label.setVisible(false);
  222. cur3Label.setVisible(false);
  223. cur4Label.setVisible(false);
  224. cur5Label.setVisible(false);
  225. cur6Label.setVisible(false);
  226. XMLCreator xmlelement = new XMLCreator();
  227. if (Double.parseDouble(xmlelement.readXml((String) targetCityComboBox.getSelectedItem()).get(1)) < 225.0 &&
  228. Double.parseDouble(xmlelement.readXml((String) targetCityComboBox.getSelectedItem()).get(1)) > 80.0) {
  229. overnightCheckBox.setEnabled(true);
  230. } else {
  231. overnightCheckBox.setEnabled(false);
  232. }
  233. }
  234. });
  235.  
  236.  
  237. //---- resetButton ----
  238. resetButton.addActionListener(new ActionListener() {
  239. @Override
  240. public void actionPerformed(ActionEvent e) {
  241. originCityTextField.setText(new Settings().readSettings("pointOfDeparture"));
  242. feeTextField.setText("0.00");
  243. scontoComboBox.setSelectedIndex(2);
  244. hoursPerDayTextField.setText(String.valueOf(workingHours));
  245. dayValueLabel.setVisible(false);
  246. hourValueLabel.setVisible(false);
  247. hourHonorarLabel.setVisible(true);//TODO Projektstadt reseten, wenn sich Ausgangsstadt ändert
  248. dayHonorarLabel.setVisible(true);
  249. cur2Label.setVisible(false);
  250. cur3Label.setVisible(false);
  251. turnoverLabel.setVisible(true);
  252. turnoverValueLabel.setVisible(false);
  253. cur4Label.setVisible(false);
  254. cur5Label.setVisible(false);
  255. cur6Label.setVisible(false);
  256. projectDaysTextField.setText("");
  257. projectEndChooser.setEnabled(false);
  258. saturdayWork.setEnabled(false);
  259. myComboBoxModel.reload();
  260. targetCityComboBox.setModel(myComboBoxModel);
  261. }
  262. });
  263.  
  264. //---- endButton ----
  265. endButton.addActionListener(new ActionListener() {
  266. @Override
  267. public void actionPerformed(ActionEvent e) {
  268. Container frame = endButton.getParent();
  269. do {
  270. frame = frame.getParent();
  271. } while (!(frame instanceof JFrame));
  272. ((JFrame) frame).dispose();
  273. }
  274. });
  275. }
  276.  
  277. /**
  278.   * Launch the application.
  279.   */
  280. public static void main(String[] args) {
  281. /* Create and display the form */
  282. EventQueue.invokeLater(new Runnable() {
  283. public void run() {
  284. try {
  285. MainGui frame = new MainGui();
  286. frame.setVisible(true);
  287. } catch (Exception ex) {
  288. ex.printStackTrace();
  289. }
  290. }
  291. });
  292. }
  293.  
  294. @Override
  295. public void windowOpened(WindowEvent e) {
  296.  
  297. }
  298.  
  299. @Override
  300. public void windowClosing(WindowEvent e) {
  301.  
  302. }
  303.  
  304. @Override
  305. public void windowClosed(WindowEvent e) {
  306.  
  307. }
  308.  
  309. @Override
  310. public void windowIconified(WindowEvent e) {
  311.  
  312. }
  313.  
  314. @Override
  315. public void windowDeiconified(WindowEvent e) {
  316.  
  317. }
  318.  
  319. @Override
  320. public void windowActivated(WindowEvent e) {
  321. myComboBoxModel.reload();
  322. targetCityComboBox.setModel(myComboBoxModel);
  323. }
  324.  
  325. @Override
  326. public void windowDeactivated(WindowEvent e) {
  327.  
  328. }
  329. }


Grüße
Wambui


JComboBox auffristen nach Listenänderung

0 commentaires:

Enregistrer un commentaire