I have 3 things left I want to do to this app, I saved the hardest for last ... now I am wondering why.
First is a search button. I want to be able to Search on my cd name field.
I have been reading and playing with this since yesterday, I thought I had it, started coding it in, and the logic fails me.
This is what I wrote that I think should work.
cdNameField.addCaretListener(new CaretListener()
{
public void careUpdate(CaretEvent e)
{
JTextField f = (JTextField)e.getSource();
final String s = f.getText();
SwingUtilites.invokeLate(new Runnable()
{
public void run()
{
try
{
for(int i-0; i < listModel.getSize(); i++
{
String item = (String)listModel.elementAt(i);
String sub = item.substring(0, s.lenght());
if(sub.equalsIgnoreCase(s))
{
Inventorylist.setSelectedIndex(i);
Inventorylist.scrollRectToVisible(Inventorylist.getCellBounds(i,i));
break;
}
}
}
}
});
}
});
Now I have never done this, and most of that is just from what I have read.
When I started putting it in to my code I noticed two things.
1st - This listener seems to be a CaretListener .... how will that work with my ActionListener and my SEARCH button?
2nd - I already have a runnable for my panel. Does the code from this runnable next inside my current one, or can I have more than one in the same app?
Here is my code - any help, as always will be appreciated.
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.lang.*;
import javax.swing.JFrame;
public class Inventory2 extends JFrame
{
private JLabel cdNameLabel; // name label
private JLabel artistLabel; // item number label
private JLabel nstockLabel; // units in stock label
private JLabel priceLabel; // price each label
private JLabel itemLabel; // item number label
private JTextField cdNameField; // name display
private JTextField artistField; // artist display
private JFormattedTextField nstockField; // units in stock display
private JFormattedTextField priceField; // price each display
private JTextField itemField; // item number display
private NumberFormat nstockFormat; // format field and parse numbers
private NumberFormat priceFormat; // format field and parse numbers
private JButton btnAdd; // first button
private JButton btnPrev; // previous button
private JButton btnNext; // next button
private JButton btnDel; // last button
private JButton btnFirst; // first button
private JButton btnLast; // last button
private JButton btnModify; // modify button
private JButton btnSave; // save button
private JButton btnSearch; // search button
private JPanel buttonJPanel; // JPanle to hold buttons
private JPanel fieldJPanel; // JPanel to hold labels and displays
private JPanel fontJPanel; // JPanel to display logo
private int currCD;
private double total = 0; // variable for total inventory
private JList Inventorylist; // JList to take place of old array
private DefaultListModel listModel;
private JScrollPane jScrollPanel;
private float Invtotal = .00f;
DecimalFormat formatter = new DecimalFormat("0.00");
public Inventory2() // create class and method to perform GUI build
{
initComponents();
}
private void initComponents()
{
// create label names
cdNameLabel = new JLabel("CD Name:");
artistLabel = new JLabel("Artist:");
nstockLabel = new JLabel("In Stock:");
priceLabel = new JLabel("Each Item Cost:$");
itemLabel = new JLabel("Item Number:");
// initial fields
cdNameField = new JTextField(25);
cdNameField.setEditable(true);
artistField = new JTextField(15);
artistField.setEditable(true);
nstockField = new JFormattedTextField(nstockFormat);
nstockField.setEditable(true);
nstockField.setColumns(5);
priceField = new JFormattedTextField(priceFormat);
priceField.setEditable(true);
priceField.setColumns(5);
itemField = new JTextField(4);
itemField.setEditable(true);
// JList
jScrollPanel = new JScrollPane();
Inventorylist = new JList();
currCD = 0;
// buttons
btnAdd = new JButton();
btnNext = new JButton();
btnPrev = new JButton();
btnDel = new JButton();
btnLast = new JButton();
btnFirst = new JButton();
btnModify = new JButton();
btnSave = new JButton();
btnSearch = new JButton();
getContentPane().setLayout(new FlowLayout());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// add shapesJPanel to frame
ShapesJPanel logo = new ShapesJPanel();
logo.setPreferredSize(new Dimension(200,200));
getContentPane().add( logo );
// place textFields and labels
//artist
artistLabel.setText("Artist");
getContentPane().add(artistLabel);
artistField.setMinimumSize(new Dimension(70,20));
artistField.setPreferredSize(new Dimension(70,20));
getContentPane().add(artistField);
// cd name
cdNameLabel.setText("CD Name");
getContentPane().add(cdNameLabel);
cdNameField.setMinimumSize(new Dimension(70,20));
cdNameField.setPreferredSize(new Dimension(70,20));
getContentPane().add(cdNameField);
// copies in stock
nstockLabel.setText("Copies In Stock");
getContentPane().add(nstockLabel);
nstockField.setMinimumSize(new Dimension(5,20));
nstockField.setPreferredSize(new Dimension(5,20));
getContentPane().add(nstockField);
//price of cd
priceLabel.setText("Price: $");
getContentPane().add(priceLabel);
priceField.setMinimumSize(new Dimension(20,20));
priceField.setPreferredSize(new Dimension(20,20));
getContentPane().add(priceField);
//item number of cd
itemLabel.setText("Item Number");
getContentPane().add(itemLabel);
itemField.setMinimumSize(new Dimension(5,20));
itemField.setPreferredSize(new Dimension(5,20));
getContentPane().add(itemField);
// add listeners
btnAdd.setText("Add");
btnAdd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnAdd);
// PREVIOUS
btnPrev.setText("Previous");
btnPrev.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnPrevActionPerformed(evt);
}
});
getContentPane().add(btnPrev);
// NEXT
btnNext.setText("Next");
btnNext.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnNextActionPerformed(evt);
}
});
getContentPane().add(btnNext);
// SEARCH
btnSearch.setText("Search");
btnSearch.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnSearch);
// FIRST
btnFirst.setText("First");
btnFirst.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnFirstActionPerformed(evt);
}
});
getContentPane().add(btnFirst);
// LAST
btnLast.setText("Last");
btnLast.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnLastActionPerformed(evt);
}
});
getContentPane().add(btnLast);
// MODIFY
btnModify.setText("Modify");
btnModify.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnModifyActionPerformed(evt);
}
});
getContentPane().add(btnModify);
// SAVE
btnSave.setText("Save");
btnSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnSave);
// DELETE
btnDel.setText("Delete");
btnDel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
btnDeleteActionPerformed(evt);
}
});
getContentPane().add(btnDel);
// new Jlist model
listModel = new DefaultListModel();
Inventorylist.setModel(listModel);
jScrollPanel.setViewportView(Inventorylist);
getContentPane().add(jScrollPanel);
pack();
}// close
private void btnAddActionPerformed(ActionEvent evt)
{
// Create cd to add
CdwArtist newCD = new CdwArtist();
newCD.setArtist(artistField.getText());
newCD.setName(cdNameField.getText());
newCD.setItemno(Integer.parseInt(itemField.getText()));
newCD.setNstock(Integer.parseInt(nstockField.getText()));
newCD.setPrice(Float.parseFloat(priceField.getText()));
// Add cd to list
listModel.addElement(newCD);
currCD = listModel.size()-1; // sets currCD to added index
// Clear the text fields after add
artistField.setText(null);
cdNameField.setText(null);
itemField.setText(null);
nstockField.setText(null);
priceField.setText(null);
}// end ADD
private void btnPrevActionPerformed(ActionEvent evt)
{
// Grab Previous cd
if (--currCD<0) currCD = listModel.size()-1;
CdwArtist newCD = (CdwArtist) listModel.get( currCD );
artistField.setText(newCD.getArtist());
cdNameField.setText(newCD.getName());
itemField.setText(String.valueOf(newCD.getItemno()));
nstockField.setText(String.valueOf(newCD.getNstock()));
priceField.setText(formatter.format(newCD.getPrice()));
}// end PREV
private void btnNextActionPerformed(ActionEvent evt)
{
// Grab Next cd
if (++currCD >= listModel.size()) currCD= 0;
CdwArtist newCD = (CdwArtist) listModel.get( currCD );
artistField.setText(newCD.getArtist());
cdNameField.setText(newCD.getName());
itemField.setText(String.valueOf(newCD.getItemno()));
nstockField.setText(String.valueOf(newCD.getNstock()));
priceField.setText(formatter.format(newCD.getPrice()));
}// end NEXT
private void btnFirstActionPerformed(ActionEvent evt)
{
// Grab First cd
CdwArtist newCD = (CdwArtist) listModel.get(0);
currCD = 0;
artistField.setText(newCD.getArtist());
cdNameField.setText(newCD.getName());
itemField.setText(String.valueOf(newCD.getItemno()));
nstockField.setText(String.valueOf(newCD.getNstock()));
priceField.setText(formatter.format(newCD.getPrice()));
}// end FIRST
private void btnLastActionPerformed(ActionEvent evt)
{
// Grab Last cd
CdwArtist newCD = (CdwArtist) listModel.lastElement();
currCD = listModel.size()-1;
artistField.setText(newCD.getArtist());
cdNameField.setText(newCD.getName());
itemField.setText(String.valueOf(newCD.getItemno()));
nstockField.setText(String.valueOf(newCD.getNstock()));
priceField.setText(formatter.format(newCD.getPrice()));
}// end LAST
private void btnDeleteActionPerformed(ActionEvent evt)
{
// Delete cd
listModel.remove(currCD);
// Clear the text fields after delete
artistField.setText(null);
cdNameField.setText(null);
itemField.setText(null);
nstockField.setText(null);
priceField.setText(null);
}// end DELETE
private void btnModifyActionPerformed(ActionEvent evt)
{
// Modify cd
listModel.remove(currCD);
// Create cd to add
CdwArtist newCD = new CdwArtist();
newCD.setArtist(artistField.getText());
newCD.setName(cdNameField.getText());
newCD.setItemno(Integer.parseInt(itemField.getText()));
newCD.setNstock(Integer.parseInt(nstockField.getText()));
newCD.setPrice(Float.parseFloat(priceField.getText()));
// Add cd to list
listModel.addElement(newCD);
currCD = listModel.size()-1; // sets currCD to added index
// Clear the text fields after add
artistField.setText(null);
cdNameField.setText(null);
itemField.setText(null);
nstockField.setText(null);
priceField.setText(null);
}// end Modify
// Search the Name Field
cdNameField.addCaretListener(new CaretListener()
{
public void caretUpdate(CaretEvent e)
{
JTextField f = (JtextField)e.getSource();
final String s = f.getText();
// run it
public static void main(String args[])
{
JFrame frame = new JFrame( "CD Inventory Logo" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Inventory2().setVisible(true);
}
});
}
} // close class