Results 1 to 5 of 5
  1. #1
    dandapice is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Populating a jTextField with data from another class

    Hello,
    First off, I am a blundering novice, so show mercy...I need to populate the jTextFields of my GUI as follows:

    Item Number: should be ItemNumber from my Book class
    Item Name: should be ItemName from my Book class
    In Stock: should be InStock from Book class
    Price Each: should be PriceEach from Book class

    Do I need to somehow import my array list? Am I using set/get methods for each jTextField? I used the Netbeans 7.2 GUI builder which did most of the GUI coding for me. There are "// TODO add your handling code here:" comments for each jTextField. My code (sorry for the length) is as follows:
    Java Code:
    package inventory;
    
    /**
     * Book Class
     * @author Dan
     * Modified 10/8/2012
     */
    public class Book implements Comparable{
        /* Define variables */
        
        protected String ItemNumber;
        protected String ItemName;
        protected Integer InStock;
        protected double PriceEach;
        
        /* Parameterized Constructor */
        public Book(String ItemNumber, String ItemName, Integer InStock, double PriceEach)
        {
            this.ItemNumber = ItemNumber;
            this.ItemName = ItemName;
            this.InStock = InStock;
            this.PriceEach = PriceEach;
    
        }
        
        /* Calculate value of stock */
        public double getTotal()
        {
            return this.PriceEach * this.InStock;
        }
        /* Define Set and Get functions */
        public void setItemNumber(String itemNumber)
        {
            ItemNumber = ItemNumber;
        }
        public String getItemNumber()
        {
            return ItemNumber;
        }
        public void setItemName(String itemName)
        {
            ItemName = ItemName;
        }
        public String getItemName()
        {
            return ItemName;
        }
        public void setInStock(Integer inStock)
        {
            InStock = InStock;
        }
        public Integer getInStock()
        {
            return InStock;
        }
        public void setPriceEach(double priceEach)
        {
            PriceEach = PriceEach;
        }
        public double getPriceEach()
        {
            return PriceEach;
        }
        
        /* Sort array by name? */
        @Override
        public int compareTo(Object o)
        {
            Book inputParameter = (Book)o;
            return ItemName.compareTo(inputParameter.ItemName);
        }
        /* Override otString to display array data */
        @Override
        public String toString()
        {
            return String.format(ItemNumber + "\t" + ItemName + "\t" + InStock + "\t\t" + PriceEach);
        }
    } /* End class */
    Java Code:
    package inventory;
    import java.util.ArrayList;
    /**
     * Inventory Class
     * @author Dan
     * Modified 10/8/2012
     */
    public class Inventory {
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {    
            
            /* Create and display GUI */        
            new InventoryGUI().setVisible(true);
            
            /* Create new variable for inventory calculation */
            double Total = 0.0;
            
            /* Create new ArrayList */
            ArrayList<Book> BookList;
            BookList = new ArrayList<>();
            ArrayList<TrainingBook> TrainList;
            TrainList = new ArrayList<>();
            
            /* Add elements to ArrayList */
            BookList.add(new Book("001a", "Java 101", 50, 19.99) );
            BookList.add(new Book("001b", "C# 101\t", 50, 19.95) );
            BookList.add(new Book("001c", "Python 101", 25, 25.99) );
            TrainList.add(new TrainingBook("001a", "Java 101", 50, 19.99, "Training"));
            TrainList.add(new TrainingBook("001b", "C# 101\t", 50, 19.95, "Training"));
            TrainList.add(new TrainingBook("001c", "Python 101", 25, 25.99, "Training"));
            
            /* Print out data from elements in ArrayList */
            System.out.println("Item#\tBook Name\tIn Stock\tPrice Each\tItem Class\tRestock Price*");
            System.out.println(TrainList.get(0) );        
            System.out.println(TrainList.get(1) );
            System.out.println(TrainList.get(2) );
            
            /* Calculate total inventory value */
            for (Book List : BookList)
            {
                Total += List.getTotal();
            }
            /* Display total inventory value */
            System.out.printf("\nThe total value of the inventory is: $%,.2f\n", Total);
            System.out.println();
            System.out.println("* Restock Price reflects a 5 percent restocking fee for entire stock of each specific item.");
            System.out.println();
        } /* End main */
    } /* End class */
    Java Code:
    package inventory;
    /**
     * Inventory GUI
     * @author Dan
     * Created 10/8/2012
     */
    public class InventoryGUI extends javax.swing.JFrame {
        
        /**
         * Creates new form InventoryGUI
         */
        public InventoryGUI() {
            initComponents();
            
        }
    
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        
       /* <<<<< GENERATED CODE DELETED FOR LENGTH >>>>> */                          
        
        private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
        }                                          
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            System.exit(0);
        }                                        
    
        private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
        }                                           
    
        private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {                                            
           // TODO add your handling code here:
        }                                           
    
        private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
        }                                           
    
        private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
        }                                           
    
        private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
        }                                           
    
        private void jTextField6ActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
        }                                           
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            
            /* Set the Nimbus look and feel */
    
            /* <<<<< LOOK AND FEEL SETTING CODE DELETED FOR LENGTH >>>>>*/
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new InventoryGUI().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        private javax.swing.JLabel jLabel7;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        private javax.swing.JTextField jTextField3;
        private javax.swing.JTextField jTextField4;
        private javax.swing.JTextField jTextField5;
        private javax.swing.JTextField jTextField6;
        // End of variables declaration                   
    
    }

    Any help would be greatly appreciated as I am completely lost...

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,834
    Rep Power
    8

    Default Re: Populating a jTextField with data from another class

    Most people here will tell you to ditch the GUI builder until you really know what's going on behind the scenes. Not only does it make your code really hard to read, it also probably isn't the best way to create the gui you need (sounds like you want a set of JTextFields for each Book Object?).
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    dandapice is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Populating a jTextField with data from another class

    Hey KevinWorkman,
    I actually like the GUI builder because I don't know what I'm doing. If I had to code all the details myself, I'd have never gotten as far as I did. This is actually an assignment due Sunday and so far I can get the GUI to run and exit via the X in the corner and the EXIT button. I am not really concerned with the JComboBox right now, I just want to populate the jTextFields. I tried setting the text ( jTextField1.setText(ItemNumber()); etc. but it errors out. Obviously I am missing something. So, people really advise against the GUI builder here, eh? Must be me...LOL

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,834
    Rep Power
    8

    Default Re: Populating a jTextField with data from another class

    That's a common misconception of novices- the gui builder seems like it helps you work faster, but you've "programmed" yourself into a corner here by doing it that way. It's much better to use the Swing tutorials and do it by hand, for the reasons I've already laid out. The fact that nobody else has commented here should be an indicator of what people think of gui-builder code.

    I suggest boiling your code down to an SSCCE and posting the exact error that it caused, and we'll go from there.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    dandapice is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Populating a jTextField with data from another class

    KevinWorkman,
    Well, that explains the lack of comments. Looks like I'm going back to the drawing board. Thanks again...

Similar Threads

  1. JTextField on JPanel-class within JFrame-class
    By floris in forum AWT / Swing
    Replies: 5
    Last Post: 06-25-2012, 12:54 PM
  2. Replies: 2
    Last Post: 08-09-2011, 07:52 PM
  3. Replies: 12
    Last Post: 06-02-2011, 06:54 PM
  4. Need help: Populating data
    By rahul202 in forum Java Servlet
    Replies: 0
    Last Post: 10-25-2010, 07:16 PM
  5. Error when populating the data
    By srinivas_k543 in forum Java Servlet
    Replies: 2
    Last Post: 07-10-2009, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •