package components;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class InventoryDepartment extends JPanel
implements ListSelectionListener {
private JList list;
private DefaultListModel listModel;
private static final String addString = "Add Item";
private static final String deleteString = "Delete Item";
private JButton deleteButton;
private JTextField itemName;
public InventoryDepartment() {
super(new BorderLayout());
listModel = new DefaultListModel();
listModel.addElement("Item 1");
listModel.addElement("Item 2");
listModel.addElement("Item 3");
listModel.addElement("Item 4");
listModel.addElement("Item 5");
//Create the list and put it in a scroll pane.
list = new JList(listModel);
list.setBackground(new Color(240, 250, 240));
list.setForeground(new Color(46, 139, 87));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.addListSelectionListener(this);
list.setVisibleRowCount(5);
JScrollPane listScrollPane = new JScrollPane(list);
JButton addButton = new JButton(addString);
addButton.setBackground(new Color(240, 250, 240));
addButton.setForeground(new Color(46, 139, 87));
AddListener addListener = new AddListener(addButton);
addButton.setActionCommand(addString);
addButton.addActionListener(addListener);
addButton.setEnabled(false);
deleteButton = new JButton(deleteString);
deleteButton.setBackground(new Color(240, 250, 240));
deleteButton.setForeground(new Color(46, 139, 87));
deleteButton.setActionCommand(deleteString);
deleteButton.addActionListener(new DeleteListener());
itemName = new JTextField(10);
itemName.setBackground(new Color(240, 250, 240));
itemName.setForeground(new Color(46, 139, 87));
itemName.addActionListener(addListener);
itemName.getDocument().addDocumentListener(addListener);
String name = listModel.getElementAt(
list.getSelectedIndex()).toString();
//Create a panel that uses BoxLayout.
JPanel buttonPane = new JPanel();
buttonPane.setBackground(new Color(46, 139, 87));
buttonPane.setLayout(new BoxLayout(buttonPane,
BoxLayout.LINE_AXIS));
buttonPane.add(deleteButton);
buttonPane.add(Box.createHorizontalStrut(5));
buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
buttonPane.add(Box.createHorizontalStrut(5));
buttonPane.add(itemName);
buttonPane.add(addButton);
buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
add(listScrollPane, BorderLayout.CENTER);
add(buttonPane, BorderLayout.PAGE_END);
}
class DeleteListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int index = list.getSelectedIndex();
listModel.remove(index);
int size = listModel.getSize();
if (size == 0) { //Nobody's left, disable firing.
deleteButton.setEnabled(false);
} else { //Select an index.
if (index == listModel.getSize()) {
//removed item in last position
index--;
}
list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
}
}
//This listener is shared by the text field and the add button.
class AddListener implements ActionListener, DocumentListener {
private boolean alreadyEnabled = false;
private JButton button;
public AddListener(JButton button) {
this.button = button;
}
//Required by ActionListener.
public void actionPerformed(ActionEvent e) {
String name = itemName.getText();
//User didn't type in a unique name...
if (name.equals("") || alreadyInList(name)) {
Toolkit.getDefaultToolkit().beep();
itemName.requestFocusInWindow();
itemName.selectAll();
return;
}
int index = list.getSelectedIndex(); //get selected index
if (index == -1) { //no selection, so insert at beginning
index = 0;
} else { //add after the selected item
index++;
}
listModel.insertElementAt(itemName.getText(), index);
//Reset the text field.
itemName.requestFocusInWindow();
itemName.setText("");
//Select the new item and make it visible.
list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
//This method tests for string equality.
protected boolean alreadyInList(String name) {
return listModel.contains(name);
}
//Required by DocumentListener.
public void insertUpdate(DocumentEvent e) {
enableButton();
}
//Required by DocumentListener.
public void removeUpdate(DocumentEvent e) {
handleEmptyTextField(e);
}
//Required by DocumentListener.
public void changedUpdate(DocumentEvent e) {
if (!handleEmptyTextField(e)) {
enableButton();
}
}
private void enableButton() {
if (!alreadyEnabled) {
button.setEnabled(true);
}
}
private boolean handleEmptyTextField(DocumentEvent e) {
if (e.getDocument().getLength() <= 0) {
button.setEnabled(false);
alreadyEnabled = false;
return true;
}
return false;
}
}
//This method is required by ListSelectionListener.
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
if (list.getSelectedIndex() == -1) {
//No selection, disable delete button.
deleteButton.setEnabled(false);
} else {
//Selection, enable the delete button.
deleteButton.setEnabled(true);
}
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("ListDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new InventoryDepartment();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
I went back and changed all my buttons to start with lowercase letters.. is that what you were talking about?? It does make sense that, that would make it easier to read and understand. Above is my code for my InventoryDepartment and I am still getting the message that it can't access this file... not sure why not, I did put them all in the same folder...
This post has been edited by SnoBunny85: 07 May 2012 - 07:14 AM

New Topic/Question
Reply





MultiQuote



|