I have spent the weekend reading about GUI and how Java uses it, and all I got was confused. :o) I decided the best way to learn it was just to get in to it and start coding, so that is what I did. Went better than I thought it would.
I feel I have a pretty good grip on it (at least for 1 day of practice) but what I do not seem to be able to figure out is how to alter my current cd inventory application to start using the GUI instead of my old DOS prompts. I want to use the same logic, same rules, and all that, except now I want the information to be entered from, and displayed in a GUI interface.
Do I have to write a whole new class and discard the Inventory one I use currently, or is there a way to apply the tried and tested rules I currently have and just mesh in GUI methods and constructors to take the place of what is there?
Any help would be welcome.
Here is my current GUI that I have been working on. It is simple, I just wanted to get the first two fields up to play with them.
First:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CdTextFrame extends JFrame
{
public JTextField cdField1; // JLabel for CdwArtist class
public JTextField cdField2;
public JTextField cdField3;
public JTextField cdField4;
//CdFrame contructor adds Text Fields to JFrame
public CdTextFrame()
{
super("CD Inventory");
setLayout(new FlowLayout()); // set frame layout
// 1st Field
cdField1 = new JTextField("CD Name");
cdField1.setEditable(false);
add(cdField1);
// 2nd Field
cdField2 = new JTextField(10);
cdField2.setToolTipText("Enter CD Name Here");
add (cdField2);
// 3rd Field
cdField3 = new JTextField("Artist");
cdField3.setEditable(false);
add(cdField3);
// 4th Field
cdField4 = new JTextField(10);
cdField4.setToolTipText("Enter Performing Artist Here");
add (cdField4);
// register event handlers
CdFieldHandler handler = new CdFieldHandler();
cdField2.addActionListener(handler);
cdField4.addActionListener(handler);
} // end constructor CdFrame
// private inner class for event handling
private class CdFieldHandler implements ActionListener
{
// process text field events method
public void actionPerformed(ActionEvent event)
{
String string = ""; //declare string to display
// user pressed Enter in JTextField cdField2
if (event.getSource()==cdField2)
string = String.format("textField2: %s",
event.getActionCommand());
// user pressed Enter in JTextField cdField4
if (event.getSource()==cdField4)
string = String.format("textField4: %s",
event.getActionCommand());
// display JTextField content
JOptionPane.showMessageDialog(null, string);
} // end process text fields method
} // end event handling class
} // end clss CdFrame Does not really do anything yet, I put a listener in there just to try it. Right now it pops open a window with whatever you enter in the field, I think there is probably where I would have it write to my array, or is it? This is where I get in over my head. I do not know what to do with any of it yet.
Next is my tester class, again, it does nothing of real substance, just something for me to test and play with. I am wondering if this is where I will have to lay down new code to replace the rules in my Inventory class that I have used up until now.
import javax.swing.JFrame;
public class Cdguitext
{
public static void main(String args[])
{
CdTextFrame cdTextFrame = new CdTextFrame(); // create cdframe
cdTextFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cdTextFrame.setSize(375,180);
cdTextFrame.setVisible(true);
} // end main
} // end class Here is that class Inventory class. There are two classes (Compactdisk and CdwArtist) that this class uses for its parameters, if anyone needs to see them just let me know. They also are pretty basic get and set classes.
import java.util.*;
public class Inventory
{// begin class Inventory
public static int maxlength = 0;
public static CdwArtist[] sort(CdwArtist[] cds)
{
Arrays.sort(cds, 0, maxlength);
return cds;
}
public static String toString(CdwArtist[] cds)
{
String toSend = "\n\n";
for(int i = 0; i < maxlength; i ++)
toSend = toSend + cds[i].getName() + "\n";
return toSend;
}
public static void main(String[] args)
{//begin method main
// create cd Array
CdwArtist[] cds = new CdwArtist[100];
float totalValue = 0;
Scanner input = new Scanner(System.in); // create scanner
// begin display method
System.out.print("Enter up to 99 CD Names or STOP to Exit: ");
String nameInput = input.nextLine(); //read cd name
for(int i = 0; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++)
{// begin main While
cds[i] = new CdwArtist();
cds[i].setName(nameInput);
System.out.print("Enter CD Artist Name: "); // prompt for artist name
CdwArtist artist = new CdwArtist(input.nextLine());
System.out.print("Enter Price of this CD: "); // prompt for price
cds[i].setPrice(input.nextFloat()); // price input from user.
while (cds[i].getPrice()<= 0)
{// begin while
System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
cds[i].setPrice(input.nextFloat()); // cd price loop from user.
} // End while
System.out.print("Enter CD Item Number: "); // prompt for cd item number
cds[i].setItemno(input.nextInt()); // cds item number input from user
System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
cds[i].setNstock(input.nextInt()); // cds in stock input from user
System.out.print("\n\nCD "+cds[i].getName()+", Item Number "+cds[i].getItemno()+","); // display name
System.out.printf(" is worth %c%.2f.",'$', + cds[i].getPrice());//
System.out.print("\nWe have "+ cds[i].getNstock()+" copies in stock,");
System.out.printf(" making our inventory for this cd worth %c%.2f.\n", '$', + cds[i].getValue()); //inventory value
if(cds[i].getValue() != -1) totalValue = totalValue + cds[i].getValue();
System.out.printf("Combined Inventory for all CDs is Worth %c%.2f.\n\n\n", '$', + totalValue);
System.out.print("Enter up to 99 CD Names or STOP to Exit: ");
input=new Scanner(System.in); // internal loop prompt
nameInput = input.nextLine(); //name input from user
maxlength ++;
} // End main While
//System.out.println(toString(cds));
System.out.println(toString(sort(cds)));
System.out.print("Ending Program.");
}// end method main
} // end class Payroll
I really hate to think all that coding goes to waste if I choose to use a GUI interface instead, I was hoping there was a way to put my GUI methods in this existing class, but if not ... then oh well, all part of the learning process I guess.
What I am trying to wrap my brain around, is how the GUI is going to talk to the array, and vice versa.
I had it in my mind (before I started) that when I began defining the fields of the GUI; field2, field4 and so on, that I would somehow be able to map the data that I entered into these new fields back to that array.
I am not sure I can do it that way anylonger, but I already have an array defined, working, and ready to be used, certainly I can create a GUI that uses it as it is, cant I?
Am I making sense?
You have two options (well, a lot more than 2 actually, but here are two):
Move the code that manages your array into the CdTextFrame class.
Give the Inventory class methods that let you add, remove, and view the CDs and call those methods from your CdTextFrame class.
Instead of a loop using Scanner, you will need a button to Add CD. You will want a JList to display the names of the current CDs in the array. You could also add buttons to Edit or Remove a CD from the Inventory. The listeners for these buttons can act upon the array itself if you have it in the CdTextFrame class, or call methods on the Inventory if you wish to put them there.
OK, I wanted to learn JList weeks ago, but did not have what I considered a good opportunity. I also will need to learn buttons, so I am going with option 2 here.
I have scrapped my CdTextFrame class completely and tried to start a JList the way my book has it, but I am missing something.
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuicdInventory extends JFrame
{
Inventory guiInventory = new Inventory();
private Jlist cDJList;
//contructor adds Inventory to JFrame
public GuicdInventory()
{
super("CD Inventory");
setLayout(new FlowLayout()); // set frame layout
<strong>cDJList = new JList(Object[] guicdInventory);</strong>
cDJList.setVisibleRowCount(99);
add(new JScrollPane(cDJList));
} // end constructor
public static void main(String args[])
{
GuicdInventory guiceInentory = new GuicdInventory;
GuicdInventory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuicdInventory.setSize(500,800);
GuicdInventory.setVisible(true);
} // end f
} // end main
I want to learn this a step at a time so that I do not get in over my head, but I am getting .class expected, and ; expected on the bolded line above, pointing at "guiInvnetory".
If I am understanding you, I will want to create the JList in this new class, and a button in my old Inventory class to replace my scanner, and that will enable me to simply list my array inventory?
Well, I had noticed buttons before, so I think this is pretty close to right. How I replace the loop with one is a bit of a mystery at this point, but right now, even with all the new stuff here I am still getting that same message on the same line.
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuicdInventory extends JFrame
{
Inventory guiInventory = new Inventory();
private Jlist cDJList;
//contructor adds Inventory to JFrame
public GuicdInventory()
{
super("CD Inventory");
setLayout(new FlowLayout()); // set frame layout
// adds buttons
addButton = new Button("Add");
add (addButton);
// create an object to listen to buttons
ButtonListener myButtonListener = new ButtonListener();
// tell buttons that myButtonListener should be notified
addButton.addActionListener(myButtonListener);
setVisible(true);
cDJList = new JList(Object[] guicdInventory);
cDJList.setVisibleRowCount(99);
add(new JScrollPane(cDJList));
} // end constructor
public static void main(String args[])
{
GuicdInventory guiceInentory = new GuicdInventory;
GuicdInventory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuicdInventory.setSize(500,800);
GuicdInventory.setVisible(true);
} // end
} // end mainBy starting on GUI programming, you are venturing a lot deeper into OO programming then you've ever been before. Java has some extremely flexible APIs for visual components but they come at a cost of complexity.
The no class found is because you have used a variable you haven't defined (you mispelled it). Also, this line:
GuicdInventory guiceInentory = new GuicdInventory; will not compile - no parenthesis.
The button to add new will not go into another class. It has to remain in the JFrame class. You will need the action listener to perform whatever code is needed to add the new CD.
Yeah, I am seeing that. I want to try and keep this as simple as possible. Too complicated and I am just going to frustrate myself.
I found the misspellings and the parenthesis in my last post, I think you were looking at code I posted before I found them. I did put the Button code in my JFrame extention also.
Editing the loop will be the next step, but first I have to get this list working and I am still getting the same error on the same line.
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuicdInventory extends JFrame
{
Inventory guicdInventory = new Inventory();
private Jlist cDJList;
//contructor adds Inventory to JFrame
public GuicdInventory()
{
super("CD Inventory");
setLayout(new FlowLayout()); // set frame layout
// adds buttons
addButton = new Button("Add");
add (addButton);
// create an object to listen to buttons
ButtonListener myButtonListener = new ButtonListener();
cDJList = new JList(Object[] guicdInventory);
cDJList.setVisibleRowCount(99);
add(new JScrollPane(cDJList));
} // end constructor
public static void main(String args[])
{
GuicdInventory guicdInentory = new GuicdInventory;
GuicdInventory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuicdInventory.setSize(500,800);
GuicdInventory.setVisible(true);
// tell buttons that myButtonListener should be notified
addButton.addActionListener(myButtonListener);
setVisible(true);
} // end
} // end main
I am going to go to lunch before I get too aggravated. Maybe it will look differently when I get back! :o)
I hate that site. I was trying to read up on arrays there this morning. Just made me more confused.
I know it is helpful to people that already understand what is going on, but for me, being the first time I have ever attempted any of this, it is vague examples I do not understand.
It is good for giving me an idea of what I should try, but when I have specific questions it just makes my head swim ... like now.
It is complaining because this line
cDJList = new JList(Object[] guicdInventory);
is not a valid parameter to the constructor - you don't need the Object[] part, just a variable that is an array of Object.
Keep in mind, if you pass in an object array, JList will use the value of toString() for that object in the list. You may have to override
public String toString()
in your CD class to have the name or other description appear in your list.
Also, if you haven't added any CDs to the array at the point you create the list, there won't be anything to display :)
I did have concerns about the toString, but again fall into unfamiliar territory with that function.
I do understand what you say about the list being empty, which goes back to an overall question about how the GUI pulls information, and from where.
I think I am way over my head here, I think I probably need to scrap the JLIST and just go back to the original plan and insert everything into a new gui class that uses my Compactdisk and Cdwartist classes.
Right now I am trying to build on a foundation I have not properly laid yet.
1st thing I need to do is get a QUI screen with some fields in it where I can enter information into my existing array.
Then I will worry about sorting, adding and all that other stuff.
Would you suggest I take everything from Inventory and just re-write it into my new class, so that I do not use Inventory anymore?
You could, if you feel that is easier for you to logically work with. JList has it's own container (the list model) that you can put CDs into. The part from your loop in inventory will largely be replaced by code in a button that creates a new CD, reads the info from your text fields and sets the properties on the CD, and then adds it to the list model.
That is a lot of my problem right there, at this point I am not even sure what I should do. This is my first attempt at GUI so I wanted to stay as basic as possible, and keep as much of my existing code as possible because I understand it as it is (for the most part.)
I have never used JList either, so between that and GUI both at once I am just getting confused as to what is changing by implimenting one, and what changes because of the other.
I am looking at the code I have attempted today, some for GUI frames, some for buttons, some for JList ... all of it new, and none of it clear.
I think I am going to delete everything I have tried up to this point, and just start over. :o(
Yes, GUI code can be a little complex to jump into. You just have to start simple and keep at it. Perhaps this demo will help. It's just a tiny form I knocked up that let's you add a CD with artist and title to a JList.
import javax.swing.DefaultListModel;
public class ListDemo extends javax.swing.JFrame {
private javax.swing.JButton btnAdd;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lblArtist;
private javax.swing.JLabel lblTitle;
private javax.swing.JList lstInventory;
private javax.swing.JTextField txtArtist;
private javax.swing.JTextField txtTitle;
private DefaultListModel listModel;
public ListDemo() {
initComponents();
}
private void initComponents() {
lblArtist = new javax.swing.JLabel();
txtArtist = new javax.swing.JTextField();
lblTitle = new javax.swing.JLabel();
txtTitle = new javax.swing.JTextField();
btnAdd = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
lstInventory = new javax.swing.JList();
getContentPane().setLayout(new java.awt.FlowLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
lblArtist.setText("Artist");
getContentPane().add(lblArtist);
txtArtist.setMinimumSize(new java.awt.Dimension(75, 19));
txtArtist.setPreferredSize(new java.awt.Dimension(75, 19));
getContentPane().add(txtArtist);
lblTitle.setText("Title");
getContentPane().add(lblTitle);
txtTitle.setMinimumSize(new java.awt.Dimension(75, 19));
txtTitle.setPreferredSize(new java.awt.Dimension(75, 19));
getContentPane().add(txtTitle);
btnAdd.setText("Add");
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnAdd);
listModel = new DefaultListModel();
lstInventory.setModel(listModel);
jScrollPane1.setViewportView(lstInventory);
getContentPane().add(jScrollPane1);
pack();
}
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
// Create cd and set title
CD cd = new CD(txtArtist.getText());
cd.setTitle(txtTitle.getText());
// Add the cd to list
listModel.addElement(cd);
// Clear the text fields
txtArtist.setText(null);
txtTitle.setText(null);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ListDemo().setVisible(true);
}
});
}
/** Small CD class to hold our data */
class CD {
private String artist;
private String title;
public CD(String artist){
this.artist = artist;
}
public void setTitle(String title){
this.title = title;
}
public String toString(){
return artist + " - " + title;
}
}
}I am going to see what I can do with that and get back with you tomorrow.
I appreciate your patience.
Just so you know I was not just sitting around waiting on code, here is what I had come up with over the afternoon ... looking at what you hve done I think I was at least headed in the right direction. I was rewriting my entire Inventory class from the ground up. I am not exactly sure where I was going with it, but I kept the idea stream flowing and this is where it took me. :o)
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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 JLabel valueLabel; // value of that item label
private JLabel rstkLabel; // cost to restock label
private JLabel totalLabel; // total value of inventory 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 JFormattedTextField valueField; // value of that item display
private JFormattedTextField rstkField; // cost to restock display
private NumberFormat nstockFormat; // format field and parse numbers
private NumberFormat priceFormat; // format field and parse numbers
private NumberFormat valueFormat; // format field and parse numbers
private NumberFormat rstkFormat; // format field and parse numbers
private JButton firstBtn; // first button
private JButton prevBtn; // previous button
private JButton nextBtn; // next button
private JButton lastBtn; // last 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; // current product display to use for button action
private int i; // iterator
private double total = 0; // variable for total inventory
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:");
valueLabel = new JLabel("Value of Item Inventory:");
rstkLabel = new JLabel("Cost to Re-Stock Item:");
totalLabel = new JLabel("Total Value of Inventory:$"+total);
// create button names
firstBtn = new JButton("First");
prevBtn = new JButton("Previous");
nextBtn = new JButton("Next");
lastBtn = new JButton("Last");
// create textFields and set uneditable
cdNameField = new JTextField(15);
cdNameField.setEditable(false);
artistField = new JTextField(15);
artistField.setEditable(false);
;
nstockField = new JFormattedTextField(nstockFormat);
nstockField.setEditable(false);
nstockField.setColumns(10);
priceField = new JFormattedTextField(priceFormat);
priceField.setEditable(false);
priceField.setColumns(10);
itemField = new JTextField(15);
itemField.setEditable(false);
valueField = new JFormattedTextField(valueFormat);
valueField.setEditable(false);
valueField.setColumns(10);
rstkField = new JFormattedTextField(rstkFormat);
rstkField.setEditable(false);
rstkField.setColumns(10);
// add buttons to panel
JPanel buttons = new JPanel(); // set up panel
buttons.setLayout( new GridLayout(1, 4)); //set layout
// add buttons to buttons
buttons.add(firstBtn);
buttons.add(prevBtn);
buttons.add(nextBtn);
buttons.add(lastBtn);
// create cd Array
CdwArtist[] cds = new CdwArtist[100];
for(int i = 0; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++)
cds[i] = new CdwArtist();
cds[i].setName(nameInput);
//total += cdProduct[currCD].calcValue(); // calculate total inventory cost
// add values to textFields
cdNameField.setText(cds[i].getProdName());
artistField.setValue(new Integer(cds[i].getArtist()));
nstockField.setValue(new Integer(cds[i].getUnits()));
priceField.setValue("$"+new Double(cds[i].getPrice()));
itemField.setText(cds[i].getFeature());
valueField.setValue("$"+new Double(cds[i].calcValue()));
rstkField.setValue("$"+new Double(cds[i].calcValueRstk()))I did not understand it at first, but after sitting down and comparing it to what I was writing I could see what I was trying to do, and it cleared up in my head.
Then I started coding, and as I created everything I could see the logic behind what was being built, how the buttons, fields, and JList all work together.
I do have a few questions of course, if you have a moment, ;)
Here is everything, from your original work, to what I have added:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.lang.*;
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 JLabel valueLabel; // value of that item label
private JLabel rstkLabel; // cost to restock label
private JLabel totalLabel; // total value of inventory 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 JFormattedTextField valueField; // value of that item display
private JFormattedTextField rstkField; // cost to restock display
private JFormattedTextField totalField; // value of all inventory combine
private NumberFormat nstockFormat; // format field and parse numbers
private NumberFormat priceFormat; // format field and parse numbers
private NumberFormat valueFormat; // format field and parse numbers
private NumberFormat rstkFormat; // 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 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; // current product display to use for button action
private int i; // iterator
private double total = 0; // variable for total inventory
private JList Inventorylist; // JList to take place of old array
private DefaultListModel listModel;
private JScrollPane jScrollPanel;
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:");
valueLabel = new JLabel("Value of Item Inventory:");
rstkLabel = new JLabel("Cost to Re-Stock Item:");
totalLabel = new JLabel("Total Value of Inventory:$"+total);
// JList
jScrollPanel = new JScrollPane();
Inventorylist = new JList();
// buttons
btnAdd = new JButton();
btnNext = new JButton();
btnPrev = new JButton();
btnDel = new JButton();
getContentPane().setLayout(new FlowLayout());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// place textFields and labels
//artist
artistLabel.setText("Artist");
getContentPane().add(artistLabel);
artistField.setMinimumSize(new Dimension(75,19));
artistField.setPreferredSize(new Dimension(75,19));
getContentPane().add(artistField);
// cd name
cdNameLabel.setText("CD Name");
getContentPane().add(cdNameLabel);
cdNameField.setMinimumSize(new Dimension(75,19));
cdNameField.setPreferredSize(new Dimension(75,19));
getContentPane().add(cdNameField);
// copies in stock
nstockLabel.setText("Copies In Stock");
getContentPane().add(nstockLabel);
nstockField.setMinimumSize(new Dimension(75,19));
nstockField.setPreferredSize(new Dimension(75,19));
getContentPane().add(nstockField);
//price of cd
priceLabel.setText("Price");
getContentPane().add(priceLabel);
priceField.setMinimumSize(new Dimension(75,19));
priceField.setPreferredSize(new Dimension(75,19));
getContentPane().add(priceField);
//item number of cd
itemLabel.setText("Item Number");
getContentPane().add(itemLabel);
itemField.setMinimumSize(new Dimension(75,19));
itemField.setPreferredSize(new Dimension(75,19));
getContentPane().add(itemField);;
// value of individual cd in stock
valueLabel.setText("Value");
getContentPane().add(valueLabel);
valueField.setMinimumSize(new Dimension(75,19));
valueField.setPreferredSize(new Dimension(75,19));
getContentPane().add(valueField);
// restocking fee
rstkLabel.setText("Restock Fee");
getContentPane().add(rstkLabel);
rstkField.setMinimumSize(new Dimension(75,19));
rstkField.setPreferredSize(new Dimension(75,19));
getContentPane().add(rstkField);
// total value of inventory
totalLabel.setText("Total Inventory Value");
getContentPane().add(totalLabel);
totalField.setMinimumSize(new Dimension(75,19));
totalField.setPreferredSize(new Dimension(75,19));
getContentPane().add(totalField);
// add buttons
//ADD
btnAdd.setText("Add");
<pre><code>btnAdd.addActionListener(new ActionListener()</code></pre>
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnAdd);
// PREVIOUS
btnPrev.setText("Previous");
<pre><code>btnPrev.addActionListener(new ActionListener()</code></pre>
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnPrev);
// NEXT
btnNext.setText("Next");
<pre><code>btnNext.addActionListener(new ActionListener()</code></pre>
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(evt);
}
});
getContentPane().add(btnNext);
// DELETE
btnDel.setText("Delete");
<pre><code>btnDel.addActionListener(new ActionListener()</code></pre>
{
public void actionPerformed(ActionEvent evt)
{
btnAddActionPerformed(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
CD cd = new CD(artistField.getText());
cd.setcdName(cdNameField.getText());
cd.setitem(itemField.getText());
cd.setnstock(nstockField.getText());
cd.setprice(priceField.getText());
cd.setvalue(valueField.getText());
cd.settotal(totalField.getText());
cd.setrstk(rstkField.getText());
// Add cd to list
listModel.addElement(cd);
// Clear the text fields after add
artistField.setText(null);
cdNameField.setText(null);
itemField.setText(null);
nstockField.setText(null);
priceField.setText(null);
valueField.setText(null);
rstkField.setText(null);
totalField.setText(null);
}// end ADD
// run it
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Inventory2().setVisible(true);
}
});
}
} // close class
That is the first class I have written where I understood what I was doing from start to finish. I do have a few details I need to pick your brain about.
first: CD cd = new CD(artistField.getText());
I know what this is doing, but I am getting the error cannot find symbol pointing at CD.
I even understand the error, I do not see where CD is defined anywhere, nor do I know where it would be put.
second: I also plan to go in an modify my previous Compactdisk and CdwArtist classes for use with this class. How does this class know to use those two?
first: CD cd = new CD(artistField.getText()); I know what this is doing, but I am getting the error cannot find symbol pointing at CD. I even understand the error, I do not see where CD is defined anywhere, nor do I know where it would be put. second: I also plan to go in an modify my previous Compactdisk and CdwArtist classes for use with this class. How does this class know to use those two?
CD was the small inner class that I put in my listing to hold cd data. You will want to change that to your own CdwArtist class. You will need to override the toString method on that class so that it shows what you want in the list also. Look at the bottom of my code for the CD class definition to see how that is done.
I feel like a genious (about the CdwArtist class), I had just gotten that to work, and have my sets going to that class ...
here is what it looks like now
// Create cd to add
CdwArtist cd = new CdwArtist(artistField.getText());
cd.setName(cdNameField.getText());
cd.setItemno(itemField.getText());
cd.setNstock(nstockField.getText());
cd.setPrice(priceField.getText());
cd.setValue(valueField.getText());
cd.setTotal(totalField.getText());
cd.setRestock(rstkField.getText());
It must be working because it sees that some of those fields (Itemno for example) is defined as INT in the other class.
It is erroring telling me that those parameters cannot be applied to java.lang.String
I "kind of" understand what it is saying, I have numbers in these fields, some of which I will be doing calculations as, but it is expecting a String. Right? Where would I alter that?
I am going to look at the toString right now and see if I can figure out what you are telling me there.
Thanks again for your help!
I believe I have the toString right.
The only thing I am having trouble with, logically, and code wise at the moment is how to solve the String/Int problem in Inventory2.
Do these fields not need to be int, double, or float in order to perform calculations on them?