Servus Gemeinde,
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 :
Jetzt der DocumentListener mit der Textbehandlung:
und ..
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 :
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.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
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(){
messageTimer = new Timer(_Sys.getTimerInfo(), new ActionListener() {
public void actionPerformed(final ActionEvent e) {
}
});
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(ActionEvent e) {
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(""))){
..
clearFrameAndClose();
return;
}
// Wenn kein Einsatzort eingetragen wurde, erfolgt ein Abbruch
if(this.tf_locationOne.getText().equals("") &&
this.tf_locationTwo.getText().equals("")){
_Error.getErrorMessage(242,
..
return;
}
else{
// Wenn der 1.Einsatzort verändert wurde
if(!checkLocation(this.tf_locationOne.getText())){
_Error.getErrorMessage(244,
new Exception().getStackTrace()[0].getClassName()
..
return;
}
// Wenn der 2.Einsatzort verändert wurde und
// nicht leer ist
if(!checkLocation(this.tf_locationTwo.getText()) &&
! this.tf_locationTwo.getText().equals("")){
_Error.getErrorMessage(245,
..
return;
}
_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());
clearFrameAndClose();
}
}
if(but == this.bt_deleteFirstLoc){
this.tf_locationOne.setText("");
this.tf_locationNumberOne.setText("");
this.tf_locationOne.grabFocus();
_Error.getErrorMessage(241,
new Exception().getStackTrace()[0].getClassName()
+ "/"
+ new Exception().getStackTrace()[0]
.getMethodName(), "");
setInfoText(_Error.getErrorClassification(), _Error
.getErrorText1(), _Error.getErrorText2(), _Error
.getErrorText3(), _Error.getErrorText4(), _Error
.getExtraText());
}
if(but == this.bt_deleteSecondLoc){
this.tf_locationTwo.setText("");
this.tf_locationNumberTwo.setText("");
this.tf_locationTwo.grabFocus();
_Error.getErrorMessage(240,
new Exception().getStackTrace()[0].getClassName()
+ "/"
+ new Exception().getStackTrace()[0]
.getMethodName(), "");
setInfoText(_Error.getErrorClassification(), _Error
.getErrorText1(), _Error.getErrorText2(), _Error
.getErrorText3(), _Error.getErrorText4(), _Error
.getExtraText());
}
}
/** 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(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;
}
/** 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);
}
public void setInfoText(final int errorClassification, final String error1,
final String error2, final String error3, final String error4, final String extraText)
{
..
}
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");
final Calendar cal = Calendar.getInstance();
lb_infoLabel.setBackground(_Sys.getBackground());
lb_infoLabel.setForeground(Color.black);
lb_infoLabel.setText("");
}
}
@Override
public void focusGained(FocusEvent e) {
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(FocusEvent e) {
// TODO Auto-generated method stub
}
@Override
public synchronized void valueChanged(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()){
String location = (String) this.jl_listOfLocationData.getSelectedValue();
System.out.println(location);
LocationDocListener.locationOne = location;
//LocationDocListener.flag = true;
this.tf_locationOne.setText(location);
// this.jl_listOfLocationData.removeListSelectionListener(this);
//Selection, enable the fire button.
}
}
}
if(!isLocationFlag()){
System.out.println("Frame / locationFlag == false");
}
}
/**
* @return the locationFlag
*/
public static boolean isLocationFlag() {
return locationFlag;
}
/**
* @param locationFlag the locationFlag to set
*/
public static void setLocationFlag(boolean locationFlag) {
LocationDataFrame.locationFlag = locationFlag;
}
public void setListSelectionListenerForJList(){
this.jl_listOfLocationData.addListSelectionListener(this);
}
}
Jetzt der DocumentListener mit der Textbehandlung:
Java:
package _Pol649;
import javax.swing.JFrame;
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++){
System.out.println("Daten einlesen-----" + i + "LocationFlag :" +
LocationDataFrame.isLocationFlag() + "--- Flag: " + flag);
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;
System.out.println("hier");
// .. 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());
// das Textfeld wird komplett markiert
//this.frame.tf_locationOne.selectAll();
// 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, "Die letzte verbliebene Straße wurde übernommen");
this.frame.tf_locationNumberOne.grabFocus();
}
if(!LocationDataFrame.isLocationFlag()){
// .. und es sich um das 2.Textfeld (isLocationFlag == false) handelt..
this.frame.tf_locationTwo.setText(LocationDataHelper.modelClone.
firstElement().toString().trim());
// das Textfeld wird komplett markiert
this.frame.tf_locationTwo.selectAll();
// 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, "Die letzte verbliebene Straße wurde übernommen");
this.frame.tf_locationNumberTwo.grabFocus();
}
//this.frame.setListSelectionListenerForJList();
checkModelSize();
}
if(LocationDataHelper.modelClone.size()==0){
JOptionPane.showMessageDialog(frame, "Keine Übereinstimmung.\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.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++){
System.out.println("CheckModelSize 3 --"+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(getLocationTwo()) &&
LocationDataHelper.modelClone.size() <=1){
flag = true;
LocationDataHelper.modelClone.clear();
for(int y = 0; y < LocationDataHelper.model.size(); y++){
LocationDataHelper.modelClone.addElement(LocationDataHelper.model.get(y));
}
this.frame.jl_listOfLocationData.setModel(LocationDataHelper.model);
setLocationOne("");
}
}
}
/**
* @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;
}
}
und ..
Java:
package _Pol649;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
public class LocationDataHelper {
private String street ;
static DefaultListModel model = new DefaultListModel();
static DefaultListModel modelClone = new DefaultListModel();
public LocationDataHelper(String street){
this.street = street;
model.addElement(street);
//this.zip= zip;
}
public static void getModelData(){
for(int i = 0; i < model.size(); i++){
}
}
public static void sortModelData(String text){
}
}
Zuletzt bearbeitet von einem Moderator: