Hi everyone! (it's been awhile XD)
I've gone and done my homework. And here I am with a program that works...pretty well I think.
There appears to be a problem with the saving/loading. Originally it worked without a problem, but when I attempted to make it more graphically friendly (when it displays in the list), the file saves, however I can't load it in panel 2.
You can get a 'Load Model Set', by just copy pasting the data below. Put it into a text file. (notepad)
48-209-351 1.3 300 5
60-344-351 1.45 250 12
72-554-351 1.32 200 9
84-754-351 1.35 350 3
96-1042-351 1.4 280 7
As I said before, originally it works, but now it doesn't. I'm nearing a deadline and I have to do uber documentation, can anyone help? I hope this is enough effort lol. Peace guys :D
PLEASE DO NOT COPY!!!!!!!
Here is Class TreeClassics
// TreeClassics Program
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class TreeClassics extends JFrame
{
//include aspects of GUI
private JTextField quantityField, thicknessField, widthField,
plysField, modelField, heightField, tipField, groupField;
// modelField, heightField, tipField, groupField have not been
// implemented yet; for panel 3
//
private JEditorPane scrollListContent;
private JTextArea enteredContent;
private JList enteredItemsList, loadedList, buggerList;
// Labels for field input identifier
private JLabel quantityLabel, thicknessLabel,
widthLabel, colorLabel, plysLabel,
productionLabel, informLabel, specifyLabel, helpLabel,
help2Label, help3Label, help4Label, help5Label,
model1Label, quantity1Label, width1Label, plys1Label,
color1Label, thickness1Label;
// The three tab panels
private JPanel panel1, panel2, panel3;
// Combobox selection system
private JComboBox modelComboBox, colorComboBox;
private JScrollPane orderScrollPane, enteredScrollPane, orderScrollPane2;
private JButton
loadModelsButton, enterButton, removeTempItemsButton,
saveListButton, deleteModelsButton, modelInfoButton,
modelButton, thicknessButton, widthButton, colorButton,
loadListButton, clearListButton, loadListButton2, selectButton;
private Vector<Model> modelList;
private String[] colors = {"Green", "Teal", "Dark Blue", "White", "Gray"};
private OrderList prodList;
private TreeClassics()
{
// GUI Setup for program
super("Tree Classics Production Line");
this.prodList = new OrderList();
this.modelList = new Vector<Model>();
// Creating JTabbed Pane
JTabbedPane tabbedPane = new JTabbedPane();
// -------------------------PANEL 1 INTERFACE---------------------------
// Set up Panel 1
JLabel label1 = new JLabel();
panel1 = new JPanel( null );
panel1.add( label1 );
tabbedPane.addTab( " Production Order ",
null, panel1, "Production orders are created here");
// Model number selection label
productionLabel = new JLabel( "Model Number");
productionLabel.setBounds( 20, 20, 100, 20 );
panel1.add( productionLabel );
// Add Combo Box into Panel 1: PRODUCTION ORDER
modelComboBox = new JComboBox();
modelComboBox.setBounds(20, 40, 150, 20);
panel1.add(modelComboBox);
// Add Help Button into Panel1
modelInfoButton = new JButton( "Model Information" );
modelInfoButton.setBounds( 20, 70, 150, 20);
panel1.add(modelInfoButton);
// Titles for Selection and treeData types
// Quantity
quantityLabel = new JLabel( "Quantity");
quantityLabel.setBounds( 250, 20, 100, 20 );
panel1.add(quantityLabel);
// Thickness
thicknessLabel = new JLabel( "Thickness");
thicknessLabel.setBounds( 250, 75, 100, 20 );
panel1.add(thicknessLabel);
// Width
widthLabel = new JLabel( "Width");
widthLabel.setBounds( 370, 20, 100, 20 );
panel1.add(widthLabel);
// Color
colorLabel = new JLabel( "Color");
colorLabel.setBounds( 490, 20, 100, 20 );
panel1.add(colorLabel);
// Plys
plysLabel = new JLabel( "Plys");
plysLabel.setBounds( 370, 75, 100, 20 );
panel1.add( plysLabel );
// Color 1 Label
color1Label = new JLabel( "Color");
color1Label.setBounds( 320, 130, 100, 20 );
panel1.add(color1Label);
// Models 1 Label
model1Label = new JLabel( "Model Number");
model1Label.setBounds( 20, 130, 100, 20 );
panel1.add(model1Label);
// quantity 1 Label
quantity1Label = new JLabel( "Quantity");
quantity1Label.setBounds( 120, 130, 100, 20 );
panel1.add(quantity1Label);
// thickness 1 Label
thickness1Label = new JLabel( "Thickness");
thickness1Label.setBounds( 185, 130, 100, 20 );
panel1.add(thickness1Label);
// width 1 Label
width1Label = new JLabel( "Width");
width1Label.setBounds( 260, 130, 100, 20 );
panel1.add(width1Label);
// plys 1 Label
plys1Label = new JLabel( "Plys");
plys1Label.setBounds( 370, 130, 100, 20 );
panel1.add(plys1Label);
// Quantity field input
quantityField = new JTextField();
quantityField = new JTextField("0", 15 );
quantityField.setBounds( 250, 40 , 100, 20);
panel1.add( quantityField );
// Width field input
widthField = new JTextField();
widthField = new JTextField("0", 15 );
widthField.setBounds( 370, 40 , 100, 20);
panel1.add( widthField );
// Thickness field input
thicknessField = new JTextField();
thicknessField = new JTextField("0", 15 );
thicknessField.setBounds( 250, 95 , 100, 20);
panel1.add(thicknessField);
// Plys field input
plysField = new JTextField();
plysField = new JTextField("0", 15 );
plysField.setBounds( 370, 95 , 100, 20);
panel1.add(plysField);
// Color scroll pane input
colorComboBox = new JComboBox( colors );
colorComboBox.setMaximumRowCount( 5 );
colorComboBox.setBounds( 490, 40, 100, 20 );
colorComboBox.setSelectedIndex( 0 );
panel1.add(colorComboBox);
// Panel 1 scroll pane for entered information
//enteredContent = new JTextArea();
//enteredContent.setEditable(false);
enteredItemsList = new JList(prodList);
enteredScrollPane = new JScrollPane(enteredItemsList);
enteredScrollPane.setBounds(20,150,400,150);
enteredScrollPane.createVerticalScrollBar();
panel1.add(enteredScrollPane);
// Add Enter Button into Panel1
enterButton = new JButton("Enter");
enterButton.setBounds( 450, 150 , 100, 20);
panel1.add(enterButton);
removeTempItemsButton = new JButton("Remove Selected");
removeTempItemsButton.setBounds( 450, 180 , 150, 20);
panel1.add(removeTempItemsButton);
// Add Save Button into Panel1
saveListButton = new JButton( "Save List" );
saveListButton.setBounds( 450, 210 , 150, 20);
panel1.add(saveListButton);
// Add load model button into Panel1
loadModelsButton = new JButton("Load Model Set");
loadModelsButton.setBounds( 450, 240, 150, 20);
panel1.add( loadModelsButton);
// Add delete model button into Panel1
deleteModelsButton = new JButton("Delete Model Set");
deleteModelsButton.setBounds( 450, 270, 150, 20);
panel1.add(deleteModelsButton);
// ------------------PANEL 2 INTERFACE----------------------------------
// Set up Panel 2
JLabel label2 = new JLabel( "Panel Two", SwingConstants.CENTER );
panel2 = new JPanel( null );
panel2.add( label2 );
tabbedPane.addTab( " List Orders ",
null, panel2, "Orders are listed and organized here");
// Add Infomer Label into Panel 2
informLabel = new JLabel( "Information of this list");
informLabel.setBounds( 40, 20, 300, 20 );
panel2.add( informLabel );
// Add scroll pane label into Panel 2
informLabel = new JLabel( "Model Quantity Thickness Width Color Plys");
informLabel.setBounds( 20, 60, 400, 20 );
panel2.add( informLabel );
// Panel 2 scroll pane
scrollListContent = new JEditorPane();
scrollListContent.setEditable(false);
orderScrollPane = new JScrollPane(scrollListContent);
orderScrollPane.setBounds(20,80,400,220);
orderScrollPane.createVerticalScrollBar();
panel2.add(orderScrollPane);
// Add Sort by Model Button into Panel2
modelButton = new JButton( "Sort by Model #" );
modelButton.setBounds( 450, 80 , 150, 20);
panel2.add( modelButton );
// Add Sort by Thickness Button into Panel2
thicknessButton = new JButton( "Sort by Thickness" );
thicknessButton.setBounds( 450, 170 , 150, 20);
panel2.add( thicknessButton );
// Add Sort by Width Button into Panel2
widthButton = new JButton( "Sort by Width" );
widthButton.setBounds( 450, 110 , 150, 20);
panel2.add( widthButton );
// Add Sort by Color Button into Panel2
colorButton = new JButton( "Sort by Color" );
colorButton.setBounds( 450, 140 , 150, 20);
panel2.add( colorButton );
// Add Load Button into Panel2
loadListButton = new JButton( "Load List" );
loadListButton.setBounds( 450, 200 , 150, 20);
panel2.add(loadListButton);
// Add Clear Button into Panel2
clearListButton = new JButton( "Clear List" );
clearListButton.setBounds( 450, 230 , 150, 20);
panel2.add(clearListButton);
// ------------------PANEL 3 INTERFACE----------------------------------
// Set up Panel 3: For Printing
JLabel label3 = new JLabel( "Panel Three", SwingConstants.CENTER );
panel3 = new JPanel( null );
panel3.add( label3 );
tabbedPane.addTab( " HELP PANEL ",
null, panel3, "Select the set of data of models to use");
// Add Specify Label into Panel 3
specifyLabel = new JLabel( "WELCOME TO THE HELP PANEL");
specifyLabel.setBounds( 40, 20, 200, 20 );
panel3.add( specifyLabel );
// Add Help Label into Panel 3
helpLabel = new JLabel( "Step 1. LOAD MODEL SET");
helpLabel.setBounds( 40, 60, 200, 20 );
panel3.add( helpLabel );
// Add Help2 Label into Panel 3
help2Label = new JLabel( "Step 2. INSERT ALL NECCESARY DATA INTO FIELDS");
help2Label.setBounds( 40, 80, 300, 20 );
panel3.add( help2Label );
// Add Help3 Label into Panel 3
help3Label = new JLabel( "Step 3. SAVE / EDIT LIST AND PROCEED TO PANEL 2 ");
help3Label.setBounds( 40, 100, 300, 20 );
panel3.add( help3Label );
// Add Help4 Label into Panel 3
help4Label = new JLabel( "Step 4. INSERT ALL NECCESARY DATA ");
help4Label.setBounds( 40, 120, 300, 20 );
panel3.add( help4Label );
// Add Help5 Label into Panel 3
help5Label = new JLabel( "Step 5. INSERT ALL NECCESARY DATA ");
help5Label.setBounds( 40, 140, 300, 20 );
panel3.add( help5Label );
getContentPane().add( tabbedPane );
setSize( 640, 400 );
setVisible(true);
//Panel 1 handlers------------------------------------------------------
// Help button to check fields entry
modelInfoButton.addActionListener (
new ActionListener()
{ public void actionPerformed( ActionEvent e) {showModelInfo();} }
);
// Enter button to check fields entry
enterButton.addActionListener (
new ActionListener()
{ public void actionPerformed(ActionEvent e) {readInput();} }
);
// button to remove selected items from temp list
removeTempItemsButton.addActionListener (
new ActionListener()
{ public void actionPerformed(ActionEvent e) {removeTempItems();} }
);
// Save button call
saveListButton.addActionListener (
new ActionListener()
{ public void actionPerformed(ActionEvent e) {saveList();} }
);
// Load button call
loadModelsButton.addActionListener(
new ActionListener()
{ public void actionPerformed(ActionEvent e) {loadModels();} }
);
// Clear button
deleteModelsButton.addActionListener (
new ActionListener()
{ public void actionPerformed( ActionEvent e) {deleteModels();} }
);
//Panel 2 handlers------------------------------------------------------
// Load button call
loadListButton.addActionListener(
new ActionListener()
{ public void actionPerformed(ActionEvent e) {openList();} }
);
// Clear button
clearListButton.addActionListener (
new ActionListener()
{ public void actionPerformed(ActionEvent e) {clearList();} }
);
// Sort by model button
modelButton.addActionListener (
new ActionListener()
{
public void actionPerformed(ActionEvent e) {prodList.sort(0);
scrollListContent.setText(prodList.toString());
} }
);
// Sort by thickness button
modelButton.addActionListener (
new ActionListener()
{
public void actionPerformed(ActionEvent e) {prodList.sort(1);
scrollListContent.setText(prodList.toString());
} }
);
// Width sort button
widthButton.addActionListener (
new ActionListener()
{
public void actionPerformed(ActionEvent e) {prodList.sort(2);
scrollListContent.setText(prodList.toString());
} }
);
// Color sort button
colorButton.addActionListener (
new ActionListener()
{
public void actionPerformed(ActionEvent e) {prodList.sort(3);
scrollListContent.setText(prodList.toString());
} }
);
//Panel 3 handlers------------------------------------------------------
// Load button call
}
//read from input fields, then add item to list
private void readInput()
{
String modelNo = (String)(modelComboBox.getSelectedItem());
if (modelNo == null) return;
try {
int quantity = Integer.parseInt(quantityField.getText());
double thickness = Double.parseDouble(thicknessField.getText());
double width = Double.parseDouble(widthField.getText());
String color = (String)(colorComboBox.getSelectedItem());
double plys = Double.parseDouble(plysField.getText());
if (quantity < 0 || thickness < 0 || width < 0 || plys < 0)
throw new NumberFormatException("Negative attributes");
prodList.add(
new TreeData( modelNo, quantity, thickness, width, color, plys));
// Repaints the scroll pane to display newly entered data
enteredItemsList.setListData(prodList);
}
catch (NumberFormatException e)
{ JOptionPane.showMessageDialog(this, "At least 1 field is illegal",
"Error", JOptionPane.ERROR_MESSAGE ); }
}
private void removeTempItems()
{
int[] select = enteredItemsList.getSelectedIndices();
for (int i = select.length-1; i >= 0; i--)
{
int selectIndex = select[i];
if (selectIndex < 0) return;
prodList.removeElementAt(selectIndex);
enteredItemsList.removeSelectionInterval(selectIndex, selectIndex);
enteredItemsList.repaint();
}
}
//pop up a window showing model info
private void showModelInfo()
{
//(String)(modelComboBox.getSelectedItem())
}
/**
* CLEAR FILE
* Clears the scroll list pane in the 2nd panel
*/
private void clearList()
{
this.prodList = null; //Cleanup by garbage collector
scrollListContent.setText("No list loaded.");
panel2.repaint();
}
/**
* OPEN FILE
* Opens
*/
private void openList()
{
File inFile = chooseFile(false);
if (inFile == null) return;
prodList = new OrderList(inFile, this);
scrollListContent.setText(prodList.toString());
//panel2.repaint();
}
//save list to file
private void saveList()
{
if (prodList.size() == 0) {
JOptionPane.showMessageDialog(this, "There is nothing to save",
"ERROR", JOptionPane.ERROR_MESSAGE );
return;
}
File outFile = chooseFile(true);
if (outFile == null) return;
if (outFile.exists())
if (JOptionPane.showConfirmDialog(this,
"File " + outFile.getName() + " already exists. Overwrite?",
"WARNING", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE) == JOptionPane.NO_OPTION)
return;
try {
FileWriter output = new FileWriter(outFile);
String toWrite = prodList.toString();
output.write(toWrite, 0, toWrite.length());
output.flush();
output.close();
}
catch (IOException e)
{ JOptionPane.showMessageDialog(this, "File cannot be saved",
"ERROR", JOptionPane.ERROR_MESSAGE ); }
}
//load list of available models to combo box
private void loadModels()
{
File inFile = chooseFile(false);
if (inFile == null) return;
try {
if (!(inFile.exists()))
throw new IOException("File does not exist");
modelComboBox.removeAllItems(); //clean up before adding
Scanner in = new Scanner(inFile);
while (in.hasNextLine()) {
Scanner lineScanner = new Scanner(in.nextLine());
try {
String modelID = lineScanner.next();
double height = lineScanner.nextDouble();
int tipNum = lineScanner.nextInt();
int group = lineScanner.nextInt();
modelList.add(new Model(modelID, height, tipNum, group));
modelComboBox.addItem(modelID);
}
catch (NoSuchElementException e) { continue; }
}
}
catch (IOException e)
{ JOptionPane.showMessageDialog(this, "Error loading file",
"Error", JOptionPane.ERROR_MESSAGE); }
}
private File chooseFile(boolean saveDialog)
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
// open file chooser; if user clicked Cancel then return null
int chooserDialogReturn;
if (saveDialog)
chooserDialogReturn = fileChooser.showSaveDialog(this);
else
chooserDialogReturn = fileChooser.showOpenDialog(this);
if (chooserDialogReturn == JFileChooser.CANCEL_OPTION)
return null;
return fileChooser.getSelectedFile(); // get selected file
}
//clear model dropdown list
private void deleteModels()
{
modelList.removeAllElements();
modelComboBox.removeAllItems();
}
//================================MAIN======================================
public static void main(String args[])
{
TreeClassics tabbedPane = new TreeClassics();
tabbedPane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Here is the TreeData
/**
* The TreeData class
*/
public class TreeData
{
private String modelNo; // Tree model number (Format: XX-XXX-XXX)
private int quantity; // Quantity of desired tree model
private double thickness; // Thickness of plys needed for each tree (mm)
private double width;
private String color;
private double plys;
// Class constructor (blank data)
public TreeData()
{}
// Class constructor (new data)
public TreeData(String modelNo, int quantity, double thickness,
double width, String color, double plys)
{
setModelNo(modelNo);
setQuantity(quantity);
setThickness(thickness);
setWidth(width);
setColor(color);
setPlys(plys);
}
public void setQuantity(int quantity)
{this.quantity = quantity; }
public int getQuantity()
{return quantity; }
public void setModelNo(String modelNo)
{this.modelNo = modelNo;}
public String getModelNo()
{return modelNo;}
public void setThickness(double thickness)
{this.thickness = thickness;}
public double getThickness()
{return thickness;}
public void setWidth(double width)
{this.width = width;}
public double getWidth()
{return width;}
public void setColor(String color)
{this.color = color;}
public String getColor()
{return color;}
public void setPlys(double plys)
{this.plys = plys;}
public double getPlys()
{return plys;}
public String toString()
{
return modelNo + " " + quantity + " " + thickness + " " + width +
" " + color + " " + plys;
}
public boolean equals(Object o)
{
if (!(o instanceof TreeData))
throw new IllegalArgumentException("Wrong type: expects TreeData");
TreeData item = (TreeData)o;
if (item.modelNo == modelNo &&
item.thickness == thickness &&
item.width == width &&
item.color.equals(color) &&
item.plys == plys)
return true;
else
return false;
}
}
Here is OrderList
/**
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class OrderList extends Vector<TreeData>
{
public static final int SORT_BY_MODEL_NO = 0;
public static final int SORT_BY_THICKNESS = 1;
public static final int SORT_BY_WIDTH = 2;
public static final int SORT_BY_COLOR = 3;
// Constructor
public OrderList()
{
super(); // accomodate 5 items, extend by 5 when overflow
}
/**
* Constructor with stuff
* @param file
*/
public OrderList(File inFile, Component c)
{
try
{
Scanner in = new Scanner(inFile);
while (in.hasNextLine())
{
Scanner lineScanner = new Scanner(in.nextLine());
lineScanner.useDelimiter("\t");
try {
String model = lineScanner.next();
int quantity = lineScanner.nextInt();
double thickness = lineScanner.nextDouble();
double width = lineScanner.nextDouble();
String color = lineScanner.next();
double plys = lineScanner.nextDouble();
add(new TreeData(model, quantity, thickness, width, color, plys));
}
catch (NoSuchElementException e) { continue; }
}
}
catch (IOException e)
{ JOptionPane.showMessageDialog(c, "Error Opening File",
"Error", JOptionPane.ERROR_MESSAGE ); }
}
//add item to order list; if item of same specs exists, add to quantity
public boolean add(TreeData item)
{
for (int i = 0; i < size(); i++) {
TreeData temp = get(i);
if (temp.equals(item)) {
temp.setQuantity(temp.getQuantity() + item.getQuantity());
return true;
}
}
return super.add(item);
}
/**
*
* @param sortType
*/
public void sort(int sortType)
{
quicksort(0, size()-1, sortType);
}
/**
* Sort an array using Quicksort algorithm. Adapted from method in C at
* http://www.tcnj.edu/~mmmartin/CMSC410/qs-complexity.doc
* @param a An array of Comparable items.
* @param left Start point of range.
* @param right End point of range.
*/
private void quicksort(int left, int right, int sortType)
{
if (left >= right) return;
else
{
int l = left;
int r = right;
TreeData pivot = get((l+r)/2); // choose pivot as middle item
// partition
do
{
while (compare(get(l),pivot,sortType) < 0) l++;
while (compare(get(r),pivot,sortType) > 0) r--;
if (l <= r)
{
// swap a[l] and a[r]
TreeData tmp = get(l);
set(l, get(r));
set(r, tmp);
// move on
l++;
r--;
}
}
while (l <= r);
// recursively sort left and right partitions
quicksort(left, r, sortType);
quicksort(l, right, sortType);
}
}
/**
* @param a First object of field type to compare
* @param b Second object of field type to compare
* @param sortType Identifier for field type
*/
private int compare(TreeData a, TreeData b, int sortType)
{
String s1, s2;
double d1, d2;
// Sorts by model number or color (string type)
switch (sortType)
{
case SORT_BY_MODEL_NO:
s1 = a.getModelNo();
s2 = b.getModelNo();
// If s1 is <= s2, returns true
return s1.compareTo(s2);
case SORT_BY_COLOR:
s1 = a.getColor();
s2 = b.getColor();
// If s1 is <= s2, returns true
return s1.compareTo(s2);
case SORT_BY_THICKNESS:
// Class compare
d1 = a.getThickness();
d2 = b.getThickness();
return Double.compare(d1,d2);
case SORT_BY_WIDTH:
// Class compare
d1 = a.getWidth();
d2 = b.getWidth();
return Double.compare(d1,d2);
}
return 0;
}
/**
*
*/
public String toString()
{
String listOutput = "";
for (int i = 0; i < size(); i++)
{
TreeData item = get(i);
if (item == null) break;
listOutput += item.toString() + "\r\n";
}
return listOutput;
}
/**
*
*/
public static void main(String[] args)
{
OrderList list = new OrderList();
list.sort(SORT_BY_WIDTH);
for (int i = 0; i<5; i++)
System.out.println(list.get(i).toString());
}
}
And Finally Models here
public class Model
{
private String modelNo;
private double height;
private int tipNum;
private int group;
// Blank constructor
public Model()
{}
// Enter values for model information
public Model(String modelNo, double height, int tipNum, int group)
{
setModelNo(modelNo);
setHeight(height);
setTipNum(tipNum);
setGroup(group);
}
// Model number methods
public void setModelNo(String modelNo)
{this.modelNo = modelNo;}
public String getModelNo()
{return modelNo;}
// Height methods
public void setHeight(double height)
{this.height = height;}
public double getHeight()
{return height;}
// tipNum methods
public void setTipNum(int tipNum)
{this.tipNum = tipNum;}
public int getTipNum()
{return tipNum;}
// group methods
public void setGroup(int group)
{this.group = group;}
public int getGroup()
{return group;}
}
It's probably a really basic problem but i can't seem to pinpoint it. Thanks in advance guys.