Reputation: 1
I am writing code for a personal project that takes data from a CSV file, and passes it to an editable JCombobox that then displays names of airports.
Having done that - I want to write the quality of life feature where you start to type and the names slowly filter down until it matches what you are looking for (suggested text.)
When I run this code the first time everything goes smoothly, the suggested text system works in a rudimentary way, and then it resets with no issues when you delete what you have searched. However, every time I perform a second search, everything resets properly except for the ArrayList 'tempArray'.
I have tried moving it outside of the keyEvent, thinking I am not fulling understanding how it is being called. Yet, no matter where I put it, or however many debug statements I throw in, all of the code works properly except for that specific arrayList 'tempArray'. The bufferedChars string has a value, the airportNames arrayList holds all of it's same values.
I have scoured the web and documentation but I am clearly missing a fundamental part of what is going on. Any help is appreciated and all I want is to be pointed in the right direction, I am not asking for code.
Thank you.
import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class WindowEngine extends csvLoader implements ActionListener{
protected String originDataToSendToScraper, destinationDataToSendToScraper;
protected TextField destinationTextField;
protected JComboBox originComboBox;
String bufferedChars = "";
ArrayList<String> tempArray = new ArrayList<>();
MyDebug debug = new MyDebug();
public void drawWindow()
{
//Make Base Frame
JFrame frame = new JFrame("Pilot's Club");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(screenSize);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS));
//Make left Side panel
JPanel leftPanel = new JPanel();
//Origin Info
JLabel originLabel = new JLabel("Origin: ");
originLabel.setSize(50,25);
originLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
//Set array elements from csv and add them to the combobox
originComboBox = new JComboBox(airportNames.toArray());
originComboBox.setSize(50,25);
originComboBox.setAlignmentX(Component.LEFT_ALIGNMENT);
originComboBox.setToolTipText("Origin Airport: ");
originComboBox.setEditable(true);
originComboBox.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
//used to debug amount of times code has been run
int ran = 0;
//key events for the Jcombobox
@Override
public void keyTyped(KeyEvent event) {
if(event.getKeyChar() != KeyEvent.VK_ENTER)
{
if(originComboBox.getModel().getSize() < airportNames.size())
{
originComboBox.setModel(new DefaultComboBoxModel(airportNames.toArray()));
bufferedChars = "";
debug.log("RESET INITIATED");
}
bufferedChars += event.getKeyChar();
System.out.println("Added: " + bufferedChars);
}
if(event.getKeyChar() == KeyEvent.VK_ENTER && !bufferedChars.isEmpty())
{
ran +=1;
bufferedChars = bufferedChars.replace(" ", "");
System.out.println("Search Key: " + bufferedChars + " : search #: " + ran);
tempArray = new ArrayList<>();
for (String name : airportNames) {
if (name.contains(bufferedChars)) {
tempArray.add(name);
}
}
originComboBox.setModel(new DefaultComboBoxModel(tempArray.toArray()));
System.out.println(tempArray.toString());
}
}
});
//Destination Info
JLabel destinationLabel = new JLabel("Destination: ");
destinationLabel.setSize(50,25);
destinationLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
destinationTextField = new TextField(25);
destinationTextField.addActionListener(this);
//Add buttons and labels
leftPanel.add(originLabel);
leftPanel.add(originComboBox);
leftPanel.add(destinationLabel);
leftPanel.add(destinationTextField);
//Add panel to frame
frame.add(leftPanel);
//set visible last
frame.setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
originDataToSendToScraper = originComboBox.getSelectedItem().toString();
System.out.println("Sent origin data to scraper.. ");
destinationDataToSendToScraper = destinationTextField.getText();
System.out.println("Sent destination data to scraper.. ");
ScrapeAirportData scrapeAirportData = new ScrapeAirportData();
scrapeAirportData.getUserInfo(originDataToSendToScraper, destinationDataToSendToScraper);
}
}
The first search goes properly and the reset always works properly, but the suggested text feature fails to function after an initial search.
Upvotes: 0
Views: 47