Thomas Lorenz
Grünschnabel
Servus Gemeinde,
erstmal ein Hallo an alle in diesem Forum, ich bin der Neue
.
Also bei meinem Frame lade ich beim Aufruf ca. 11.000 Straßen in das Model meiner JList.
Anschließend kann ich über ein Textfeld anhand der Eingabe die Einträge der JList hervorragend filtern.
Klappt super.
Auch wenn ich die Eingabe im Textfeld ändere, passt sich die JList immer an.
Wenn ich aber aus der JList einen Eintrag per Maus auswähle, dann macht er auch was er soll. Er setzt die entsprechende Straße in das Textfeld und gut ist.
Nun schreibe ich per Tastatur in das Textfeld eine neue Straße und schon läuft es nicht mehr richtig.
Er passt das Model zwar an, aber das dauert sehr, sehr lange.
Das tritt immer dann auf, nachdem ich per Maus einen Eintrag ausgewählt habe und danach per Hand was neues eintrage.
Wenn ich z.B. immer mit der Maus auswähle ist auch alles in Ordnung.
Hier meine Klassen :
Der Frame
Der DocListener
und noch eine Klasse für die Objekte
Habt Ihr eine Erklärung für dieses Verhalten ?
erstmal ein Hallo an alle in diesem Forum, ich bin der Neue

Also bei meinem Frame lade ich beim Aufruf ca. 11.000 Straßen in das Model meiner JList.
Anschließend kann ich über ein Textfeld anhand der Eingabe die Einträge der JList hervorragend filtern.
Klappt super.
Auch wenn ich die Eingabe im Textfeld ändere, passt sich die JList immer an.
Wenn ich aber aus der JList einen Eintrag per Maus auswähle, dann macht er auch was er soll. Er setzt die entsprechende Straße in das Textfeld und gut ist.
Nun schreibe ich per Tastatur in das Textfeld eine neue Straße und schon läuft es nicht mehr richtig.
Er passt das Model zwar an, aber das dauert sehr, sehr lange.
Das tritt immer dann auf, nachdem ich per Maus einen Eintrag ausgewählt habe und danach per Hand was neues eintrage.
Wenn ich z.B. immer mit der Maus auswähle ist auch alles in Ordnung.
Hier meine Klassen :
Der Frame
Java:
package _Pol649;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class LocationDataFrame extends JFrame implements ActionListener, FocusListener, ListSelectionListener{
private static boolean locationFlag = true;
JFrame Location = new JFrame("Einsatzort");
JLabel lb_infoLabel = new JLabel("<html><b>Änderungen werden nach dem erneuten Programmstart aktiv</b></html>");
JLabel lb_location = new JLabel("Einsatzort");
JLabel lb_locationNumber = new JLabel("Hausnr.");
JPanel panel = new JPanel();
JPanel pan_north = new JPanel();
JPanel pan_northLeft = new JPanel();
JPanel pan_northRight = new JPanel();
JPanel pan_center = new JPanel();
JPanel pan_south = new JPanel();
JPanel pan_top = new JPanel();
JPanel pan_bottom = new JPanel();
JList jl_listOfLocationData = new JList(LocationDataHelper.model);
JScrollPane scr_listOfLocationData = new JScrollPane(jl_listOfLocationData);
JTextField tf_locationOne = new JTextField();
JTextField tf_locationTwo = new JTextField();
JTextField tf_locationNumberOne = new JTextField();
JTextField tf_locationNumberTwo = new JTextField();
GridLayout lay_panel = new GridLayout(3,1);
GridLayout lay_panNorth = new GridLayout(1,2);
GridLayout lay_panNorthLeft = new GridLayout(3,1);
GridLayout lay_panNorthRight = new GridLayout(3,2);
GridLayout lay_center = new GridLayout(1,1);
GridLayout lay_panSouth = new GridLayout(2,1);
GridLayout lay_panTop = new GridLayout(1,2);
GridLayout lay_panBottom = new GridLayout(1,1);
JButton bt_deleteFirstLoc = new JButton("löschen");
JButton bt_deleteSecondLoc = new JButton("löschen");
JButton bt_save = new JButton("übernehmen");
JButton bt_close = new JButton ("zur Hauptansicht");
SystemData _Sys;
ErrorHelper _Error;
PropertyData _Data;
MainFrame _Main;
LocationDataHelper _loc;
JFrame main;
Message msg = new Message();
private Timer messageTimer;
private boolean timerFlag = true;
public LocationDataFrame(){
this._Sys = SystemData.getInstance();
this._Error = ErrorHelper.getInstance();
this._Data = PropertyData.getInstance();
this._Main = (MainFrame)CollectionController.getObject("MainFrame");
}
public Container addComponentsToPane(){
/*
* Dieser Timer hat eine Laufzeit, die von _Sys.getTimerInfo() bestimmt
* wird. Nach dieser Zeit füllt er das InfoFenster (unten) mit einer
* Standardmeldung mit einem zusätzl. Zeitstempel Durch
* 'setRepeats(false) wird der Timer nach jedem Aufruf nur einmal
* aufgerufen.
*/
messageTimer = new Timer(_Sys.getTimerInfo(), new ActionListener() {
public void actionPerformed(final ActionEvent e) {
clearInfoWindow();
}
});
messageTimer.setRepeats(false);
final Container pane = getContentPane();
panel.setBorder(new EmptyBorder (0,0,0,0));
panel.setLayout(lay_panel);
panel.setBackground(_Sys.getBackground());
pan_north.setLayout(lay_panNorth);
pan_north.setBorder(new EmptyBorder(0,0,0,0));
pan_northLeft.setLayout(lay_panNorthLeft);
pan_northLeft.setBorder(new EmptyBorder(60,20,60,20));
pan_northLeft.add(lb_location);
pan_northLeft.add(tf_locationOne);
tf_locationOne.addFocusListener(this);
tf_locationOne.getDocument().addDocumentListener(new LocationDocListener(tf_locationOne,this));
tf_locationTwo.addFocusListener(this);
tf_locationTwo.getDocument().addDocumentListener(new LocationDocListener(tf_locationTwo, this));
pan_northLeft.add(tf_locationTwo);
pan_north.add(pan_northLeft);
pan_northRight.setLayout(lay_panNorthRight);
pan_northRight.setBorder(new EmptyBorder(60,25,60,70));
pan_northRight.add(lb_locationNumber);
pan_northRight.add(new JLabel());
pan_northRight.add(tf_locationNumberOne);
//tf_locationNumberOne.getDocument().addDocumentListener(new LocationDocListener(tf_locationNumberOne, this));
tf_locationNumberOne.addFocusListener(this);
pan_northRight.add(this.bt_deleteFirstLoc);
bt_deleteFirstLoc.addActionListener(this);
pan_northRight.add(tf_locationNumberTwo);
tf_locationNumberTwo.addFocusListener(this);
pan_northRight.add(this.bt_deleteSecondLoc);
bt_deleteSecondLoc.addActionListener(this);
pan_north.add(pan_northRight);
pan_center.setLayout(lay_center);
pan_center.setBorder(new EmptyBorder(0,20,0,20));
jl_listOfLocationData.addListSelectionListener(this);
pan_center.add(scr_listOfLocationData);
pan_south.setLayout(lay_panSouth);
pan_south.setBorder(new EmptyBorder(20,0,0,0));
pan_top.setLayout(lay_panTop);
pan_top.setBorder(new EmptyBorder(40,20,30,20));
pan_top.add(this.bt_close);
bt_close.addActionListener(this);
bt_save.addActionListener(this);
pan_top.add(this.bt_save);
pan_bottom.setLayout(lay_panBottom);
pan_bottom.setBorder(new EmptyBorder(0,20,10,20));
Border loweredbevel;
loweredbevel = BorderFactory.createLoweredBevelBorder();
lb_infoLabel.setSize(new Dimension(300,300));
lb_infoLabel.setBorder(loweredbevel );
lb_infoLabel.setOpaque(true);
lb_infoLabel.setBackground(_Sys.getBackground());
lb_infoLabel.setForeground(Color.black);
msg.setText("<html>" + "" + "<br>" + "" + "<br>" +
"" + "<br> " + "" + "<br>" + "" + "<br>" + "</B/html>");
lb_infoLabel.setToolTipText(_Data.getToolTipTextsForButtons().elementAt(29));
lb_infoLabel.setText(msg.getText());
pan_bottom.add(this.lb_infoLabel);
pan_south.add(pan_top);
pan_south.add(pan_bottom);
panel.add(pan_north);
panel.add(pan_center);
panel.add(pan_south);
pane.add(panel);
return pane;
}
public void createAndShowGUI(final JFrame Main){
this.main = Main;
Location.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
Location.setContentPane(addComponentsToPane());
Location.setBackground(_Sys.getBackground());
Location.setResizable(false);
Location.setTitle("Einsatzort");
Location.setSize(600, 650);
Location.setLocationRelativeTo(null); // Bildschirmmitte
Location.setVisible(true);
}
@Override
public void actionPerformed(final ActionEvent e) {
final JButton but = (JButton)e.getSource();
if(but == this.bt_close){
Location.setVisible(false);
main.setEnabled(true);
main.repaint();
}
if(but == this.bt_save){
// Wenn kein 1.Einsatzort, sondern ein 2.Einsatzort eingetragen wurde
if(this.tf_locationOne.getText().equals("") &&
(!this.tf_locationTwo.getText().equals(""))){
_Main.tf_firstLocation.setText(this.tf_locationTwo.getText());
_Main.tf_firstLocationNumber.setText(this.tf_locationNumberTwo.getText());
_Main.tf_secondLocation.setText("");
_Main.tf_secondLocationNumber.setText("");
clearFrameAndClose();
return;
}
// Wenn kein Einsatzort eingetragen wurde, erfolgt ein Abbruch
if(this.tf_locationOne.getText().equals("") &&
this.tf_locationTwo.getText().equals("")){
return;
}
else{
// Wenn der 1.Einsatzort verändert wurde
if(!checkLocation(this.tf_locationOne.getText())){
}
// Wenn der 2.Einsatzort verändert wurde und
// nicht leer ist
if(!checkLocation(this.tf_locationTwo.getText()) &&
! this.tf_locationTwo.getText().equals("")){
}
// Wenn ein Einsatzort eingetragen wurde, dann werden die Felder auf dem MainFrame entsprechend
// angepasst. Ansonsten werden die alten Werte behalten
if(checkLocationFields()){
_Main.tf_firstLocation.setText(this.tf_locationOne.getText());
_Main.tf_firstLocationNumber.setText(this.tf_locationNumberOne.getText());
_Main.tf_secondLocation.setText(this.tf_locationTwo.getText());
_Main.tf_secondLocationNumber.setText(this.tf_locationNumberTwo.getText());
_Main.ch_acrossFrom.setSelected(false);
_Main.ch_streetNumber.setSelected(false);
}
clearFrameAndClose();
}
}
if(but == this.bt_deleteFirstLoc){
this.tf_locationOne.setText("");
this.tf_locationNumberOne.setText("");
this.tf_locationOne.grabFocus();
}
if(but == this.bt_deleteSecondLoc){
this.tf_locationTwo.setText("");
this.tf_locationNumberTwo.setText("");
this.tf_locationTwo.grabFocus();
}
}
/** Gibt 'true' zurück, wenn der Inhalt des Textfeldes identisch
* mit einer Straße aus dem Straßenverzeichnis ist.
* Somit wird gewährleistet, dass nur schreibweisen übernommen werden,
* die auch im Verzeichnis vorhanden sind.
* @param location
* @return
*/
public boolean checkLocation(final String location){
for(int i = 0; i< LocationDataHelper.model.size(); i++){
if(location.trim().equals(LocationDataHelper.model.get(i).toString().trim())){
return true;
}
}
return false;
}
public boolean checkLocationFields(){
if(!this.tf_locationOne.getText().equals("") ||
!this.tf_locationTwo.getText().equals("")){
return true;
}
return false;
}
/** Löscht alle Felder zur Örtlichkeit
*
*/
public void clearFrameAndClose(){
this.tf_locationOne.setText("");
this.tf_locationNumberOne.setText("");
this.tf_locationNumberTwo.setText("");
this.tf_locationTwo.setText("");
Location.setVisible(false);
_Main.Main.setEnabled(true);
MissionData.isActualMissionChanged();
}
public void setInfoText(final int errorClassification, final String error1,
final String error2, final String error3, final String error4, final String extraText)
{
/*
* Durch das Eintreten eines PropertyChangeEvent (immer wenn die
* _Error.getErrorMessage Methode aufgerufen wird) wird ein Object der
* Message Klasse (Kapselung) aufgerufen. Der Timer 'messageTimer' wird
* neu gestartet.
*/
addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(final PropertyChangeEvent evt) {
final String propertyName = evt.getPropertyName();
if ("message".equals(propertyName)){
final Message msg = (Message) (evt.getNewValue());
lb_infoLabel.setText(msg.getText());
messageTimer.restart();
}
}
});
final SimpleDateFormat fmt = new SimpleDateFormat();
fmt.applyPattern(" HH:mm:ss");
final Calendar cal = Calendar.getInstance();
// Hier beginnen die unterschiedlichen Formen der Fehlerdarstellung,
// anhand ihrer 'errorClassification'
final Message msg = new Message();
if (errorClassification == 5){ // z.B. Prüfung von Einsatzzeiten (dauerhafte Anzeige)
// Label wird formatiert
lb_infoLabel.setBackground(_Sys.getBackgroundInfoLightBlue());
lb_infoLabel.setForeground(Color.black);
msg.setText("<html>" + error1 + "<br>" + error2 + "<br>" + error3
+ "<br> " + error4 + " " + extraText + "<br>" + "<B>"
+ "letzte Meldung um : " + fmt.format(cal.getTime())
+ " Uhr </B> (Durch Doppelklick auf dieses Feld wird diese Meldung gelöscht.)</html>");
// wenn timerFlag == true, dann wird der Timer erneut aufgerufen
timerFlag = false;
}
if (errorClassification == 4){ // grüner Hintergrund
// Label wird formatiert
lb_infoLabel.setBackground(_Sys.getBackgroundInfoGreen());
lb_infoLabel.setForeground(Color.black);
msg.setText("<html>" + error1 + "<br>" + error2 + "<br>" + error3
+ "<br> " + error4 + " " + extraText + "<br>" + "<B>"
+ "letzte Meldung um : " + fmt.format(cal.getTime())
+ " Uhr </B/html>");
// wenn timerFlag == true, dann wird der Timer erneut aufgerufen
timerFlag = true;
}
if (errorClassification == 3){ // gelber Hintergrund
lb_infoLabel.setForeground(Color.black);
lb_infoLabel.setBackground(_Sys.getBackgroundInfoYellow());
msg.setText("<html>" + error1 + "<br>" + error2 + "<br>" + error3
+ "<br> " + error4 + " " + extraText + "<br>" + "<B>"
+ "letzte Meldung um : " + fmt.format(cal.getTime())
+ " Uhr </B/html>");
timerFlag = true;
}
if (errorClassification == 2){ // roter Hintergrund
lb_infoLabel.setForeground(Color.white);
lb_infoLabel.setBackground(_Sys.getBackgroundInfoRed());
msg.setText("<html>" + error1 + "<br>" + error2 + "<br>" + error3
+ "<br> " + error4 + " " + extraText + "<br>" + "<B>"
+ "letzte Meldung um : " + fmt.format(cal.getTime())
+ " Uhr </B/html>");
timerFlag = true;
}
// Das passiert, wenn der Anwender per Doppelklick in das Label klickt.
if (errorClassification == 0){ // Textfeld wird auf NULL gesetzt
// (Standardwert)
msg.setText("<html>" + error1 + "<br>" + error2 + "<br>" + error3
+ "<br> " + error4 + " " + extraText + "<br>" + "<B>"
+ "letzte Meldung wurde durch den Anwender um : "
+ fmt.format(cal.getTime()) + " Uhr gelöscht.</B/html>");
// nur wenn das timerFlag == false ist, dann wird der Timer nicht
// erneut gestartet
timerFlag = false;
}
firePropertyChange("message", null, msg);
// Wenn der Anwender per Doppelklick in das Meldungsfenster klickt,
// wird die dadurch entstehende Meldung nicht aufgezeichnet.
if (timerFlag){
/*
* Es folgt das Speichern des aufgetretenen Fehlers. , Achtung: die
* Zusammensetzung erfolgt in umgekehrter Reihenfolge. Dadurch kann
* der zuletzt ausgelöste Fehler an 1. Stelle dargestellt werden.
*/
_Error
.addToErrorList("_________________________________________________________________________________________"
+ System.getProperty("line.separator"));
_Error.addToErrorList("Uhrzeit : " + fmt.format(cal.getTime()));
_Error.addToErrorList(extraText + System.getProperty("line.separator"));
_Error.addToErrorList(error4 + System.getProperty("line.separator"));
_Error.addToErrorList(error3 + System.getProperty("line.separator"));
_Error.addToErrorList(error2 + System.getProperty("line.separator"));
_Error.addToErrorList(error1 + System.getProperty("line.separator"));
}
}
public void clearInfoWindow(){
if(timerFlag){
// .. und nun wird nach der eingestellten Zeit (_Sys.getTimerInfo())
//die letzte Meldung mit einer Standardanzeige überschrieben
final SimpleDateFormat fmt = new SimpleDateFormat();
fmt.applyPattern(" HH:mm:ss");
lb_infoLabel.setBackground(_Sys.getBackground());
lb_infoLabel.setForeground(Color.black);
lb_infoLabel.setText("");
}
}
@Override
public void focusGained(final FocusEvent e) {
final JTextField text = (JTextField)e.getSource();
if(text == tf_locationOne ||
text == tf_locationNumberOne){
locationFlag = true;
tf_locationOne.setBackground(_Sys.getBackgroundInfoYellow());
tf_locationNumberOne.setBackground(_Sys.getBackgroundInfoYellow());
tf_locationTwo.setBackground(Color.white);
tf_locationNumberTwo.setBackground(Color.white);
}
if(text == tf_locationTwo ||
text == tf_locationNumberTwo) { locationFlag = false;
tf_locationOne.setBackground(Color.white);
tf_locationNumberOne.setBackground(Color.white);
tf_locationTwo.setBackground(_Sys.getBackgroundInfoYellow());
tf_locationNumberTwo.setBackground(_Sys.getBackgroundInfoYellow());}
}
@Override
public void focusLost(final FocusEvent e) {
// TODO Auto-generated method stub
}
@Override
public synchronized void valueChanged(final ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
if (this.jl_listOfLocationData.getSelectedIndex() == -1) {
//No selection, disable fire button.
} else {
System.out.println(e.getFirstIndex()+"--firstInd-------" + LocationDocListener.flag);
if(isLocationFlag()){
final String location = (String) this.jl_listOfLocationData.getSelectedValue();
LocationDocListener.locationOne = location;
//LocationDocListener.flag = true;
this.tf_locationOne.setText(location);
}
if(!isLocationFlag()){
final String location = (String) this.jl_listOfLocationData.getSelectedValue();
LocationDocListener.locationTwo = location;
this.tf_locationTwo.setText(location);
}
}
}
}
/**
* @return the locationFlag
*/
public static boolean isLocationFlag() {
return locationFlag;
}
/**
* @param locationFlag the locationFlag to set
*/
public static void setLocationFlag(final boolean locationFlag) {
LocationDataFrame.locationFlag = locationFlag;
}
public void setListSelectionListenerForJList(){
this.jl_listOfLocationData.addListSelectionListener(this);
}
public void removeListSelectionListenerForJList(){
this.jl_listOfLocationData.removeListSelectionListener(this);
}
}
Der DocListener
Java:
package _Pol649;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
public class LocationDocListener implements DocumentListener ,Cloneable {
JTextField text ;
LocationDataFrame frame;
public static boolean flag = true;
private boolean foundFlag = false;
public static String locationOne = "";
public static String locationTwo = "";
public LocationDocListener( JTextField text, LocationDataFrame location){
this.text = text;
this.frame = location;
}
@Override
public synchronized void changedUpdate(DocumentEvent e) {
final Document doc = (Document)e.getDocument();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try{
if(flag){
searchForString(doc.getText(0, text.getText().length()));
}
}
catch (BadLocationException e1){
// TODO Auto-generated catch block
e1.printStackTrace();
}
}});
}
@Override
public synchronized void insertUpdate(DocumentEvent e) {
final Document doc = (Document)e.getDocument();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try{
if(flag){
searchForString(doc.getText(0, text.getText().length()));
}
}
catch (BadLocationException e1){
// TODO Auto-generated catch block
e1.printStackTrace();
}
}});
}
@Override
public synchronized void removeUpdate(DocumentEvent e) {
final Document doc = (Document)e.getDocument();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try{
checkModelSize();
if(!flag){
}
if(flag){
searchForString(doc.getText(0, text.getText().length()));
}
}
catch (BadLocationException e1){
// TODO Auto-generated catch block
e1.printStackTrace();
}
}});
}
/** Überprüft, ob der übergebene Text des Textfeldes im Modelvektor vorkommt.
* Hierbei werden auch Teilstrings zugelassen.
* Sollte der übergebene Text vorkommen, wird das Model entsprechend
* geändert.
* @param text
*/
public void searchForString(String text){
LocationDataHelper.modelClone.clear();
this.frame.jl_listOfLocationData.setModel(LocationDataHelper.model);
// Kommt der String im Model vor?
for(int i = 0; i < LocationDataHelper.model.size(); i++){
if(LocationDataHelper.model.get(i).toString().startsWith(text) ||
LocationDataHelper.model.get(i).toString().equals(text) ||
LocationDataHelper.model.get(i).toString().toLowerCase().contains(text.toLowerCase())){
// Dem geklonten Model werden die gefundenen Einträge übergeben
LocationDataHelper.modelClone.addElement(LocationDataHelper.model.get(i).toString().trim());
// Die JList bekommt das geklonte Model übergeben
this.frame.jl_listOfLocationData.setModel(LocationDataHelper.modelClone);
}
}
// Wenn nur noch ein Eintrag in der JList steht..
if(LocationDataHelper.modelClone.size()==1){
flag = false;
// .. und es sich um das 1.Textfeld (isLocationFlag == true) handelt..
if(LocationDataFrame.isLocationFlag()){
// .. bekommt das Textfeld den übriggebliebenen Eintrag
this.frame.tf_locationOne.setText(LocationDataHelper.modelClone.firstElement().toString().trim());
// eine Hilfsvariable für das 1.Textfeld bekommt den einzigen Eintrag aus dem geklonten Model
setLocationOne(LocationDataHelper.modelClone.firstElement().toString().trim());
// Um weitere Eingaben zu unterbinden / unterbrechen, wird eine Meldung ausgegeben,
// die bestätigt werden muss
JOptionPane.showMessageDialog(frame, "Ihre Eingabe ließ nur noch eine mögliche Örtlichkeit übrig." +
" \nDiese Straße wurde übernommen" );
this.frame.tf_locationNumberOne.grabFocus();
this.frame.jl_listOfLocationData.setModel(LocationDataHelper.model);
flag = true;
}
if(!LocationDataFrame.isLocationFlag()){
// .. und es sich um das 2.Textfeld (isLocationFlag == false) handelt..
this.frame.tf_locationTwo.setText(LocationDataHelper.modelClone.firstElement().toString().trim());
// eine Hilfsvariable für das 2.Textfeld bekommt den einzigen Eintrag aus dem geklonten Model
setLocationTwo(LocationDataHelper.modelClone.firstElement().toString().trim());
// Um weitere Eingaben zu unterbinden / unterbrechen, wird eine Meldung ausgegeben,
// die bestätigt werden muss
JOptionPane.showMessageDialog(frame, "Ihre Eingabe ließ nur noch eine mögliche Örtlichkeit übrig. \nDiese Straße wurde übernommen");
this.frame.tf_locationNumberTwo.grabFocus();
this.frame.jl_listOfLocationData.setModel(LocationDataHelper.model);
flag = true;
}
checkModelSize();
}
if(LocationDataHelper.modelClone.size()==0){
JOptionPane.showMessageDialog(frame, "Es wurde zu Ihrer Eingabe keine Übereinstimmung gefunden.\n\nIhre Eingabe : " + text);
if(LocationDataFrame.isLocationFlag()){
this.frame.tf_locationOne.setText(this.frame.tf_locationOne.getText().substring(0, this.frame.tf_locationOne.getText().length()-1));
this.frame.tf_locationOne.grabFocus();
this.frame.tf_locationOne.select(0, this.frame.tf_locationOne.getText().length());
this.frame.tf_locationOne.setCaretPosition(this.frame.tf_locationOne.getText().length());
//this.frame.tf_locationOne.setCaretPosition(this.frame.tf_locationOne.getText().length());
//this.frame.tf_locationOne.sel
}
if(!LocationDataFrame.isLocationFlag()){
this.frame.tf_locationTwo.setText(this.frame.tf_locationTwo.getText().substring(0, this.frame.tf_locationTwo.getText().length()-1));
this.frame.tf_locationTwo.grabFocus();
this.frame.tf_locationTwo.select(0, this.frame.tf_locationTwo.getText().length());
this.frame.tf_locationTwo.setCaretPosition(this.frame.tf_locationTwo.getText().length());
this.frame.tf_locationTwo.setCaretPosition(this.frame.tf_locationTwo.getText().length());
}
}
else{return;}
}
/** Überprüft die aktuelle Größe des geklonten Models
*
*/
public synchronized void checkModelSize(){
// Wenn es sich um das 1.Textfeld handelt..
if(LocationDataFrame.isLocationFlag()){
// Wenn sich der Inhalt des Textfeldes von der Hilfsvariablen unterscheidet
// (z.B. wenn der Anwender einen Buchstaben gelöscht hat)
// und nur noch ein Eintrag im geklonten Model enthalten ist
if(!this.frame.tf_locationOne.getText().equals(getLocationOne()) &&
LocationDataHelper.modelClone.size() <=1){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
flag = true;
// Das Model wird geleert
LocationDataHelper.modelClone.clear();
// und mit den gesamten Straßenverzeichnis (model) gefüllt.
for(int y = 0; y < LocationDataHelper.model.size(); y++){
LocationDataHelper.modelClone.addElement(LocationDataHelper.model.get(y));
}
}});
// Die JList bekommt alle Straßen
this.frame.jl_listOfLocationData.setModel(LocationDataHelper.model);
// Die Hilfsvariable wird gelöscht
setLocationOne("");
}
}
// Wie oben , nur für das 2.Textfeld
if(!LocationDataFrame.isLocationFlag()){
if(!this.frame.tf_locationTwo.getText().equals(getLocationOne()) &&
LocationDataHelper.modelClone.size() <=1){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
flag = true;
// Das Model wird geleert
LocationDataHelper.modelClone.clear();
// und mit den gesamten Straßenverzeichnis (model) gefüllt.
for(int y = 0; y < LocationDataHelper.model.size(); y++){
LocationDataHelper.modelClone.addElement(LocationDataHelper.model.get(y));
}
}});
// Die JList bekommt alle Straßen
this.frame.jl_listOfLocationData.setModel(LocationDataHelper.model);
// Die Hilfsvariable wird gelöscht
setLocationTwo("");
}
}
}
/**
* @return the locationOne
*/
public String getLocationOne() {
return locationOne;
}
/**
* @return the locationTwo
*/
public String getLocationTwo() {
return locationTwo;
}
/**
* @param locationOne the locationOne to set
*/
public void setLocationOne(String locationOne) {
this.locationOne = locationOne;
}
/**
* @param locationTwo the locationTwo to set
*/
public void setLocationTwo(String locationTwo) {
this.locationTwo = locationTwo;
}
/**
* @param foundFlag the foundFlag to set
*/
public void setFoundFlag(boolean foundFlag) {
this.foundFlag = foundFlag;
}
/**
* @return the foundFlag
*/
public boolean isFoundFlag() {
return foundFlag;
}
}
und noch eine Klasse für die Objekte
Java:
package _Pol649;
import javax.swing.DefaultListModel;
public class LocationDataHelper {
private String street ;
static DefaultListModel model = new DefaultListModel();
static DefaultListModel modelClone = new DefaultListModel();
public LocationDataHelper(String street){
this.setStreet(street);
model.addElement(street);
}
public static void getModelData(){
for(int i = 0; i < model.size(); i++){
}
}
/**
* @param street the street to set
*/
public void setStreet(String street) {
this.street = street;
}
/**
* @return the street
*/
public String getStreet() {
return street;
}
}
Habt Ihr eine Erklärung für dieses Verhalten ?