Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-12-2008, 12:35 AM
Member
 
Join Date: Jan 2008
Posts: 4
glenrowan is on a distinguished road
GUI help please.
Hello everyone,

I am having some trouble adding a "first" and "last" button to my GUI. So far I have just copied the next and previous methods as they are the easy part. I know the code looks ugly to begin with, but please bare with me as I am learning. I have pretty much muddled my way through this class so far and as you can see it shows.

I think I have the "next" and "previous" buttons working right. Well except for my layout, still need to tweak that as well. They are all shifted to the right. Anyway, if someone could point me in the right direction about how to modify the actionlistener to the right area for even one of them I would greatly appreciate it.

Also, I am not asking someone to do my work for me just for some pointers.

Code:
//GUI.java import java.awt.*; import java.awt.event.*; import javax.swing.*; // GUI class creates and maintains the GUI public class GUI extends JFrame { private Inventory inventory; // create new reference to object private JPanel jpOuterPanel; // for outermost frame private JPanel jpInstructsPanel; // the instructions private JPanel currentDataPanel; // the current data header private JLabel jlCurrentDataTitle; //the current data header private JLabel jlCurrentDataContents; // the data contents private JTextField jtfValue; // for data acquisition private JButton jbFirst; // First button private JButton jbNext; // Next button private JButton jbPrevious; // Previous button private JButton jbLast; // Last button private int nCurrentDVDnum; //selecting the current dvd GUI(Inventory inventory) { super(" DVD Inventory Program"); this.inventory = inventory; nCurrentDVDnum = 0; // creates outer panel JPanel jp; jpOuterPanel = new JPanel(); jpOuterPanel.setLayout(new BoxLayout(jpOuterPanel, BoxLayout.Y_AXIS)); // creates title line jpInstructsPanel = new JPanel(); jpInstructsPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); JLabel jLabel = new JLabel("DVD Inventory"); jpInstructsPanel.add(jLabel); jpOuterPanel.add(jpInstructsPanel); // creates current data line jp = new JPanel(); jp.setLayout(new FlowLayout(FlowLayout.CENTER)); jlCurrentDataTitle = new JLabel("" + inventory.getDVD(nCurrentDVDnum)); jp.add(jlCurrentDataTitle); jpOuterPanel.add(jp); // creates the data contents display line jp = new JPanel(); jp.setLayout(new FlowLayout(FlowLayout.CENTER)); jlCurrentDataContents = new JLabel("This DVD collection contains " + inventory.getNcount() + " DVDs"); jp.add(jlCurrentDataContents); jpOuterPanel.add(jp); //create text field for data acquisition jp = new JPanel(); jp.add(new JLabel("Value of DVD collection:")); jp.setLayout(new FlowLayout(FlowLayout.CENTER)); jtfValue = new JTextField(4); jtfValue.setText("$" + inventory.getTotalValue()); jtfValue.setEditable(false); jp.add(jtfValue); jpOuterPanel.add(jp); updateFields(); // updates field after next button // set up and add the first button jp = new JPanel(); jp.setLayout(new FlowLayout(FlowLayout.LEFT)); jbFirst = new JButton("First DVD"); jbFirst.addActionListener(new FirstButtonHandler()); jp.add(jbFirst); jpOuterPanel.add(jp); // set up and add the previous button jp.setLayout(new FlowLayout(FlowLayout.CENTER)); jbPrevious = new JButton("Previous DVD"); jbPrevious.addActionListener(new PreviousButtonHandler()); jp.add(jbPrevious); jpOuterPanel.add(jp); // set up and add the next button jp.setLayout(new FlowLayout(FlowLayout.CENTER)); jbNext = new JButton("Next DVD"); jbNext.addActionListener(new NextButtonHandler()); jp.add(jbNext); jpOuterPanel.add(jp); // set up and add the last button jp.setLayout(new FlowLayout(FlowLayout.RIGHT)); jbLast = new JButton("Last DVD"); jbLast.addActionListener(new LastButtonHandler()); jp.add(jbLast); jpOuterPanel.add(jp); // quit application when window is closed setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(200, 200, 600, 300); // set frame location and size // set up the outer panel, turn it on. setContentPane(jpOuterPanel); setResizable(false); setVisible(true); } // end GUI constructor private void updateFields() // updates info on the current DVD { jlCurrentDataTitle.setText((nCurrentDVDnum+1) + ": " + inventory.getDVD(nCurrentDVDnum)); } // end updateFields method // inner class to handle the first button class FirstButtonHandler implements ActionListener { public void actionPerformed(ActionEvent event) { // sequences through the inventory if (nCurrentDVDnum < inventory.getNcount()) updateFields(); else jbFirst.setEnabled(false); } // end method }// end inner class // inner class to handle the previous button class PreviousButtonHandler implements ActionListener { public void actionPerformed(ActionEvent event) { // sequences through the inventory --nCurrentDVDnum; if (nCurrentDVDnum < inventory.getNcount()) updateFields(); else jbPrevious.setEnabled(false); } // end method }// end inner class // inner class to handle the next button class NextButtonHandler implements ActionListener { public void actionPerformed(ActionEvent event) { // sequences through the inventory ++nCurrentDVDnum; if (nCurrentDVDnum < inventory.getNcount()) updateFields(); //else //jbNext.setEnabled(false); } // end method }// end inner class // inner class to handle the last button class LastButtonHandler implements ActionListener { public void actionPerformed(ActionEvent event) { // sequences through the inventory if (nCurrentDVDnum < inventory.getNcount()) updateFields(); else jbLast.setEnabled(false); } // end method }// end inner class }// end class GUI
[
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-12-2008, 05:51 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
We'll need your Inventory class to compile your code.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-12-2008, 09:35 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
I second what hardwired said.. we need more code.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-12-2008, 08:43 PM
Member
 
Join Date: Jan 2008
Posts: 4
glenrowan is on a distinguished road
I knew I was forgetting something. Thank you for the replies though. And before you even look, I know it doesn't look "pretty" yet. More concerned about functionality then looks at this point. Anyway, here is the rest of my code. And thanks again! After this I will tackle making it look more pleasing to the eye.

Code:
import java.util.Arrays; public class Inventory4 { public static void main(String[] args) { FeatDVD dvd = null; Inventory inventory = new Inventory(); dvd = new FeatDVD(0, "Superman", 5, 12.99f, "Action"); inventory.add(dvd); dvd = new FeatDVD(1, "Resident Evil", 7, 14.99f, "Horror"); inventory.add(dvd); dvd = new FeatDVD(2, "Hostel", 1, 10.00f, "Horror"); inventory.add(dvd); dvd = new FeatDVD(3, "Office Space", 3, 15.99f, "Comedy"); inventory.add(dvd); dvd = new FeatDVD(4, "Rambo", 8, 11.99f, "Action"); inventory.add(dvd); dvd = new FeatDVD(5, "Spiderman", 2, 12.99f, "Action"); inventory.add(dvd); dvd = new FeatDVD(6, "Star Trek", 7, 15.99f, "Drama"); inventory.add(dvd); dvd = new FeatDVD(7, "Bruce Almighty", 10, 11.99f, "Comedy"); inventory.add(dvd); inventory.display(); GUI gui = new GUI(inventory); // Start the GUI } // end main } // end class Inventory4 /**** Class decribes DVD while demostrating polymorphism and inheritance**/ class DVD implements Comparable { private int dvditem; private String dvdtitle; private int dvdstock; private double dvdprice; // Constructor DVD() { dvditem = 0; dvdtitle = ""; dvdstock = 0; dvdprice = 0; }// end constructor //constructor initializes variables DVD(int item, String title, int stock, double price) { this.dvditem = item; this.dvdtitle = title; this.dvdstock = stock; this.dvdprice = price; } private void setTitle(String title) { this.dvdtitle = title; } public String getdvdTitle() { return dvdtitle; } private void setdvdItem(int item) { this.dvditem = item; } public int getdvdItem() { return dvditem; } private void setdvdStock(int stock) { this.dvdstock = stock; } public int getdvdStock() { return dvdstock; } private void setdvdPrice (double price) { this.dvdprice = price; } public double getdvdPrice() { return dvdprice; } public double getValue() { double value = dvdstock * dvdprice; return value; } // This method tells the sort method what is to be sorted public int compareTo(Object o) { return dvdtitle.compareTo(((DVD) o).getdvdTitle()); } // This method passes the format for the string public String toString() { return String.format("Unit number:%2d %-15s Units:%2d Price: $%5.2f Movie value: $%6.2f", dvditem, dvdtitle, dvdstock, dvdprice, getValue()); } } // end class DVD /**** This is a subclass that adds 5% restocking fee and new feature***/ class FeatDVD extends DVD { private String genres; // class constructor FeatDVD(int item, String title, int stock, float price, String genres) { super(item, title, stock, price); this.genres = genres; } public double getValue() { // getvalue method overrides // getvalue method in the superclass double value = 1.05F * super.getValue(); return value; } // end getValue method public String toString() { // toString method overrides the superclass toString method // adding another fields return super.toString() + " Genre:" + genres; } // end toString method } // end class FeatDVD /*****class has inventory of DVDs. * This class has methods to add and display dvds****/ class Inventory { private DVD[] dvds; private int nCount; // constructor Inventory() { dvds = new DVD[10]; nCount = 0; } public void add(DVD dvd) { dvds[nCount] = dvd; ++nCount; sort(); } public int getNcount() { return nCount; } // method calculates total value of inventory public double getTotalValue() { double totalValue = 0; for (int i = 0; i < nCount; i++) totalValue += dvds[i].getValue(); return totalValue; } // end getTotalValue public DVD getDVD(int n) //use in GUI { // protects n and keep in range if (n <0 ) n = 0; else if (n >= nCount) n = nCount - 1; return dvds[n]; } // sorts the DVDs private void sort() { Arrays.sort(dvds, 0, nCount); }// end sort method public void display() { System.out.println("\nThe inventory contains " + nCount + "DVDs\n"); for (int i = 0; i < nCount; i++) System.out.printf("%d: %s\n", i, dvds[i]); System.out.printf("\nTotal value of the inventory is $%.2f\n\n", getTotalValue()); } // end display method } // end class Inventory
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-13-2008, 09:16 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
class FirstButtonHandler implements ActionListener { public void actionPerformed(ActionEvent event) { nCurrentDVDnum = 0; updateFields(); // else // jbFirst.setEnabled(false); } } class PreviousButtonHandler implements ActionListener { public void actionPerformed(ActionEvent event) { --nCurrentDVDnum; if(nCurrentDVDnum < 0) nCurrentDVDnum = inventory.getNcount()-1; updateFields(); // else // jbPrevious.setEnabled(false); } } class NextButtonHandler implements ActionListener { public void actionPerformed(ActionEvent event) { ++nCurrentDVDnum; if(nCurrentDVDnum > inventory.getNcount()-1) nCurrentDVDnum = 0; updateFields(); //else //jbNext.setEnabled(false); } } class LastButtonHandler implements ActionListener { public void actionPerformed(ActionEvent event) { nCurrentDVDnum = inventory.getNcount()-1; updateFields(); // else // jbLast.setEnabled(false); } }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-13-2008, 10:29 AM
Member
 
Join Date: Jan 2008
Posts: 2
dejavujr is on a distinguished road
helooo.....
i'm from indonesia and before continue i sory if my english language is very bad ..... i want to ask how to compile 2 public class ??? thank before .....
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-13-2008, 05:54 PM
Member
 
Join Date: Jan 2008
Posts: 4
glenrowan is on a distinguished road
Thank you hardwired!! It sure seems like there are so many different ways to accomplish things through Java. I was looking through examples and couldn't apply it to what I was doing. Makes sense after seeing the way you posted it though. Now I can focus on cleaning up my GUI and the way it "looks". Thanks again.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 02:23 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org