| | |
Impementing First GUI into Current App
Thread Solved
![]() |
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:
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.
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.
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.
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:
Java Syntax (Toggle Plain Text)
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
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.
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
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.
Last edited by no1zson; Jul 22nd, 2007 at 8:52 pm.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
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?
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?
Last edited by no1zson; Jul 22nd, 2007 at 11:49 pm.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
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.
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.
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?
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
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 f
} // end mainI 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?
Last edited by no1zson; Jul 23rd, 2007 at 1:28 pm.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
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.
Java Syntax (Toggle Plain Text)
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 main
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
By 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: 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.
The no class found is because you have used a variable you haven't defined (you mispelled it). Also, this line:
Java Syntax (Toggle Plain Text)
GuicdInventory guiceInentory = new GuicdInventory;
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.
I am going to go to lunch before I get too aggravated. Maybe it will look differently when I get back! :o)
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.
Java Syntax (Toggle Plain Text)
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 never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
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.
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
It is complaining because this line
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
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
Java Syntax (Toggle Plain Text)
cDJList = new JList(Object[] guicdInventory);
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
Java Syntax (Toggle Plain Text)
public String toString()
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
![]() |
Other Threads in the Java Forum
- Previous Thread: How to download a file
- Next Thread: Java image programming question
| Thread Tools | Search this Thread |
911 addball addressbook android api applet application apps array automation awt binary bluetooth businessintelligence busy_handler(null) button card class client code collision component constructor crashcourse css csv database draw eclipse ee error eventlistener exception fractal free game gis givemetehcodez graphics gui html ide image integration j2me japplet java javaarraylist javadoc javafx javamicroeditionuseofmotionsensor javaprojects jni jpanel jtree julia jvm linux loan map method migrate mobile netbeans objects oracle oriented phone physics plazmic problem program programming project projects radio recursion replaydirector reporting scanner se server service set sharepoint smart sms software sortedmaps sql swing test textfield threads tree trolltech ubuntu unlimited utility windows






