import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Ch10Assignment extends JFrame implements ActionListener, ListSelectionListener {
JList pWrapList;
DefaultListModel listElements = new DefaultListModel();
JLabel pWrapLbl,pInfo, pWrapInputLbl, brandAInputLbl, brandBInputLbl, brandCInputLbl;
JTextArea brands;
JTextField pWrapInputTxt, brandAInputTxt, brandBInputTxt, brandCInputTxt;
JScrollPane scrollPane;
String[][] partNumberString = {
{"PR214", "MR43T", "RBL8", "14K22"},
{"PR223", "R43", "RJ6 ", "14K24"},
{"PR224", "R43N ", "RN4 ", "14K30"},
{"PR246", "R46N", "RN8 ", "14K32"}}; //etc..
String[][] tmp;
JButton addBtn, searchBtn, confirmBtn, cancelBtn, listAllBtn;
boolean show = false;
int counterInteger = 0;
int plainWrapInteger [] = new int [3];
String plainWrapString, brandAString, brandCString, brandXString;
public void showGui(){
getContentPane().setLayout(null);
pWrapLbl = new JLabel("Choose plain wrap: ");
pWrapLbl.setBounds(10,0,150,20);
pInfo = new JLabel("Parts:");
pInfo.setBounds(170,0,100,20);
pWrapList = new JList(listElements);
pWrapList.addListSelectionListener(this);
scrollPane = new JScrollPane(pWrapList);
scrollPane.setBounds(10,20,150,150);
brands = new JTextArea("Brand A Brand B Brand C\n" +
"none none none");
brands.setEditable(false);
brands.setBorder(BorderFactory.createLineBorder(Color.black, 1));
brands.setBounds(170,20,280,40);
addBtn = new JButton("Add new");
addBtn.addActionListener(this);
addBtn.setBounds(460,20,100,20);
searchBtn = new JButton("Search");
searchBtn.setEnabled(false);
searchBtn.addActionListener(this);
searchBtn.setBounds(460,40,100,20);
listAllBtn= new JButton("List all");
listAllBtn.setEnabled(false);
listAllBtn.addActionListener(this);
listAllBtn.setBounds(460,60,100,20);
pWrapInputLbl = new JLabel("Wrap:");
pWrapInputLbl.setBounds(170,70,100,20);
brandAInputLbl = new JLabel("Brand A:");
brandAInputLbl.setBounds(170,90,100,20);
brandBInputLbl = new JLabel("Brand B:");
brandBInputLbl.setBounds(170,110,100,20);
brandCInputLbl = new JLabel("Brand C:");
brandCInputLbl.setBounds(170,130,100,20);
pWrapInputTxt = new JTextField();
pWrapInputTxt.setBounds(250,70,200,20);
brandAInputTxt = new JTextField();
brandAInputTxt.setBounds(250,90,200,20);
brandBInputTxt = new JTextField();
brandBInputTxt.setBounds(250,110,200,20);
brandCInputTxt = new JTextField();
brandCInputTxt.setBounds(250,130,200,20);
confirmBtn = new JButton("Ok");
confirmBtn.addActionListener(this);
confirmBtn.setBounds(250,150,100,20);
cancelBtn = new JButton("Cancel");
cancelBtn.addActionListener(this);
cancelBtn.setBounds(350,150,100,20);
add(pWrapLbl); add(scrollPane);
add(brands); add(addBtn);
add(cancelBtn); add(pInfo);
// not needed, dropped.
add(searchBtn);
add(listAllBtn);
add(pWrapInputLbl); add(brandAInputLbl);
add(brandBInputLbl); add(brandCInputLbl);
add(pWrapInputTxt); add(brandAInputTxt);
add(brandBInputTxt); add(brandCInputTxt);
add(confirmBtn);
showForm(show);
setSize(600,200);
setTitle("By: HappyFace - Engineeringserver.com");
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
Ch10Assignment partNumber = new Ch10Assignment();
partNumber.setInitialState();
partNumber.showGui();
}
public void addNew(){
boolean duplicate = false;
tmp = new String[partNumberString.length+1][4];
for( int i = 0; i < partNumberString.length; i++ ){
tmp[i][0] = partNumberString[i][0];
for(int j = 1; j <partNumberString[i].length; j++){
tmp[i][j] = partNumberString[i][j];
}
}
//add the new part(s)
for(int i = 0; i<listElements.getSize(); i++ ){
if(listElements.get(i).equals(pWrapInputTxt.getText().toUpperCase())){
duplicate= true;
JOptionPane.showMessageDialog(null,"Duplicate found, try again!","Warning",0);
}
}
if( !pWrapInputTxt.getText().equals("") &&
!brandAInputTxt.getText().equals("") &&
!brandBInputTxt.getText().equals("") &&
!brandCInputTxt.getText().equals("") &&
!duplicate && pWrapInputTxt.getText().substring(0, 2).equals("PR") )
{
tmp[partNumberString.length][0] = pWrapInputTxt.getText();
tmp[partNumberString.length][1] = brandAInputTxt.getText();
tmp[partNumberString.length][2] = brandBInputTxt.getText();
tmp[partNumberString.length][3] = brandCInputTxt.getText();
cleanForm();
listElements.addElement(tmp[partNumberString.length][0]);
showForm(false);
//copy the tmp array back to the original array
partNumberString = new String[tmp.length][4];
for( int i = 0; i < tmp.length; i++ ){
partNumberString[i][0] = tmp[i][0];
for(int j = 1; j <tmp[i].length; j++){
partNumberString[i][j] = tmp[i][j];
}
}
}
else{
JOptionPane.showMessageDialog(null,"Item not added, try again!","Warning",0);
}
}
public void setInitialState(){
tmp = new String[partNumberString.length][4];
for( int i = 0; i < partNumberString.length; i++ ){
tmp[i][0] = partNumberString[i][0];
listElements.addElement("" + tmp[i][0]);
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == addBtn){
showForm(true);
}
if(e.getSource() == confirmBtn){
addNew();
}
if(e.getSource() == cancelBtn){
showForm(false);
cleanForm();
}
}
public void showForm(boolean show){
pWrapInputLbl.setVisible(show); brandAInputLbl.setVisible(show);
brandBInputLbl.setVisible(show); brandCInputLbl.setVisible(show);
pWrapInputTxt.setVisible(show); brandAInputTxt.setVisible(show);
brandBInputTxt.setVisible(show); brandCInputTxt.setVisible(show);
confirmBtn.setVisible(show); cancelBtn.setVisible(show);
}
public void cleanForm(){
pWrapInputTxt.setText(""); brandAInputTxt.setText("");
brandBInputTxt.setText("");
brandCInputTxt.setText("");
}
public void valueChanged(ListSelectionEvent f){
int selection = pWrapList.getSelectedIndex();
brands.setText("Brand A Brand B Brand C\n" +
partNumberString[selection][1] + " " +
partNumberString[selection][2] + " " +
partNumberString[selection][3]);
}
}