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.
Als Alternative habe ich im Reset-Listener
einfügt. Der Focus oder die Aktivierung des Hauptfenster erzeugt überhaupt keine Reaktion. Und der Resetbutton wirft diese Exception:
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.
Grüße
Wambui
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:
@Override
public void windowActivated(WindowEvent e) {
myComboBoxModel.reload();
targetCityComboBox.setModel(myComboBoxModel);
}
Als Alternative habe ich im Reset-Listener
Java:
myComboBoxModel.reload();
targetCityComboBox.setModel(myComboBoxModel);
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)
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:
package com.example.calculator;
import java.util.Collections;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
public class ComboBoxModel extends DefaultComboBoxModel<String> {
private static final long serialVersionUID = 1L;
private XMLCreator cityList = new XMLCreator();
public void reload() {
removeAllElements();
Vector<String> sortedCityList = new Vector<String>();
for (int i = 0; i < cityList.CityList().size(); i++) {
sortedCityList.add(cityList.CityList().get(i));
}
Collections.sort(sortedCityList);
for (int i = 0; i < sortedCityList.size(); i++) {
addElement("" + sortedCityList.get(i));
}
}
public int setText(String cityName) {
int cityId = 0;
for (int i = 0; i < cityList.CityList().size(); i++) {
if (cityList.CityList().get(i).equals(cityName)) {
cityId = cityList.CityList().indexOf(cityName);
}
}
return cityId;
}
}
Java:
package com.example.calculator.gui;
import com.linuxmaker.calculator.*;
import com.linuxmaker.calculator.ComboBoxModel;
import com.toedter.calendar.JDateChooser;
import org.w3c.dom.DOMException;
import org.xml.sax.SAXException;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;
import java.awt.*;
import java.awt.Container;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.text.DecimalFormat;
import java.io.File;
import java.text.SimpleDateFormat;
public class MainGui extends JFrame implements WindowListener {
// Variables declaration
private JComboBox targetCityComboBox;
private JComboBox stateComboBox;
private JLabel feeLabel;
private ComboBoxModel myComboBoxModel;
private Double fee;
private int projektdays;
// End of variables declaration
/**
* Creates new form MainGui
*/
public MainGui() {
initComponents();
}
/**
* Components of the form
*/
private void initComponents() {
targetCityLabel.setText("Projektstadt");
targetCityLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
targetCityComboBox = new JComboBox<>();
targetCityComboBox.setFont(new Font("Dialog", Font.PLAIN, 11));
targetCityComboBox.setEditable(false);
myComboBoxModel = new ComboBoxModel();
// Sets ComboBoxModel
myComboBoxModel.reload();
targetCityComboBox.setModel(myComboBoxModel);
/**
* Creates the GUI-Layout
*/
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
setIconImage(new ImageIcon(getClass().getResource("/resources/images16x16/currency_euro_yellow.png")).getImage());
setTitle("Freelancer - Honorarrechner");
setLocationRelativeTo(null);
BorderLayout layout = new BorderLayout();
getContentPane().setLayout(layout);
getContentPane().setBackground(new Color(250, 219, 108));
//======== dialogPane ========
{
dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
dialogPane.setLayout(new BorderLayout());
//======== contentPanel ========
{
contentPanel.setLayout(new GridBagLayout());
((GridBagLayout)contentPanel.getLayout()).columnWidths = new int[] {60, 35, 0, 63, 94, 0};
((GridBagLayout)contentPanel.getLayout()).rowHeights = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
((GridBagLayout)contentPanel.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
((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};
contentPanel.add(originCityLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPanel.add(originCityTextField, new GridBagConstraints(2, 0, 3, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 0), 0, 0));
contentPanel.add(targetCityLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPanel.add(targetCityComboBox, new GridBagConstraints(2, 1, 3, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 0), 0, 0));
contentPanel.add(feeLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPanel.add(feeTextField, new GridBagConstraints(2, 2, 2, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPanel.add(cur1Label, new GridBagConstraints(4, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 0), 0, 0));
contentPanel.add(scontoComboBox, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPanel.add(scontoLabel, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPanel.add(projectDaysLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPanel.add(projectStartChooser, new GridBagConstraints(2, 4, 2, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPanel.add(stateComboBox, new GridBagConstraints(4, 4, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPanel.add(cur2Label, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPanel.add(projectEndChooser, new GridBagConstraints(2, 5, 2, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPanel.add(projectDaysTextField, new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
contentPanel.add(cur3Label, new GridBagConstraints(3, 6, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
{
settingsPanel.setBorder(new TitledBorder(null, "Weitere Einstellungen", TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION,
new Font("Dialog", Font.PLAIN, 11), Color.blue));
settingsPanel.setLayout(new GridBagLayout());
((GridBagLayout)settingsPanel.getLayout()).columnWidths = new int[] {150, 27, 169, 0};
((GridBagLayout)settingsPanel.getLayout()).rowHeights = new int[] {0, 0, 0};
((GridBagLayout)settingsPanel.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 1.0};
((GridBagLayout)settingsPanel.getLayout()).rowWeights = new double[] {0.0, 0.0, 1.0};
settingsPanel.add(overnightCheckBox, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
settingsPanel.add(carCheckBox, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 0), 0, 0));
settingsPanel.add(saturdayWork, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
settingsPanel.add(hoursPerDayTextField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5, 0, 5, 0), 0, 0));
settingsPanel.add(hoursPerDayLabel, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 5, 0, 0), 0, 0));
buttonGroup.add(netFeeRadioButton);
settingsPanel.add(netFeeRadioButton, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 5), 0, 0));
buttonGroup.add(allinFeeRadioButton);
settingsPanel.add(allinFeeRadioButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
}
contentPanel.add(settingsPanel, new GridBagConstraints(0, 7, 5, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 0), 0, 0));
{
resultPanel.setLayout(new GridBagLayout());
((GridBagLayout)resultPanel.getLayout()).columnWidths = new int[] {108, 115, 89, 0};
((GridBagLayout)resultPanel.getLayout()).rowHeights = new int[] {0, 0, 0, 0};
((GridBagLayout)resultPanel.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 1.0};
((GridBagLayout)resultPanel.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 1.0};
resultPanel.add(dayHonorarLabel, new GridBagConstraints(0, 9, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
resultPanel.add(dayValueLabel, new GridBagConstraints(2, 9, 1, 1, 0.0, 0.0,
GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
new Insets(0, 0, 5, 5), 0, 0));
resultPanel.add(cur4Label, new GridBagConstraints(3, 9, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 0), 0, 0));
resultPanel.add(hourHonorarLabel, new GridBagConstraints(0, 10, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 5), 0, 0));
resultPanel.add(hourValueLabel, new GridBagConstraints(2, 10, 1, 1, 0.0, 0.0,
GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
new Insets(0, 0, 5, 5), 0, 0));
resultPanel.add(cur5Label, new GridBagConstraints(3, 10, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 5, 0), 0, 0));
resultPanel.add(turnoverLabel, new GridBagConstraints(0, 11, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 5), 0, 0));
resultPanel.add(turnoverValueLabel, new GridBagConstraints(2, 11, 1, 1, 0.0, 0.0,
GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
new Insets(0, 0, 0, 5), 0, 0));
resultPanel.add(cur6Label, new GridBagConstraints(3, 11, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
}
contentPanel.add(resultPanel, new GridBagConstraints(0, 8, 5, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
}
dialogPane.add(contentPanel, BorderLayout.CENTER);
}
add(dialogPane, BorderLayout.CENTER);
//======== buttonBar ========
{
buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
buttonBar.setLayout(new GridBagLayout());
((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 85, 80};
((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0, 0.0};
buttonBar.add(calculateButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 12, 5), 0, 0));
buttonBar.add(resetButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 12, 5), 0, 0));
buttonBar.add(endButton, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 12, 12), 0, 0));
}
add(buttonBar, BorderLayout.SOUTH);
// Create the menuBar
{
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
editMenu.add(addMenuItem);
editMenu.add(changeMenuItem);
editMenu.add(settingsMenuItem);
menuBar.add(editMenu);
infoMenu.add(helpMenuItem);
infoMenu.add(aboutMenuItem);
menuBar.add(infoMenu);
}
setJMenuBar(menuBar);
pack();
/** End of Creates the GUI-Layout **/
/**
* Button Listeners and Action Listeners
*/
targetCityComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dayValueLabel.setVisible(false);
hourValueLabel.setVisible(false);
turnoverValueLabel.setVisible(false);
cur2Label.setVisible(false);
cur3Label.setVisible(false);
cur4Label.setVisible(false);
cur5Label.setVisible(false);
cur6Label.setVisible(false);
XMLCreator xmlelement = new XMLCreator();
if (Double.parseDouble(xmlelement.readXml((String) targetCityComboBox.getSelectedItem()).get(1)) < 225.0 &&
Double.parseDouble(xmlelement.readXml((String) targetCityComboBox.getSelectedItem()).get(1)) > 80.0) {
overnightCheckBox.setEnabled(true);
} else {
overnightCheckBox.setEnabled(false);
}
}
});
//---- resetButton ----
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
originCityTextField.setText(new Settings().readSettings("pointOfDeparture"));
feeTextField.setText("0.00");
scontoComboBox.setSelectedIndex(2);
hoursPerDayTextField.setText(String.valueOf(workingHours));
dayValueLabel.setVisible(false);
hourValueLabel.setVisible(false);
hourHonorarLabel.setVisible(true);//TODO Projektstadt reseten, wenn sich Ausgangsstadt ändert
dayHonorarLabel.setVisible(true);
cur2Label.setVisible(false);
cur3Label.setVisible(false);
turnoverLabel.setVisible(true);
turnoverValueLabel.setVisible(false);
cur4Label.setVisible(false);
cur5Label.setVisible(false);
cur6Label.setVisible(false);
projectDaysTextField.setText("");
projectEndChooser.setEnabled(false);
saturdayWork.setEnabled(false);
myComboBoxModel.reload();
targetCityComboBox.setModel(myComboBoxModel);
}
});
//---- endButton ----
endButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Container frame = endButton.getParent();
do {
frame = frame.getParent();
} while (!(frame instanceof JFrame));
((JFrame) frame).dispose();
}
});
}
/**
* Launch the application.
*/
public static void main(String[] args) {
/* Create and display the form */
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainGui frame = new MainGui();
frame.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
myComboBoxModel.reload();
targetCityComboBox.setModel(myComboBoxModel);
}
@Override
public void windowDeactivated(WindowEvent e) {
}
}
Grüße
Wambui
Zuletzt bearbeitet: