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.