I have to somehow add a search function to my code. I have figured out how to add the search button and the text box next to it. The action event for the search button is currently the same as the event for the Next button. I need to change the action to that of a search function for the DVD array that will search for a title in the array and display the results. The JButton is "search", the editable text field is "Search" How do I tie the button action with the text field input with the result being displayed? Below is the code for my GUI.
class ButtonFrame extends JFrame
{
private JButton nextJButton; // button with icons
private JButton prevJButton; // button with icons
private JButton lastJButton; // button with icons
private JButton firstJButton; // button with icons
private JButton addJButton; // button with just text
private JButton deleteJButton; // button with just text
private JButton modifyJButton; // button with just text
private JButton saveJButton; // button with just text
private JLabel logoLabel;
private JButton searchJButton; //Feeble attempt at a search function
//private JTextField space1;
//private JTextField lblSearch; // text field with set size
private JTextField txtSearch; // text field constructed with text
private JTextField lblGenre; // text field with set size
private JTextField txtGenre; // text field constructed with text
private JTextField lblItemNum; // text field with set size
private JTextField txtItemNum; // text field constructed with text
private JTextField lblTitle; // text field with set size
private JTextField txtTitle; // text field constructed with text
private JTextField lblQuantity; // text field with set size
private JTextField txtQuantity; // text field constructed with text
private JTextField lblUnitPrice; // text field with set size
private JTextField txtUnitPrice; // text field constructed with text
private JTextField lblRestockFee; // text field with set size
private JTextField txtRestockFee; // text field constructed with text
private JTextField lblItemValue; // text field with set size
private JTextField txtItemValue; // text field constructed with text
private JTextField lblInventoryValue; // text field with set size
private JTextField txtInventoryValue; // text field constructed with text
// Just keeping an a class variable to keep count of where I am in the array
// keeping a class variable of the myPlayer array that I passed
Genre[] arrayDVDs;
private int currentArrayCounter;
private int arrayCount;
private String invTotal;
// ButtonFrame adds JButtons to JFrame
public ButtonFrame(Genre[] inventory, int totalArrayCount, String stringInventoryTotal)
{
super( "Movie Inventory" );
arrayDVDs = inventory;
invTotal=stringInventoryTotal;
// setting the local passed variable totalArrayCount
// to the class variable arrayCounter so I can see it in the setTextfields method
arrayCount = totalArrayCount;
currentArrayCounter = 0;
// Setting the current array position to 0
setLayout( new FlowLayout() ); // set frame layout
// Load the next and previous icons
Icon logo = new ImageIcon( getClass().getResource( "logo.jpg" ) );
Icon iconNext = new ImageIcon( getClass().getResource( "forward.gif" ) );
Icon iconPrev = new ImageIcon( getClass().getResource( "back.gif" ) );
Icon iconLast = new ImageIcon( getClass().getResource( "last.gif" ) );
Icon iconFirst = new ImageIcon( getClass().getResource( "first.gif" ) );
logoLabel = new JLabel("",logo,SwingConstants.LEFT);
add(logoLabel);
// construct Label Fields with default text and 15 columns
searchJButton = new JButton( "Search" );
//space1.setEditable( false ); // disable editing
add( searchJButton );
txtSearch = new JTextField( "", 15 );
txtSearch.setEditable( true ); // disable editing
add( txtSearch );
lblItemNum = new JTextField( "Item Number", 15 );
lblItemNum.setEditable( false ); // disable editing
add( lblItemNum );
txtItemNum = new JTextField("", 15 );
add( txtItemNum ); // add txtItemNum to JFrame
lblGenre = new JTextField( "Genre", 15 );
lblGenre.setEditable( false ); // disable editing
add( lblGenre );
txtGenre = new JTextField("", 15 );
add( txtGenre ); // add txtGenre to JFrame
lblTitle = new JTextField( "Title", 15 );
lblTitle.setEditable( false ); // disable editing
add( lblTitle );
txtTitle = new JTextField("", 15 );
add( txtTitle ); // add txtTitle to JFrame
lblQuantity = new JTextField( "Quantity", 15 );
lblQuantity.setEditable( false ); // disable editing
add( lblQuantity );
txtQuantity = new JTextField("", 15 );
add( txtQuantity ); // add txtQuantity to JFrame
lblUnitPrice = new JTextField( "Unit Price", 15 );
lblUnitPrice.setEditable( false ); // disable editing
add( lblUnitPrice );
txtUnitPrice = new JTextField("", 15 );
add( txtUnitPrice ); // add txtUnitPrice to JFrame
lblRestockFee = new JTextField( "Restocking Fee", 15 );
lblRestockFee.setEditable( false ); // disable editing
add( lblRestockFee );
txtRestockFee = new JTextField("", 15 );
add( txtRestockFee ); // add txtRestockFee to JFrame
lblItemValue = new JTextField( "Total Item Value", 15 );
lblItemValue.setEditable( false ); // disable editing
add( lblItemValue );
txtItemValue = new JTextField("", 15 );
add( txtItemValue ); // add txtItemValue to JFrame
lblInventoryValue = new JTextField( "Total Inventory Value", 15 );
lblInventoryValue.setEditable( false ); // disable editing
add( lblInventoryValue );
txtInventoryValue = new JTextField(invTotal, 15 );
add( txtInventoryValue ); // add txtInventoryValue to JFrame
// construct textfield with default text
// Create the buttons
nextJButton = new JButton( "Next", iconNext ); // button with Next
prevJButton = new JButton( "Prev" , iconPrev ); // button with Next
lastJButton = new JButton( "Last", iconLast ); // button with Next
firstJButton = new JButton( "First" , iconFirst ); // button with Next
addJButton = new JButton( "Add" ); //button with add function
deleteJButton = new JButton( "Delete" ); //button with delete
modifyJButton = new JButton( "Modify" ); //button with modify
saveJButton = new JButton( "Save" ); //button with save
add(firstJButton ); // add iconJButton to JFrame
add(prevJButton); // add iconJButton to JFrame
add(nextJButton ); // add plainJButton to JFrame
add(lastJButton); // add iconJButton to JFrame
add(addJButton); // add textJButton to JFrame
add(deleteJButton); // add textJButton to JFrame
add(modifyJButton); // add textJButton to JFrame
add(saveJButton); // add textJButton to JFrame
// create new ButtonHandler for button event handling
ButtonHandler handler = new ButtonHandler();
searchJButton.addActionListener( handler );
firstJButton.addActionListener( handler );
prevJButton.addActionListener( handler );
nextJButton.addActionListener( handler );
lastJButton.addActionListener( handler );
addJButton.addActionListener( handler );
deleteJButton.addActionListener( handler );
modifyJButton.addActionListener( handler );
saveJButton.addActionListener( handler );
// Now I am going to call SetTextFields to set the text fields
setTextFields();
} // end ButtonFrame constructor
// inner class for button event handling
private class ButtonHandler implements ActionListener
{
// handle button event
public void actionPerformed( ActionEvent event )
{
//System.out.println(event.getActionCommand());
// See which button was pressed
if (event.getActionCommand()== "Search"){
currentArrayCounter++;
}
else if (event.getActionCommand()== "Next"){
currentArrayCounter++;
}
else if (event.getActionCommand()== "Prev"){
currentArrayCounter--;
}
else if (event.getActionCommand()== "Last"){
currentArrayCounter = arrayCount-1;
}
else if (event.getActionCommand()== "First"){
currentArrayCounter = 0;
}
else if (event.getActionCommand()== "Add"){ //Adds 1 to quantity on hand
arrayDVDs[currentArrayCounter].addOneToQuantity();
}
else if (event.getActionCommand()== "Delete"){ //Deletes 1 from quantity on hand
arrayDVDs[currentArrayCounter].removeOneFromQuantity();
}
else if (event.getActionCommand()== "Save") //Writes data to C:/data/inventory.dat
{
FileOutputStream out;
PrintStream p;
try
{
String strDirectory = "c:/data";
new File(strDirectory).mkdir();
out = new FileOutputStream ("C:/data/inventory.dat");
p = new PrintStream(out);
for (int i = 0; i<= 5 - 1; i++)
{
p.println("DVD Genre: " + arrayDVDs[i].getgenre()+"\n");
p.println("DVD Title: " + arrayDVDs[i].gettitle()+"\n");
p.println("Item Number: " + arrayDVDs[i].getitemnumber()+"\n");
p.println("Stock Level: " + arrayDVDs[i].getquantity()+"\n");
p.println("DVD Price: $" + arrayDVDs[i].getunitprice()+"\n");
p.println("");
}
p.close();
}
catch (Exception e)
{
System.out.println("error");
}
}
setTextFields();
} // end method actionPerformed
} // end private inner class ButtonHandler
private void setTextFields ()
{
// Make sure you havent gone past the end of the array
if (currentArrayCounter == arrayCount)
{
currentArrayCounter = 0;
}
// Make sure you havent gone past the first if so, set it to the last
if (currentArrayCounter < 0)
{
currentArrayCounter = arrayCount-1;
}
NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US); //Provides Locale appropriate currency format
txtGenre.setText(arrayDVDs[currentArrayCounter].getgenre());
txtItemNum.setText(arrayDVDs[currentArrayCounter].getitemnumber());
txtTitle.setText(arrayDVDs[currentArrayCounter].gettitle());
txtQuantity.setText(Double.toString(arrayDVDs[currentArrayCounter].getquantity()));
txtUnitPrice.setText(n.format(arrayDVDs[currentArrayCounter].getunitprice()));
String stringRestockFee = n.format(arrayDVDs[currentArrayCounter].CalculateRestockFee());
txtRestockFee.setText(stringRestockFee);
txtItemValue.setText(n.format(arrayDVDs[currentArrayCounter].calculateDVDValue()));
}
} // end class ButtonFrame