JComboBox auffristen nach Listenänderung

Wambui

Aktives Mitglied
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:
@Override
    public void windowActivated(WindowEvent e) {
        myComboBoxModel.reload();
        targetCityComboBox.setModel(myComboBoxModel);
    }

Als Alternative habe ich im Reset-Listener
Java:
myComboBoxModel.reload();
                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:
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:

Enceladus271

Bekanntes Mitglied
1. Kann es sein dass du vergessen hast den WindowListener mit addWindowListener zum Fenster hinzuzufügen? Ich kann das in deinem geposteten Code nirgends sehen.

2. Laut Exception rufts du in der actionPerformed bei einer ArrayList die get Methode auf. Im Code ist davon allerdings nichts zu sehen. Die Exception kann in dieser Form bei diesem Code nicht aufgetreten sein.
 

Wambui

Aktives Mitglied
Stimmt, das passiert mir öfters nachdem ich mich von den GuiBuildern entwöhnt habe und die GUI selber schreibe. Da vergesse ich schnell das add.

Mit dem zweiten Punkten melde ich mich noch mal. Dann mit den gezielten Programmstellen, denn der Code ist zu lang in der MainGui-Klasse.
 
Zuletzt bearbeitet:

Wambui

Aktives Mitglied
Ich kann mein Problem inzwischen soweit eingrenzen, dass ein Listener beim Programmstart eine
IndexOutOfBoundsException wirft.
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$2.actionPerformed(MainGui.java:532)
Das ist diese Stelle:
Java:
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);
                }
Ich würde das so lösen, dass hier nur abgefragt werden kann, wenn die Liste geladen ist. Hier fehlen mir die Ansatzpunkte zur Umsetzung.

Gesamtcode der Klasse, die anderen beiden sind oben veröffentlicht. Ich habe Teile rausgenommen, da sonst der Post zu lang wird. "MainGui.java:532" entspricht hier Zeile 256.
Java:
package com.example.calculator.gui;

import com.example.calculator.*;
import com.example.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;

/**
 * Created by Wambui Mustafa
 * Date: 18.02.15
 */
public class MainGui extends JFrame {
    // Variables declaration
   
    private JComboBox targetCityComboBox;
    private JComboBox stateComboBox;
    
    private GridBagConstraints gridBagConstraints;
    
    // End of variables declaration

    /**
     * Creates new form MainGui
     */
    public MainGui() {
        initComponents();
    }

    private void thisWindowGainedFocus(WindowEvent e) {
        myComboBoxModel.reload();
        targetCityComboBox.setModel(myComboBoxModel);
    }

    /**
     * Components of the form
     */
    private void initComponents() {
       
        targetCityLabel = new JLabel();
        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);
        stateComboBox = new JComboBox();
      

        /**
         * 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 ========
        {
            
        }
        add(buttonBar, BorderLayout.SOUTH);

        // Create the menuBar
        {
            
        }
        setJMenuBar(menuBar);
        pack();
        /** End of Creates the GUI-Layout **/

        /**
         * Button Listeners and Action Listeners
         */
        addWindowFocusListener(new WindowAdapter() {
            @Override
            public void windowGainedFocus(WindowEvent e) {
                thisWindowGainedFocus(e);
            }
        });
        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);
                }
            }
        });
        
        

        //---- calculateButton ----
        calculateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                XMLCreator xmlelement = new XMLCreator();
                Fee price = new Fee();
                Double sconto = 0.0;
                DecimalFormat f = new DecimalFormat("#0.00");
                String city = (String) targetCityComboBox.getSelectedItem();
                String fileName = System.getProperties().getProperty("user.home")+ File.separator + path + File.separator + originCityTextField.getText() + "City.xml";
                File xmlFile = new File(fileName);
                if (feeTextField.getText().replace(',', '.').matches("\\d+([.]{1}\\d+)?")) {
                    fee = Double.parseDouble(feeTextField.getText().replace(',', '.'));
                } else {
                    JOptionPane.showMessageDialog(null, "Honorar: Es sind nur Zahlenwerte erlaubt!");
                }
                Double travelDistance = Double.parseDouble(xmlelement.readXml(city).get(1));
                Double hoursPerDay = Double.parseDouble(hoursPerDayTextField.getText());
                try {
                    projektdays = Integer.parseInt((String) projectDaysTextField.getText());
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null, "Manntage: Es sind nur Zahlenwerte erlaubt!");
                }
                if (scontoComboBox.getSelectedItem() == "0") {
                    sconto = 1.0;
                } else {
                    sconto = Double.parseDouble((String) scontoComboBox.getSelectedItem())/100 + 1.0;
                }

                /**
                 * Checking whether the XML file of the selected city already exists
                 */
                if (xmlFile.exists()) {
                    Double netFee = price.netFeeCalculator(
                            city,
                            travelDistance,
                            fee,
                            hoursPerDay,
                            sconto,
                            projektdays,
                            overnightCheckBox.isSelected(),
                            carCheckBox.isSelected());
                    Double grossFee = price.feeCalculator(
                            city,
                            travelDistance,
                            fee,
                            hoursPerDay,
                            sconto,
                            projektdays,
                            overnightCheckBox.isSelected(),
                            carCheckBox.isSelected());
                    Double turnover = price.netTurnover(price.netFeeCalculator(
                                    city,
                                    travelDistance,
                                    fee,
                                    hoursPerDay,
                                    sconto,
                                    projektdays,
                                    overnightCheckBox.isSelected(),
                                    carCheckBox.isSelected()),
                            hoursPerDay,
                            projektdays);
                    /**
                     * Check if discount was selected and allocation of that value
                     */
                    if (netFeeRadioButton.isSelected()) {
                        dayValueLabel.setText(String.valueOf(f.format(price.feeCalculator(
                                city,
                                travelDistance,
                                fee,
                                hoursPerDay,
                                sconto,
                                projektdays,
                                overnightCheckBox.isSelected(),
                                carCheckBox.isSelected()))));
                        hourValueLabel.setText(String.valueOf(f.format(grossFee/hoursPerDay)));
                        turnoverValueLabel.setText(String.valueOf(f.format(price.netTurnover(fee, hoursPerDay, projektdays))));
                        feeTextField.setToolTipText("Gewünschtes Tageshonorar oder Stundensatz");
                    }

                    /**
                     * Calculation of the net price of a given allin price
                     */
                    if (allinFeeRadioButton.isSelected()) {
                        dayValueLabel.setText(String.valueOf(f.format(netFee)));
                        hourValueLabel.setText(String.valueOf(f.format(netFee/hoursPerDay)));
                        turnoverValueLabel.setText(String.valueOf(f.format(turnover)));
                        feeTextField.setToolTipText("Angebotenes Tageshonorar oder Stundensatz");
                    }
                    if (feeTextField.getText().equals("0") || feeTextField.getText().equals("0,0")) {
                        dayHonorarLabel.setText("Reisekosten");
                        hourHonorarLabel.setText("Reisekosten/h");
                        turnoverValueLabel.setVisible(false);
                        cur4Label.setVisible(true);
                        cur5Label.setVisible(true);
                    } else {
                        dayHonorarLabel.setText("Tageshonorar");
                        hourHonorarLabel.setText("Stundenhonorar");
                        turnoverLabel.setText("Umsatz nach Steuern");
                        turnoverValueLabel.setVisible(true);
                        turnoverLabel.setVisible(true);
                        cur4Label.setVisible(true);
                        cur5Label.setVisible(true);
                        cur6Label.setVisible(true);
                    }

                    dayValueLabel.setVisible(true);
                    hourValueLabel.setVisible(true);
                    dayHonorarLabel.setVisible(true);
                    hourHonorarLabel.setVisible(true);
                    cur2Label.setVisible(true);
                    cur3Label.setVisible(true);
                } else {
                    XmlFileWriter newCity = new XmlFileWriter();
                    try {
                        newCity.xmlWrite(originCityTextField.getText());
                        JOptionPane.showMessageDialog(null, "Für die Stadt " + originCityTextField.getText() + " existieren noch keine Einträge!");
                    } catch (DOMException
                            | XPathExpressionException | IOException
                            | SAXException | TransformerException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        //---- 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();
                }
            }
        });
    }
}
 
Zuletzt bearbeitet:

Wambui

Aktives Mitglied
Wie ich das ComboBox-Modell baue für das Holen der Daten aus der XML-Datei für die Liste, ist hier sichtbar:
Java:
myComboBoxModel = new ComboBoxModel();
Java:
package com.example.calculator;

import java.util.Collections;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;

/**
 * Created by @author Wambui Mustafa on 29.01.15.
 */
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;
    }

}
Wie gesagt, ich glaube, es reicht aus die If-Abfrage nur dann ablaufen zu lassen, wenn geprüft ist, dass die Liste bereits existiert.
 
Zuletzt bearbeitet:

Enceladus271

Bekanntes Mitglied
Was mir auffällt: Warum erzeugst du in der actionPerformed einen neuen XMLCreator? Es macht doch bestimmt mehr Sinn den XMLCreator vom Model zu nehmen. Denn dort hast du doch schon irgendwie Daten eingelesen.

Wann liest der XMLCreator eigentlich die Daten ein? Schon im Konstuktor?
 

Wambui

Aktives Mitglied
Der XMLCreator wird eigentlich nur für den OK-/Berechnen-Button genutzt. Hier werden die Daten aus dem GUI-Formular mit den Werten aus der XML-Datei in Methoden übergeben, damit die Ergebnisse generiert werden können. Und dann eben auch noch in der actionPerformed des targetCityComboBox.addActionListener. Diese Abfrage benötige ich, um eine Checkbox zu enablen bzw. disablen, je nach den Werten in der XML-Datei.
Der XMLCreator sieht so aus
Java:
package com.example.calculator;

import static com.example.calculator.Constants.ATTRIBUTE_NAME;
import static com.example.calculator.Constants.ELEMENT_CITY;
import static com.example.calculator.Constants.ELEMENT_DISTANCE;
import static com.example.calculator.Constants.ELEMENT_DURATION;
import static com.example.calculator.Constants.ELEMENT_HOTEL;
import static com.example.calculator.Constants.ELEMENT_TICKET;
import static com.example.calculator.Constants.ELEMENT_DTICKET;
import static com.example.calculator.Constants.ELEMENT_PUBLICTRANSPORT;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.xpath.XPathFactory;
import org.jdom2.xpath.XPathExpression;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

/**
 * Created by @author Wambui Mustafa on 29.01.15.
 */
public class XMLCreator {
    private static String path = System.getProperties().getProperty("user.home")+
            File.separator+PlatformDependence.offerValue("directory")+
            File.separator+new Settings().readSettings("pointOfDeparture")+"City.xml";
    //TODO Abfragen, ob die XML-Datei existiert!
    private static File xmlFile = new File(path);
    private Namespace ns = Namespace.getNamespace("http://www.example.com/Preiskalkulator");
    private File defaultXmlFile = new File("resources"+File.separator+"City.xml"); //TODO Jar-tauglich machen!
    private String origin = new Settings().readSettings("pointOfDeparture");
    private String publictransport;
    private SAXBuilder builder = new SAXBuilder();
    private Document jdomDoc;

    public XMLCreator() {
        super();
    }

    public void writeXML(String cityName,
                         String ticket,
                         String dticket,
                         String hotel,
                         String publictransport)
            throws XPathExpressionException, SAXException,
            ParserConfigurationException, JDOMException {
        Parser distance = new Parser();
        Parser duration = new Parser();
        try {
            if (ticket.length() == 0) {
                ticket = "0.00";
            }
            if (dticket.length() == 0) {
                dticket = "0.00";
            }
            if (hotel.length() == 0) {
                hotel = "0.00";
            }
            SAXBuilder builder = new SAXBuilder();
            Document document = builder.build(xmlFile);
            Element xmlRoot = document.getRootElement();
            Element city = new Element(ELEMENT_CITY);
            city.setAttribute(ATTRIBUTE_NAME, cityName);
            city.addContent(new Element(ELEMENT_DISTANCE).setText(String.valueOf(distance.parsedistance(origin, cityName))));
            city.addContent(new Element(ELEMENT_DURATION).setText(String.valueOf(duration.parseduration(origin, cityName))));
            city.addContent(new Element(ELEMENT_TICKET).setText(ticket));
            city.addContent(new Element(ELEMENT_DTICKET).setText(dticket));
            city.addContent(new Element(ELEMENT_HOTEL).setText(hotel));
            city.addContent(new Element(ELEMENT_PUBLICTRANSPORT).setText(publictransport));
            xmlRoot.addContent(city);

            XMLOutputter xmlOutput = new XMLOutputter();
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(document, new FileWriter(path));

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void changeXML(String cityName,
                          String varMonthlyTicket,
                          String varRoundTripTicket,
                          String varHotel,
                          String varPublictransport) {
        try {
            jdomDoc = builder.build(xmlFile);
            Element costCalculator = jdomDoc.getRootElement();
            Iterator<?> cityList = costCalculator.getChildren(ELEMENT_CITY).iterator();
            while (cityList.hasNext()) {
                Element city = (Element) cityList.next();
                if (cityName.equals(city.getAttribute("name").getValue())) {
                    Element monthlyTicket = new Element(ELEMENT_TICKET);
                    monthlyTicket.addContent(varMonthlyTicket);
                    city.removeChild(ELEMENT_TICKET);
                    city.addContent(monthlyTicket);
                    Element roundTripTicket = new Element(ELEMENT_DTICKET);
                    roundTripTicket.addContent(varRoundTripTicket);
                    city.removeChild(ELEMENT_DTICKET);
                    city.addContent(roundTripTicket);
                    Element hotelCost = new Element(ELEMENT_HOTEL);
                    hotelCost.addContent(varHotel);
                    city.removeChild(ELEMENT_HOTEL);
                    city.addContent(hotelCost);
                    Element publicTransport = new Element(ELEMENT_PUBLICTRANSPORT);
                    publicTransport.addContent(varPublictransport);
                    city.removeChild(ELEMENT_PUBLICTRANSPORT);
                    city.addContent(publicTransport);
                }
            }

            XMLOutputter xmlOutput = new XMLOutputter();
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(jdomDoc, new FileWriter(path));

        } catch (JDOMException | IOException ex) {
            ex.printStackTrace();
        }
    }

    public void removeElement(String cityName) {
        try {
            jdomDoc = builder.build(xmlFile);
            Element costCalculator = jdomDoc.getRootElement();
            Iterator<Element> cities = costCalculator.getChildren("city").iterator();
            while (cities.hasNext()) {
                Element city = cities.next();
                if (cityName.equals(city.getAttribute("name").getValue())) {
                    cities.remove();
                }
            }
            XMLOutputter xmlOutput = new XMLOutputter();
			xmlOutput.setFormat(Format.getPrettyFormat());
			xmlOutput.output(jdomDoc, new FileWriter(xmlFile));
        } catch (JDOMException | IOException ex) {
            ex.printStackTrace();
        }
    }

    public List<String> readXml(String cityName) {
        List<String> results  = new ArrayList<String>();
        List<Element> elements = getCityElements(cityName);
        String result = null;
        if(elements != null) {
            results.add(cityName);
            for (Element element : elements) {
                results.add(element.getValue());
            }
        }
        return results;
    }

    /**
     * Read all elements of the document
     */
    private static List<Element> getRootElements() {
        SAXBuilder builder = null;
        Document jdomDoc = null;
        try {
            builder = new SAXBuilder();
            jdomDoc = builder.build(xmlFile);
        } catch (JDOMException ex) {
            System.err.println(ex);
        } catch (IOException ex) {
            System.err.println(ex);
        }
        if(builder != null && jdomDoc != null) {
            return jdomDoc.getRootElement().getChildren();
        }
        else {
            return null;
        }
    }

    private static List<Element> getCityElements(String cityName){
        List<Element> cities = getRootElements();
        if(cities != null) {
            for (Element element : cities) {
                if(element.getAttributeValue("name").equals(cityName)) {
                    return element.getChildren();
                }
            }
        }
        return null;
    }


    public List<String> CityList() {
        List<String> CityList = new ArrayList<>();
        try {
            if (new File(System.getProperties().getProperty("user.home")+
                    File.separator+PlatformDependence.offerValue("directory")+
                    File.separator+PlatformDependence.offerValue("setting")).exists()) {
                jdomDoc = builder.build(xmlFile);
            } else { //TODO Überprüfen wegen JAR-File!
                jdomDoc = builder.build(defaultXmlFile);
            }
            XPathFactory xFactory = XPathFactory.instance();
            XPathExpression xpath = xFactory.compile("/costcalculator/city/@name");
            for (Object object : xpath.evaluate(jdomDoc)) {
                CityList.add(((Attribute)object).getValue());
            }
        } catch (JDOMException | IOException ex) {
            ex.printStackTrace();
        }
        return CityList;
    }
}
Das stimmt, das hast Du Recht, ich habe die Initialisierung des XMLCreator in den Konstruktor gelegt. Allerdings ändert das nichts an der IndexOutOfBoundsException an der gleich Stelle. Wenn ich die Abfrage auskommentiere, dann funktioniert die JComboBox, sobald ich hier die Liste ändere und das Hauptfenster wieder den Fokus bekommt ohne Fehler und ohne Schluckauf.
Allerdings lässt sich so die JCheckbox nicht steuern, weil die davon abhängt, welche Kilometer-Werte der in der JComboBox ausgewählte Wert liefert.
Deswegen dachte an so etwas wie eine zeitliche Verzögerung, Thread, die dafür dass die Werte da sind, wenn die Aktion ausgeführt werden soll.
 

Enceladus271

Bekanntes Mitglied
Ich glaube da hilft nur in der actionPerformed einen Haltepunkt zu setzten und das mal zu debuggen, oder wenigstens mit Ausgaben auf der Konsole zu überprüfen was z.B. getRootElements oder getCityElements zurückliefern. Ich denke da ist entweder ein Fehler im Code oder fehlende Elemente in der XML-Datei. Eine Ferndiagnose ist da ziemlich schwer.
 

Wambui

Aktives Mitglied
Ich habe das jetzt mal versucht zu debuggen (Intelij 14), das ist wirklich haarig. Ich habe das Problem jetzt noch nicht lösen können. Dann muss ich die unelegantere Methode anwenden mit einem ToolTip an der JCheckbox, wann sie Sinn macht aktiviert zu werden. Ich wollte das zwar so lösen, dass das Programm selber anhand der userspezifischen Werte erkennt, ob ein Enablen oder Disablen angesagt ist.
Eines kann ich weitergeben, der Debugger von Intelij ist wesentlich besser als der von Eclipse. Er schreibt in den Sourcecode andersfarbig die aktuellen Werte der Variablen hinein, so dass man ständig im Code bleiben kann. Und er zeigt auch die anderen Klassen, was da gerade abgearbeitet wird.

Grüße
Wambui
 
Zuletzt bearbeitet:

Tom299

Bekanntes Mitglied
Code:
        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);
                }
            }
        });

- Du greifst mit get(1) immer auf ein Item zu, aber wenn du ein Reset machst, dann ist deine CB doch sicher leer, also gibts kein get(1) und deswegen OutOfBounds. Vielleicht einfach die size() oder sowas abfragen bevor du den Zugriff machst. mit get(1) greifst du auch auf das 2. Element zu, get(0) ist das 1. element, nur nochmal zur info.

- Wieso liest du deine XML-Datei nicht in eine Objekt-Struktur (ArrayList, Map) und greifst dann später darauf zu?
Code:
               if (Double.parseDouble(xmlelement.readXml((String) targetCityComboBox.getSelectedItem()).get(1)) < 225.0 &&
                        Double.parseDouble(xmlelement.readXml((String) targetCityComboBox.getSelectedItem()).get(1)) > 80.0) {
liest sich jedenfalls furchtbar :(
 

Wambui

Aktives Mitglied
Vielen Dank für das Feedback:
- Du greifst mit get(1) immer auf ein Item zu, aber wenn du ein Reset machst, dann ist deine CB doch sicher leer, also gibts kein get(1) und deswegen OutOfBounds. Vielleicht einfach die size() oder sowas abfragen bevor du den Zugriff machst. mit get(1) greifst du auch auf das 2. Element zu, get(0) ist das 1. element, nur nochmal zur info.
Deshalb hatte ich ja erwogen, etwas zu machen, dass die CB wieder füllt. Das ist mir klar, dass diese bei einem Reset leer ist, deswegen mache ich dasselbe wie bei der Initialisierung. Und Du meinst eine Abfrage nach der size() > 0 füllt die CB? Ich muss doch in jedem Fall das Modell reloaden, denke ich mir.
- Wieso liest du deine XML-Datei nicht in eine Objekt-Struktur (ArrayList, Map) und greifst dann später darauf zu?
liest sich jedenfalls furchtbar :(
Weil ich offen gesagt nicht weiß, wie das aussehen soll und was sich gegenüber der jetztigen Vorgehensweise ändert. Für mich liest sich der Part so:
"Lese mir für das übergebene CB-Item das 2.Element des XML-Knotens aus und vergleiche, ob der Double-Wert < 225 ist. Bzw. das Gleiche für > 80". Warum jetzt einen umfangreichen XML-Baum in eine ArrayList quetschen. Das verkompliziert die Sache für mich.
 

Tom299

Bekanntes Mitglied
Ich meinte ja zuerst ein Objekt (z.B. TargetCity) erstellen (mit den Daten aus dem XML) und die Objekte dann in eine ArrayList oder Map stecken. Dann kannste dir z.B. das Double-parsen sparen bzw. mußt das nur beim Erstellen des Objektes machen und du kannst auch gleich eine Methode isOvernight() implementieren und eine toString() zum Anzeigen in der ComboBox ect. Ist bischen mehr Aufwand, aber dafür sicher besser les- und wartbar.
 

Wambui

Aktives Mitglied
Okay, das leuchtet mir durchaus ein. Aber dieses Objekt "targetCity" müsste ich ebenso ständig mit jeder neuen Auswahl in der CB neu generieren. Ausserdem die XML-Datei sieht gekürzt so aus (bei mir sind da bereits 80 Städte drin)
HTML:
<?xml version="1.0" encoding="UTF-8"?>
<costcalculator>
  <city name="München">
    <distance>221.357</distance>
    <duration>2.3294444444444444</duration>
    <monthlyticket>547.00</monthlyticket>
    <roundtripticket>114.0</roundtripticket>
    <hotelcosts>0.00</hotelcosts>
    <publictransport>false</publictransport>
  </city>
  <city name="Frankfurt">
    <distance>204.365</distance>
    <duration>2.0494444444444446</duration>
    <monthlyticket>467.00</monthlyticket>
    <roundtripticket>126.00</roundtripticket>
    <hotelcosts>0.00</hotelcosts>
    <publictransport>false</publictransport>
  </city>
  <city name="Köln">
    <distance>367.153</distance>
    <duration>3.4047222222222224</duration>
    <monthlyticket>292.40</monthlyticket>
    <roundtripticket>185.00</roundtripticket>
    <hotelcosts>49.00</hotelcosts>
    <publictransport>false</publictransport>
  </city>
</costcalculator>
Ich bräuchte strenggenommen für jede Stadt eigene Objekte Duration, Distance, Monthlyticket usw. Ich glaube nicht, dass dadurch eine Übersichtlichkeit respektive bessere Wartbarkeit entsteht.
 

Wambui

Aktives Mitglied
Das mag zwar richtig sein, aber für mich ist das unrealistisch. Ich habe über Deinen Vorschlag vorhin ausgiebig reflektiert. Er hat Charme, sicher. Aber ich würde hier Objekte wie
Java:
Double Distanz = new XmlCreator.readXml((String) targetCityComboBox.getSelectedItem()).get(1));
Double Dauer = new XmlCreator.readXml((String) targetCityComboBox.getSelectedItem()).get(2));
usw. anlegen. Zu einem Zeitpunkt, wenn das Programm startet und der Anwender noch gar nichts in targetCityComboBox ausgewählt hat. Anders ausgedrückt, ich brauche für die Objekte die Information "Welche Stadt", die ich zum Zeitpunkt der Deklaration und Initialisierung noch gar nicht kenne.
Ohnehin verlagere ich damit doch nur das beschriebene Probleme zum Zeitpunkt der Objektinitialisierung.
Nee, du brauchst für jede Stadt ein Objekt mit den Attributen Name -> München, Distanz, Dauer, Hotelkosten usw.
Das, würde ich sagen, verkompliziert das Ganze noch. Denn hier müsste ich, wenn die XML-Datei wächst, auch neue städtespezifische Objekte plus dem eben Gesagten. Also für mich sieht Dein Gedanke danach aus, dass man bei der Programmierung nur die Anzahl der Städte als auch ihre Namen bereits kennt.

Ich kann mich täuschen, aber ich lasse mich auch gerne überzeugen.
 

Tom299

Bekanntes Mitglied
Ich bin davon ausgegangen, daß du eine große XML-Datei hast, wo alle Städte drinstehen mit ihren Daten. Und ich würde dann hingehen, und die Daten in eine Objektstruktur schaufeln bzw. falls verfügbar auch in eine DB importieren.
Aber ich kenn deine Anforderungen nicht wirklich und es kann ja sein, daß deine XML-Struktur zur Laufzeit generiert wird, je nachdem, welchen Start-Ort du auswählst. Dann wäre es ja unnütz, die ganze Datei einzulesen, so ich wie das vor habe.
Sollte aber prinzipiell halt nur ein Vorschlag sein, wie man das ganze etwas übersichtlicher programmieren kann. Ich versuche immer wenn möglich alles in Objekte zu bringen, und dann damit zu arbeiten.
 

Wambui

Aktives Mitglied
Deswegen diskutieren wir hier ja auch. Die XML-Datei könnte genauso eine Datenbank sein, beides dient zur lokalen Speicherung der Daten der Anwender. Und da ich jetzt noch keine Erfahrung habe mit der javaeigenen SQLEngine und der lokalen Ablage der Datenbank auf Desktop und Android, habe ich XML als Speichermedium gewählt.
Möglicherweise hätte ich dasselbe Problem mit SQL, weil die Daten in der CB entsprechend schnell parat sein sollten.
Jedenfalls besten Dank für die Diskussion.
 

Tom299

Bekanntes Mitglied
Achso, versteh ich das richtig, daß du die XML-Datei nicht übers Web von einem WebService oder so bekommst sondern selbst die Datei erstellst, um Daten zu speichern?
Dann würde ich auf jeden Fall eine DB bevorzugen, auf Android bekommt man eh schon die SQLite geschenkt und auf PC-Seite würde sich dann auch eine File-DB wie SQLite oder H2 anbieten. MySQL oder PostgreSQL sind wohl eher ungeeignet, wenn du nur ein paar Daten in einem File speichern willst. Mit H2 hab ich nur experimentelle Erfahrung, mit SQLite hab ich unter Android schon gearbeitet, die ist schon flott, würde sich also auch auf dem PC anbieten. Hab auch grad mal ein bischen damit rumgespielt, klapp einwandfrei.
 

Wambui

Aktives Mitglied
Richtig!
Achso, versteh ich das richtig, daß du die XML-Datei nicht übers Web von einem WebService oder so bekommst sondern selbst die Datei erstellst, um Daten zu speichern?
Das ist im Prinzip mein Gesellenstück mit GUI (ohne WindowBuilder) und XML-Datei erstellen, verändern, lesen, schreiben etc. Und SQLite habe ich noch nicht verstanden, wo dort die Datei auf dem Filesystem abgelegt wird und wie das ohne SQL-Server technisch geht. Ich hole mir aus dem Web bei einer Google-API auch eine XML-Datei aus der die Entfernungen, Zeiten zwischen zwei Städten geliefert werden. Das muss aber in eine XML-Datei vorort, damit die Kostenkalkulation auch ohne Internet läuft. Das brauche ich nur, wenn ich eine neue Zielstadt anlege.
Deshalb hatte ich mich für XML entschieden, zumal ich das aus der Linux-Administration als ideales Instrument bei automatischen Installationsskripten kennengelernt habe.
 
Zuletzt bearbeitet:

Tom299

Bekanntes Mitglied
Also SQLite ist ganz einfach:
- Java-Treiber gibts hier https://bitbucket.org/xerial/sqlite-jdbc/downloads
- Den zu deinem Projekt hinzufügen
- SQLite Expert Personal (oder Alternative) gibts hier SQLite Expert - The expert way to SQLite.
- Damit kannst du eine DB anlegen (z.b. demo.db) und Tabellen etc. erstellen und die Datei dann abspeichern. Damit hast du dann schon eine File-DB, die du mittels dem Java-Treiber dann ansprechen kannst, genauso wie MySQL, usw. Nur liegt das File bei dir lokal
- Einbinden des Treibers z.B. so:
Code:
package de.test.zeitmanagement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DatabaseConnectorSQLite {

	public DatabaseConnectorSQLite() {
		try {
			// The newInstance() call is a work around for some broken Java implementations
			Class.forName("org.sqlite.JDBC").newInstance();
		} 
		catch (Exception ex) {
			// handle the error
			System.err.println("Datenbank-Treiber (SQLite) konnte nicht geladen werden!");
		}
	}
	
	public Connection getConnection() {
		Connection connection = null;

		try {
			connection = DriverManager.getConnection("jdbc:sqlite:D:/WS_TEST/Zeitmanagement/bin/database/demo.db");
		}
		catch (SQLException ex) {
			// handle any errors
			System.err.println("SQLException: " + ex.getMessage());
			System.err.println("SQLState: " + ex.getSQLState());
			System.err.println("VendorError: " + ex.getErrorCode());			
		}
		
		return connection;
	}
}

DB-Test:
Code:
	private void testSQLite() {
		DatabaseConnectorSQLite connector = new DatabaseConnectorSQLite();
		Connection conn = connector.getConnection();
		if (conn != null) {
			try {
//				Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
				Statement st = conn.createStatement();
				ResultSet rs = st.executeQuery("select * from TEST");
				if (rs != null) {
//					rs.beforeFirst();
					while (rs.next()) {
						System.out.println("ID: " + rs.getString("ID"));
						System.out.println("Name: " + rs.getString("NAME"));
						System.out.println("Vorname: " + rs.getString("VORNAME"));
						System.out.println("Datum: " + rs.getTimestamp("DATUM"));
					}
					rs.close();
					st.close();
					conn.close();
				}
			}
			catch (SQLException ex) {
				ex.printStackTrace();				
			}
		}		
	}
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
hannibalstgt Fehler bei JCOMBOBOX GUI Anzeige AWT, Swing, JavaFX & SWT 4
L jComboBox Actionlistener wird beim erstmaligen Befüllen getriggert AWT, Swing, JavaFX & SWT 7
N Keylistener & JComboBox AWT, Swing, JavaFX & SWT 5
R JComboBox abfragen AWT, Swing, JavaFX & SWT 1
Esquid If Befehl bei JComboBox AWT, Swing, JavaFX & SWT 3
kodela Swing Element einer JComboBox ausgrauen AWT, Swing, JavaFX & SWT 3
D JComboBox Strings aus JTextFields zuordnen AWT, Swing, JavaFX & SWT 2
F JComboBox und Model AWT, Swing, JavaFX & SWT 10
I Editierbare JComboBox, die nur Ganzzahlen als Eingabewerte zulässt AWT, Swing, JavaFX & SWT 3
Drachenbauer Swing Wie ändere ich die Farbe der Konturen von jButton und jCombobox? AWT, Swing, JavaFX & SWT 18
Drachenbauer Swing Wie ändere ich den Hintergrund vom Anzeigefeld einer JComboBox, die nicht zum Hineinschreiben offen steht? AWT, Swing, JavaFX & SWT 32
Hatsi09 JCombobox default wert AWT, Swing, JavaFX & SWT 6
D Swing JCombobox Aktion löscht Grafik AWT, Swing, JavaFX & SWT 1
cezary Choice, JComboBox oder JList AWT, Swing, JavaFX & SWT 5
R Swing Java9 NullPointerException bei JComboBox AWT, Swing, JavaFX & SWT 13
ralfb1105 Swing JComboBox update der Daten AWT, Swing, JavaFX & SWT 8
D Swing JCombobox Weiße Leerzeilen AWT, Swing, JavaFX & SWT 10
G JComboBox: Arrow-Button permanent anzeigen AWT, Swing, JavaFX & SWT 4
D Swing Java JComboBox Example AWT, Swing, JavaFX & SWT 16
K Swing JComboBox mit ArrayList füllen AWT, Swing, JavaFX & SWT 4
D Swing "blaues" Element aus JComboBox erhalten AWT, Swing, JavaFX & SWT 6
F JComboBox und Einträge AWT, Swing, JavaFX & SWT 3
gamillton Swing JComboBox mit extra Inhalt + breiteres Popupmenü AWT, Swing, JavaFX & SWT 0
T JPanelsteuerung mit JComboBox AWT, Swing, JavaFX & SWT 5
N Swing Duplikate im Jcombobox vermeiden AWT, Swing, JavaFX & SWT 15
L Swing JComboBox kann nicht erstellt werden! AWT, Swing, JavaFX & SWT 2
C JComboBox Objecte übergeben und Eintrag auswählen AWT, Swing, JavaFX & SWT 3
M Swing JComboBox mit Laufwerksbuchstaben (Windows) AWT, Swing, JavaFX & SWT 6
D Swing Größe einer JComboBox im GridBagLayout aufgrund der maximalen Länge der enthaltenen Daten AWT, Swing, JavaFX & SWT 7
D Swing JComboBox (DefaultComboBoxModel) überschreibt Eintrag beim erstellen AWT, Swing, JavaFX & SWT 0
R Swing Durch JComboBox-Item eine TextArea aktualisieren AWT, Swing, JavaFX & SWT 2
3 JComboBox - Action - Auslesen & Umwandeln AWT, Swing, JavaFX & SWT 9
M Alle Schriftarten in JComboBox AWT, Swing, JavaFX & SWT 5
M Swing JComboBox eigenes Design AWT, Swing, JavaFX & SWT 3
Sin137 JComboBox mit Generics AWT, Swing, JavaFX & SWT 14
R JComboBox erweitern AWT, Swing, JavaFX & SWT 5
S Swing Eigene JComboBox Problem! AWT, Swing, JavaFX & SWT 1
V Editierbare JComboBox AWT, Swing, JavaFX & SWT 2
D Swing JCombobox in einem JTable vorbelegen AWT, Swing, JavaFX & SWT 4
F neuen Menüpunkt in jcombobox einfügen AWT, Swing, JavaFX & SWT 1
F Swing Jcombobox mit manueller Rückgabe AWT, Swing, JavaFX & SWT 4
S JComboBox mit mehreren Spalten? AWT, Swing, JavaFX & SWT 6
J JComboBox + ItemListener AWT, Swing, JavaFX & SWT 12
T Swing jComboBox auslesen mit Datenbankanbindung AWT, Swing, JavaFX & SWT 3
S JComboBox aus anderer Klasse füllen (Java-Swing) AWT, Swing, JavaFX & SWT 0
D Swing Erstes Item einer JCombobox in JTable anzeigen AWT, Swing, JavaFX & SWT 2
N JComboBox in JTable [Swing] -> totaler UI-Einsteiger AWT, Swing, JavaFX & SWT 3
H Swing jComboBox Ausgabe -1/null AWT, Swing, JavaFX & SWT 4
C Arrow Farbe bei JComboBox AWT, Swing, JavaFX & SWT 2
H Swing JList/JTable mit JButton, JTextField, Image, JComboBox und JLable AWT, Swing, JavaFX & SWT 2
O JTextfield und JComboBox Wert in SQL Database speichern AWT, Swing, JavaFX & SWT 4
S JComboBox UTF-8 Codierung zuweisen AWT, Swing, JavaFX & SWT 5
A JComboBox mit Array aktualisieren AWT, Swing, JavaFX & SWT 7
S JComboBox nach auswahl erneuern AWT, Swing, JavaFX & SWT 4
S Swing JComboBox mit Listener aktualisieren AWT, Swing, JavaFX & SWT 13
A Swing per JComboBox gewünschtes Attribut auswählen und Komponenten passen sich an AWT, Swing, JavaFX & SWT 7
I JComboBox gibt bei ausgewählten ITem NullPointer an... AWT, Swing, JavaFX & SWT 3
D Swing JCombobox - Tooltip Location ändern AWT, Swing, JavaFX & SWT 4
J JComboBox Dropdown Icon AWT, Swing, JavaFX & SWT 2
M Swing Mix JComboBox - JButton? AWT, Swing, JavaFX & SWT 6
A Swing bei Auswahl und klick eines JComboBox buttons die Klasse eines anderen Projekts aufrufen AWT, Swing, JavaFX & SWT 3
F Swing jComboBox auslesen AWT, Swing, JavaFX & SWT 6
O Swing TableCellRenderer und JComboBox AWT, Swing, JavaFX & SWT 4
F Swing JComboBox - Frage zur Größe AWT, Swing, JavaFX & SWT 11
C Swing JComboBox, ItemListener gibt 2x aus AWT, Swing, JavaFX & SWT 2
O JComboBox - autom. Selektieren AWT, Swing, JavaFX & SWT 6
J JComboBox - wie getSelectedItem() überschreiben? AWT, Swing, JavaFX & SWT 8
S Swing Jcombobox und array AWT, Swing, JavaFX & SWT 6
D Swing JComboBox aktualisieren AWT, Swing, JavaFX & SWT 4
E JComboBox AWT, Swing, JavaFX & SWT 8
N Swing JComboBox Frage AWT, Swing, JavaFX & SWT 5
S Swing Bild auf jPanel nach Änderung von JComboBox zeichnen AWT, Swing, JavaFX & SWT 4
H Swing Element aus JComboBox auswählen AWT, Swing, JavaFX & SWT 2
H JCombobox inhalt löschen AWT, Swing, JavaFX & SWT 17
N Swing JCombobox - PopupMenu-Inhalt mit KSKB AWT, Swing, JavaFX & SWT 2
O JComboBox mit ArrayList füllen AWT, Swing, JavaFX & SWT 3
S Swing JComboBox mit Datenbank füllen AWT, Swing, JavaFX & SWT 16
S Inhalt einer JComboBox aktualisieren AWT, Swing, JavaFX & SWT 6
F Swing JComboBox in JTable AutoComplete + Tab AWT, Swing, JavaFX & SWT 4
O JComboBox getSelectedItem AWT, Swing, JavaFX & SWT 4
M JComboBox Hintergrundfarbe des gewählten Items AWT, Swing, JavaFX & SWT 3
B Swing Problem: Horizontaler Scrollbalken in JComboBox hinzufügen AWT, Swing, JavaFX & SWT 4
M JCombobox mit ID und Text AWT, Swing, JavaFX & SWT 4
A JComboBox-Inhalt durch neues Array ersetzen AWT, Swing, JavaFX & SWT 2
C Swing Dynamische JComboBox (Filter) AWT, Swing, JavaFX & SWT 28
M JComboBox Item-Auswahl in JTable AWT, Swing, JavaFX & SWT 2
L Jbutton + jcombobox mit vorhandenen frame verknüfen AWT, Swing, JavaFX & SWT 8
D JCombobox mit Linien Styles AWT, Swing, JavaFX & SWT 4
H Unterschiedliche JComboBox je JTable Zeile AWT, Swing, JavaFX & SWT 4
B jCombobox addItem funktioniert nicht AWT, Swing, JavaFX & SWT 9
G JComboBox mit CellRenderer (Auswahl) AWT, Swing, JavaFX & SWT 11
G Swing JComboBox anpassen AWT, Swing, JavaFX & SWT 6
M Item in JComboBox umbenennen AWT, Swing, JavaFX & SWT 5
M Swing Busy Waiting Problem (JComboBox) AWT, Swing, JavaFX & SWT 11
C JComboBox Renderer Problem AWT, Swing, JavaFX & SWT 7
N Renderer Editoren und die JCombobox AWT, Swing, JavaFX & SWT 2
T Wie ist das "Lookup-Verhalten" von JList, JCombobox änderbar? AWT, Swing, JavaFX & SWT 4
N JCombobox und Actionlistener Aktion nur ausführen, wenn Useraktion ihn auslöst AWT, Swing, JavaFX & SWT 4
T Swing JComboBox mit grauem Vorgabetext AWT, Swing, JavaFX & SWT 3
M TableLayout: JComboBox zerstört alles... AWT, Swing, JavaFX & SWT 10

Ähnliche Java Themen

Neue Themen


Oben