Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following program compiles and runs, but it does nothing when I click on the

ID: 3534632 • Letter: T

Question

The following program compiles and runs, but it does nothing when I click on the ‘Look Up’ button.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

The program reads in an inventory file and loads it to an ArrayList, sorts the ArrayList in order to do a binary search on the ArrayList by product code. Once the frame opens you can enter a product code and when you hit ‘Look Up’ it should retrieve the information for the specified product code. But the look up button is not working. Any ideas?

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.*;
import java.util.Collections;
import java.awt.*;
import java.awt.event.*;

public class LookUpInventory extends JFrame {
public LookUpInventory() {
setTitle("INVEVTORY REPORT JAVA 205");
setLayout(new BorderLayout());
setSize(450,200);
Container contentPane = getContentPane();

topPanel= new JPanel(new FlowLayout());
lookUp = new JButton("Look Up");
productCode = new JTextField("Enter product code here");
topPanel.add(new JLabel("Product Code:"));
topPanel.add(productCode);
topPanel.add(lookUp);

bottomPanel = new JPanel(new GridLayout(0,2));
price = new JTextField(" ");
quantityOnHand = new JTextField(" ");
status = new JTextField(" ");
bottomPanel.add(new JLabel("Price: " ));
bottomPanel.add(price);
bottomPanel.add(new JLabel("Quantity On Hand : "));
bottomPanel.add(quantityOnHand);
bottomPanel.add(new JLabel("Status: "));
bottomPanel.add(status);

contentPane.add(topPanel, BorderLayout.NORTH);
contentPane.add(bottomPanel, BorderLayout.SOUTH);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} // windowClosing
}); // WindowListener

lookUp.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("listner active");
lookUpPressed();
}
});


} // end of constructor
static JPanel topPanel;
static JPanel bottomPanel;
static JButton lookUp;
static JTextField productCode;
static JTextField price;
static JTextField quantityOnHand;
static JTextField status;
static ArrayList<Inventory> inventoryList = new ArrayList<Inventory>();

//Main method
public static void main(String[] args) throws IOException {
File fileIn1 = new File("Inventory.txt");
if ( !fileIn1.exists()) {
System.out.println("Inventory.txt file does not exist - exiting program.");
System.exit(1);
}

//Create a Scanner Object
Scanner inventoryFile = new Scanner(fileIn1);

//Declare variables
int invenCount = 0;
boolean controlBreak = false;


//Process input files until inventory file is at end and load to array.
while ( inventoryFile.hasNext() ) {
inventoryList.add(new Inventory(inventoryFile.next()) );
invenCount++;
controlBreak = false;
}

//Close input files
inventoryFile.close();

//Sort the arraylist to ensure list is in order.
sortInventory(inventoryList);


//Start panel
final JFrame frame = new InventoryPanel();
frame.show();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void lookUpPressed() {
int index = inventorySearch(inventoryList, Integer.parseInt(productCode.getText()));
price.setText(String.valueOf(((Inventory)inventoryList.get(index)).getPrice()));
quantityOnHand.setText(String.valueOf(((Inventory)inventoryList.get(index)).getQuantityOnHand()));
System.out.println("mouselistner active");
}


public static void sortInventory(ArrayList<Inventory> list) {
boolean sorted = false;
int tempProductCode;
double tempPrice;
int tempQuantityOnHand;

while ( ! sorted ) {
sorted = true;
for ( int k = 0; k < list.size() - 1; k++ ) {
if ( ((Inventory)list.get(k)).getProductCode() > ((Inventory)list.get(k+1)).getProductCode() ) {
//Set temporary values.
tempProductCode = ((Inventory)list.get(k)).getProductCode();
tempPrice = ((Inventory)list.get(k)).getPrice();
tempQuantityOnHand = ((Inventory)list.get(k)).getQuantityOnHand();
//Swith the k and k+1 values
((Inventory)list.get(k)).setProductCode(((Inventory)list.get(k+1)).getProductCode());
((Inventory)list.get(k)).setPrice(((Inventory)list.get(k+1)).getPrice());
((Inventory)list.get(k)).setQuantityOnHand(((Inventory)list.get(k+1)).getQuantityOnHand());
((Inventory)list.get(k+1)).setProductCode(tempProductCode);
((Inventory)list.get(k+1)).setPrice(tempPrice);
((Inventory)list.get(k+1)).setQuantityOnHand(tempQuantityOnHand);
sorted = false;
}
}
}

}


public static int inventorySearch(ArrayList<Inventory> list, int key) {
int low = 0;
int high = list.size() - 1;
int middle;

while(high >= low) {
middle = (low + high) / 2;
if( ((Inventory)list.get(middle)).getProductCode() == key ) {
return middle;
}
if( ((Inventory)list.get(middle)).getProductCode() < key ) {
low = middle + 1;
}
if( ((Inventory)list.get(middle)).getProductCode() > key) {
high = middle - 1;
}
}
return -1;
}

}

Explanation / Answer

Please rate with 5 stars :)


Here is an example which shows you how to use Jbutton listeners.