943,938 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 4864
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 22nd, 2007
0

Impementing First GUI into Current App

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5.  
  6. public class CdTextFrame extends JFrame
  7. {
  8. public JTextField cdField1; // JLabel for CdwArtist class
  9. public JTextField cdField2;
  10. public JTextField cdField3;
  11. public JTextField cdField4;
  12.  
  13. //CdFrame contructor adds Text Fields to JFrame
  14. public CdTextFrame()
  15. {
  16. super("CD Inventory");
  17. setLayout(new FlowLayout()); // set frame layout
  18.  
  19. // 1st Field
  20. cdField1 = new JTextField("CD Name");
  21. cdField1.setEditable(false);
  22. add(cdField1);
  23. // 2nd Field
  24. cdField2 = new JTextField(10);
  25. cdField2.setToolTipText("Enter CD Name Here");
  26. add (cdField2);
  27. // 3rd Field
  28. cdField3 = new JTextField("Artist");
  29. cdField3.setEditable(false);
  30. add(cdField3);
  31. // 4th Field
  32. cdField4 = new JTextField(10);
  33. cdField4.setToolTipText("Enter Performing Artist Here");
  34. add (cdField4);
  35.  
  36. // register event handlers
  37. CdFieldHandler handler = new CdFieldHandler();
  38. cdField2.addActionListener(handler);
  39. cdField4.addActionListener(handler);
  40.  
  41. } // end constructor CdFrame
  42.  
  43. // private inner class for event handling
  44. private class CdFieldHandler implements ActionListener
  45. {
  46. // process text field events method
  47. public void actionPerformed(ActionEvent event)
  48. {
  49. String string = ""; //declare string to display
  50.  
  51. // user pressed Enter in JTextField cdField2
  52. if (event.getSource()==cdField2)
  53. string = String.format("textField2: %s",
  54. event.getActionCommand());
  55.  
  56. // user pressed Enter in JTextField cdField4
  57. if (event.getSource()==cdField4)
  58. string = String.format("textField4: %s",
  59. event.getActionCommand());
  60.  
  61. // display JTextField content
  62. JOptionPane.showMessageDialog(null, string);
  63. } // end process text fields method
  64. } // end event handling class
  65. } // 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.
Java Syntax (Toggle Plain Text)
  1. import javax.swing.JFrame;
  2.  
  3. public class Cdguitext
  4. {
  5. public static void main(String args[])
  6. {
  7. CdTextFrame cdTextFrame = new CdTextFrame(); // create cdframe
  8. cdTextFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  9. cdTextFrame.setSize(375,180);
  10. cdTextFrame.setVisible(true);
  11. } // end main
  12. } // 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.
Java Syntax (Toggle Plain Text)
  1.  
  2. import java.util.*;
  3.  
  4. public class Inventory
  5. {// begin class Inventory
  6.  
  7. public static int maxlength = 0;
  8. public static CdwArtist[] sort(CdwArtist[] cds)
  9. {
  10. Arrays.sort(cds, 0, maxlength);
  11. return cds;
  12. }
  13.  
  14. public static String toString(CdwArtist[] cds)
  15. {
  16. String toSend = "\n\n";
  17.  
  18. for(int i = 0; i < maxlength; i ++)
  19. toSend = toSend + cds[i].getName() + "\n";
  20. return toSend;
  21. }
  22.  
  23. public static void main(String[] args)
  24. {//begin method main
  25.  
  26. // create cd Array
  27. CdwArtist[] cds = new CdwArtist[100];
  28.  
  29.  
  30. float totalValue = 0;
  31.  
  32. Scanner input = new Scanner(System.in); // create scanner
  33.  
  34. // begin display method
  35. System.out.print("Enter up to 99 CD Names or STOP to Exit: ");
  36. String nameInput = input.nextLine(); //read cd name
  37.  
  38.  
  39. for(int i = 0; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++)
  40. {// begin main While
  41. cds[i] = new CdwArtist();
  42. cds[i].setName(nameInput);
  43.  
  44. System.out.print("Enter CD Artist Name: "); // prompt for artist name
  45. CdwArtist artist = new CdwArtist(input.nextLine());
  46.  
  47. System.out.print("Enter Price of this CD: "); // prompt for price
  48. cds[i].setPrice(input.nextFloat()); // price input from user.
  49. while (cds[i].getPrice()<= 0)
  50. {// begin while
  51. System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
  52. cds[i].setPrice(input.nextFloat()); // cd price loop from user.
  53. } // End while
  54.  
  55. System.out.print("Enter CD Item Number: "); // prompt for cd item number
  56. cds[i].setItemno(input.nextInt()); // cds item number input from user
  57.  
  58. System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
  59. cds[i].setNstock(input.nextInt()); // cds in stock input from user
  60.  
  61. System.out.print("\n\nCD "+cds[i].getName()+", Item Number "+cds[i].getItemno()+","); // display name
  62. System.out.printf(" is worth %c%.2f.",'$', + cds[i].getPrice());//
  63. System.out.print("\nWe have "+ cds[i].getNstock()+" copies in stock,");
  64. System.out.printf(" making our inventory for this cd worth %c%.2f.\n", '$', + cds[i].getValue()); //inventory value
  65.  
  66. if(cds[i].getValue() != -1) totalValue = totalValue + cds[i].getValue();
  67. System.out.printf("Combined Inventory for all CDs is Worth %c%.2f.\n\n\n", '$', + totalValue);
  68.  
  69. System.out.print("Enter up to 99 CD Names or STOP to Exit: ");
  70. input=new Scanner(System.in); // internal loop prompt
  71. nameInput = input.nextLine(); //name input from user
  72.  
  73. maxlength ++;
  74. } // End main While
  75.  
  76. //System.out.println(toString(cds));
  77. System.out.println(toString(sort(cds)));
  78. System.out.print("Ending Program.");
  79.  
  80. }// end method main
  81. } // 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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 22nd, 2007
0

Re: Impementing First GUI into Current App

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?
Last edited by no1zson; Jul 22nd, 2007 at 11:49 pm.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 23rd, 2007
0

Re: Impementing First GUI into Current App

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 23rd, 2007
0

Re: Impementing First GUI into Current App

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
		
		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 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?
Last edited by no1zson; Jul 23rd, 2007 at 1:28 pm.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 23rd, 2007
0

Re: Impementing First GUI into Current App

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)
  1. import java.util.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5.  
  6. public class GuicdInventory extends JFrame
  7. {
  8. Inventory guiInventory = new Inventory();
  9.  
  10. private Jlist cDJList;
  11.  
  12.  
  13. //contructor adds Inventory to JFrame
  14. public GuicdInventory()
  15. {
  16. super("CD Inventory");
  17. setLayout(new FlowLayout()); // set frame layout
  18.  
  19. // adds buttons
  20. addButton = new Button("Add");
  21. add (addButton);
  22.  
  23. // create an object to listen to buttons
  24. ButtonListener myButtonListener = new ButtonListener();
  25.  
  26. // tell buttons that myButtonListener should be notified
  27. addButton.addActionListener(myButtonListener);
  28. setVisible(true);
  29.  
  30. cDJList = new JList(Object[] guicdInventory);
  31. cDJList.setVisibleRowCount(99);
  32. add(new JScrollPane(cDJList));
  33.  
  34. } // end constructor
  35.  
  36.  
  37. public static void main(String args[])
  38. {
  39. GuicdInventory guiceInentory = new GuicdInventory;
  40. GuicdInventory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  41. GuicdInventory.setSize(500,800);
  42. GuicdInventory.setVisible(true);
  43. } // end
  44.  
  45. } // end main
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 23rd, 2007
1

Re: Impementing First GUI into Current App

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:
Java Syntax (Toggle Plain Text)
  1. 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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 23rd, 2007
0

Re: Impementing First GUI into Current App

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.
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5.  
  6. public class GuicdInventory extends JFrame
  7. {
  8. Inventory guicdInventory = new Inventory();
  9.  
  10. private Jlist cDJList;
  11.  
  12.  
  13. //contructor adds Inventory to JFrame
  14. public GuicdInventory()
  15. {
  16. super("CD Inventory");
  17. setLayout(new FlowLayout()); // set frame layout
  18.  
  19. // adds buttons
  20. addButton = new Button("Add");
  21. add (addButton);
  22.  
  23. // create an object to listen to buttons
  24. ButtonListener myButtonListener = new ButtonListener();
  25.  
  26.  
  27.  
  28. cDJList = new JList(Object[] guicdInventory);
  29. cDJList.setVisibleRowCount(99);
  30. add(new JScrollPane(cDJList));
  31.  
  32. } // end constructor
  33.  
  34.  
  35. public static void main(String args[])
  36. {
  37. GuicdInventory guicdInentory = new GuicdInventory;
  38. GuicdInventory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39. GuicdInventory.setSize(500,800);
  40. GuicdInventory.setVisible(true);
  41. // tell buttons that myButtonListener should be notified
  42. addButton.addActionListener(myButtonListener);
  43. setVisible(true);
  44. } // end
  45.  
  46. } // end main

I am going to go to lunch before I get too aggravated. Maybe it will look differently when I get back! :o)
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 23rd, 2007
0

Re: Impementing First GUI into Current App

Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 23rd, 2007
0

Re: Impementing First GUI into Current App

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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Jul 23rd, 2007
0

Re: Impementing First GUI into Current App

It is complaining because this line
Java Syntax (Toggle Plain Text)
  1. 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
Java Syntax (Toggle Plain Text)
  1. 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
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Reversing the elements of an array
Next Thread in Java Forum Timeline: Java image programming question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC