Results 1 to 15 of 15
  1. #1
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Exclamation Looking for some help.

    Java Code:
    import java.awt.*;              
    import java.awt.event.*;      
    import javax.swing.*;                 
    
    public class Inventory5 {
    
       
        public static void main(String[] args) {
    
    
            Movie dvd = null;
            Inventory inventory = new Inventory();        
    
            dvd = new Movie(15035, "The Davinci Code", 18, 19.99f, 2001);
            System.out.println(dvd);
            inventory.addMovie(dvd);                    
            
            dvd = new Movie(24478, "Fight Club", 14, 20.99f, 1998);
            System.out.println(dvd);
            inventory.addMovie(dvd);                    
            
            dvd = new Movie(32059, "Ladder 49", 1, 21.99f, 1995);
            System.out.println(dvd);
            inventory.addMovie(dvd);                    
    
            dvd = new Movie(35989, "National Treasure", 15, 18.99f, 2003);
            inventory.addMovie(dvd);                    
            System.out.println(dvd);
    
            dvd = new Movie(14418, "Facing the Giants", 10, 21.99f, 2006);
            System.out.println(dvd);
            inventory.addMovie(dvd);                    
            
            dvd = new Movie(13679, "Crimson Tide", 9, 7.99f, 2005);
            System.out.println(dvd);
            System.out.println("\nEnd of DVD collection!\n");
            inventory.addMovie(dvd);                   
    
            inventory.printInventory();
            new InventoryGUI(inventory);                          
        } 
    
    } 
    
    
    class DVD {
        private int itemNo;
        private String title;
        private int inStock;
        private float unitPrice;
    
        DVD(int itemNo, String title, int inStock, float unitPrice) {
            this.itemNo    = itemNo;
            this.title     = title;
            this.inStock   = inStock;
            this.unitPrice = unitPrice;
        }
    
        public int getItemNo()      { return itemNo; }
        public String getTitle()    { return title; }
        public int getInStock()     { return inStock; }
        public float getUnitPrice() { return unitPrice; }
    
        public float value() {
            return inStock * unitPrice;
        }
    
        public String toString() {
            return String.format("ItemNo=%2d   Title=%-22s   InStock=%3d   Price=$%7.2f   Value=$%8.2f",
                                  itemNo, title, inStock, unitPrice, value());
        }
    
    } 
    
    
    class Inventory {
       
        private final int INVENTORY_SIZE = 30;
        private DVD[] items;
        private int numItems;
         
        Inventory() {
            items = new Movie[INVENTORY_SIZE];
            numItems = 0;
        }
    
        public int getNumItems() {
            return numItems;
        }
    
        public DVD getDVD(int n) {
            return items[n];
        }
    
        
        public void addMovie(DVD item) {
            items[numItems] = item;    
            ++numItems;
        }
    
    
        public double value() {
            double sumOfInventory = 0.0;
    
            for (int i = 0; i < numItems; i++)
                sumOfInventory += items[i].value();
                            
            return sumOfInventory;
        }
    
        
        public void printInventory() {
            System.out.println("\nAllen's DVD Inventory\n");
           
            
            if (numItems <= 0) {
                System.out.println("Inventory is empty at the moment.\n");
            } else {
                for (int i = 0; i < numItems; i++)
                    System.out.printf("%3d   %s\n", i, items[i]);
                System.out.printf("\nTotal value in inventory is $%,.2f\n\n", value());
            }
        }
        
    } 
    
    
    
    class Movie extends DVD {
        
        private int movieYear;
    
        
        public Movie(int MovieID, String itemName, int quantityOnHand, float itemPrice, int year) {
            super(MovieID, itemName, quantityOnHand, itemPrice);    
           
            this.movieYear = movieYear;        
        }
    
       
        public void setYear(int year) {
            movieYear = year;
        }
    
        
        public int getMovieYear() {
            return movieYear;
        }
    
        
         public float value() {
            return super.value() + restockingFee();
        } 
    
        
        public float restockingFee() {
            return super.value() * 0.05f;
        }
    
    } 
    
    
    
    class InventoryGUI extends JFrame 
    {
        
        private Inventory theInventory; 
        
         
        private int index = 0; 
        
         
        private final JLabel itemNumberLabel = new JLabel("  Item Number:"); 
        private JTextField itemNumberText; 
        
        private final JLabel prodnameLabel = new JLabel("  Product Name:"); 
        private JTextField prodnameText; 
        
        private final JLabel prodpriceLabel = new JLabel("  Price:"); 
        private JTextField prodpriceText; 
        
        private final JLabel numinstockLabel = new JLabel("  Number in Stock:"); 
        private JTextField numinstockText; 
        
        private final JLabel valueLabel = new JLabel("  Value:"); 
        private JTextField valueText; 
        
        private final JLabel restockingFeeLabel = new JLabel("  Restocking Fee:"); 
        private JTextField restockingFeeText; 
        
        private final JLabel totalValueLabel = new JLabel("  Inventory Total Value:"); 
        private JTextField totalValueText; 
    
        private JPanel centerPanel;
        private JPanel buttonPanel;
    
    
    
    
         
        InventoryGUI(Inventory inventory) {
            super("Allen's Movie Inventory");
            final Dimension dim = new Dimension(140, 20);
            final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
            JPanel jp;
    
             
            theInventory = inventory;        
            
          
            
            
            centerPanel = new JPanel(); 
            centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
    
            buttonPanel = new JPanel();
            JButton nextButton = new JButton("Next");    
            nextButton.addActionListener(new NextButtonHandler());
            buttonPanel.add(nextButton); 
            centerPanel.add(buttonPanel);
            
            buttonPanel = new JPanel();
            JButton previousButton = new JButton("Previous");    
            previousButton.addActionListener(new PreviousHandler());
            buttonPanel.add(previousButton); 
            centerPanel.add(buttonPanel);
            
            buttonPanel = new JPanel();
            JButton firstButton = new JButton("First");    
            firstButton.addActionListener(new FirstHandler());
            buttonPanel.add(firstButton); 
            centerPanel.add(buttonPanel);
            
            buttonPanel = new JPanel();
            JButton lastButton = new JButton("Last");    
            lastButton.addActionListener(new LastHandler());
            buttonPanel.add(lastButton); 
            centerPanel.add(buttonPanel);
    
            jp = new JPanel(flo);
            itemNumberLabel.setPreferredSize(dim);
            jp.add(itemNumberLabel); 
            itemNumberText = new JTextField(10); 
            itemNumberText.setEditable(false); 
            jp.add(itemNumberText); 
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            prodnameLabel.setPreferredSize(dim);
            jp.add(prodnameLabel); 
            prodnameText = new JTextField(10); 
            prodnameText.setEditable(false); 
            jp.add(prodnameText); 
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            prodpriceLabel.setPreferredSize(dim);
            jp.add(prodpriceLabel); 
            prodpriceText = new JTextField(10); 
            prodpriceText.setEditable(false); 
            jp.add(prodpriceText); 
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            numinstockLabel.setPreferredSize(dim);
            jp.add(numinstockLabel); 
            numinstockText = new JTextField(10); 
            numinstockText.setEditable(false); 
            jp.add(numinstockText);  
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            restockingFeeLabel.setPreferredSize(dim);
            jp.add(restockingFeeLabel); 
            restockingFeeText = new JTextField(10); 
            restockingFeeText.setEditable(false); 
            jp.add(restockingFeeText); 
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            valueLabel.setPreferredSize(dim);
            jp.add(valueLabel); 
            valueText = new JTextField(10); 
            valueText.setEditable(false); 
            jp.add(valueText); 
            centerPanel.add(jp);
            
            
            jp = new JPanel(flo);
            totalValueLabel.setPreferredSize(dim);
            jp.add(totalValueLabel); 
            totalValueText = new JTextField(10);
            totalValueText.setEditable(false);
            jp.add(totalValueText); 
            centerPanel.add(jp);
    
            
            setContentPane(centerPanel);
    
            repaintGUI();
    
            setDefaultCloseOperation(EXIT_ON_CLOSE); 
            setSize(360, 450); 
            setResizable(false); 
            setLocationRelativeTo(null); 
            setVisible(true); 
        } 
    
         
        public void repaintGUI() { 
            Movie temp = (Movie) theInventory.getDVD(index); 
            
            if (temp != null) { 
                itemNumberText.setText("" + temp.getItemNo());     
                prodnameText.setText(temp.getTitle()); 
                prodpriceText.setText(String.format("$%.2f", temp.getUnitPrice())); 
                restockingFeeText.setText(String.format("$%.2f", temp.restockingFee())); 
                numinstockText.setText("" + temp.getInStock()); 
                valueText.setText(String.format("$%.2f", temp.value())); 
            }
            totalValueText.setText(String.format("$%.2f", theInventory.value())); 
        }
    
        class NextButtonHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (++index) % numItems;
                repaintGUI();
            }
        }
       class PreviousHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (--index) % numItems;
                repaintGUI();
            }
         }
         class FirstHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (index) % numItems;
                repaintGUI();
            }
         }   
       
         class LastHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (index) % numItems;
                repaintGUI();
            }
         }
         
     
    
    }

    How do I Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item should display.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    You have lots of questions here. Seems to me, first of all you have add an button. Did you do that? Later you have to think about the functionality of the button.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    The program appears to have buttons and listeners.
    If you get errors, copy and paste them here with your questions.

  4. #4
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    The program appears to have buttons and listeners.
    If you get errors, copy and paste them here with your questions.
    I'm not getting any errors, but I don't know how to work with the first,last, and previous buttons. Any help will be grateful.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    how to work with the first,last, and previous buttons
    First thing then is put in some println() statements to see if the listener code is being executed.
    When you see that it is, then design what you want to happen when a button is pressed. Then put that code in the program.

    For example: what should happen when the first button is pressed? Describe that and think what the program will have to do to do it.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #7
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    Ok I have work that out now. thanks but I have one more, how do you includes a company logo.?? Anything will help...Thanks

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
            JLabel title = new JLabel("title name");
            add(title, "North");
            add(centerPanel); // no constraint -> center section
    
            repaintGUI();
    
            setDefaultCloseOperation(EXIT_ON_CLOSE); 
            setSize(360, 450); 
            setResizable(false); 
            setLocationRelativeTo(null); 
            setVisible(true);

  9. #9
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    I'm getting some errors with this.

    Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself
    at java.awt.Container.addImpl(Container.java:1017)
    at java.awt.Container.add(Container.java:935)
    at javax.swing.JFrame.addImpl(JFrame.java:545)
    at java.awt.Container.add(Container.java:352)
    at javaapplication52.InventoryGUI.<init>(Week8.java:3 16)
    at javaapplication52.Week8.main(Week8.java:47)

    Here is my code....
    Java Code:
    import java.awt.*;              
    import java.awt.event.*;      
    import javax.swing.*;                 
    
    
    
    public class Week8 {
    
       
        public static void main(String[] args) {
    
    
            Movie dvd = null;
            Inventory inventory = new Inventory();        
    
            dvd = new Movie(15035, "The Davinci Code", 18, 19.99f, 2001);
            System.out.println(dvd);
            inventory.addMovie(dvd);                    
            
            dvd = new Movie(24478, "Fight Club", 14, 20.99f, 1998);
            System.out.println(dvd);
            inventory.addMovie(dvd);                    
            
            dvd = new Movie(32059, "Ladder 49", 1, 21.99f, 1995);
            System.out.println(dvd);
            inventory.addMovie(dvd);                    
    
            dvd = new Movie(35989, "National Treasure", 15, 18.99f, 2003);
            inventory.addMovie(dvd);                    
            System.out.println(dvd);
    
            dvd = new Movie(14418, "Facing the Giants", 10, 21.99f, 2006);
            System.out.println(dvd);
            inventory.addMovie(dvd);                    
            
            dvd = new Movie(13679, "Crimson Tide", 9, 7.99f, 2005);
            System.out.println(dvd);
            System.out.println("\nEnd of DVD collection!\n");
            inventory.addMovie(dvd);                   
    
            inventory.printInventory();
            new InventoryGUI(inventory);                          
        } 
    
    } 
    class DVD {
        private int itemNo;
        private String title;
        private int inStock;
        private float unitPrice;
    
        DVD(int itemNo, String title, int inStock, float unitPrice) {
            this.itemNo    = itemNo;
            this.title     = title;
            this.inStock   = inStock;
            this.unitPrice = unitPrice;
        }
    
        public int getItemNo()      { return itemNo; }
        public String getTitle()    { return title; }
        public int getInStock()     { return inStock; }
        public float getUnitPrice() { return unitPrice; }
    
        public float value() {
            return inStock * unitPrice;
        }
    
        public String toString() {
            return String.format("ItemNo=%2d   Title=%-22s   InStock=%3d   Price=$%7.2f   Value=$%8.2f",
                                  itemNo, title, inStock, unitPrice, value());
        }
    
    } 
    
    
    class Inventory {
       
        private final int INVENTORY_SIZE = 30;
        private DVD[] items;
        private int numItems;
         
        Inventory() {
            items = new Movie[INVENTORY_SIZE];
            numItems = 0;
        }
    
        public int getNumItems() {
            return numItems;
        }
    
        public DVD getDVD(int n) {
            return items[n];
        }
    
        
        public void addMovie(DVD item) {
            items[numItems] = item;    
            ++numItems;
        }
    
    
        public double value() {
            double sumOfInventory = 0.0;
    
            for (int i = 0; i < numItems; i++)
                sumOfInventory += items[i].value();
                            
            return sumOfInventory;
        }
    
        
        public void printInventory() {
            System.out.println("\n DVD Inventory\n");
           
            
            if (numItems <= 0) {
                System.out.println("Inventory is empty at the moment.\n");
            } else {
                for (int i = 0; i < numItems; i++)
                    System.out.printf("%3d   %s\n", i, items[i]);
                System.out.printf("\nTotal value in inventory is $%,.2f\n\n", value());
            }
        }
        
    } 
    
    
    
    class Movie extends DVD {
        
        private int movieYear;
    
        
        public Movie(int MovieID, String itemName, int quantityOnHand, float itemPrice, int year) {
            super(MovieID, itemName, quantityOnHand, itemPrice);    
           
            this.movieYear = movieYear;        
        }
    
       
        public void setYear(int year) {
            movieYear = year;
        }
    
        
        public int getMovieYear() {
            return movieYear;
        }
    
        
         public float value() {
            return super.value() + restockingFee();
        } 
    
        
        public float restockingFee() {
            return super.value() * 0.05f;
        }
    
    } 
    
    
    
    
    class InventoryGUI extends JFrame 
    {
       
        
        private Inventory theInventory; 
        
         
        private int index = 0; 
        
         
        private final JLabel itemNumberLabel = new JLabel("  Item Number:"); 
        private JTextField itemNumberText; 
        
        private final JLabel prodnameLabel = new JLabel("  Product Name:"); 
        private JTextField prodnameText; 
        
        private final JLabel prodpriceLabel = new JLabel("  Price:"); 
        private JTextField prodpriceText; 
        
        private final JLabel numinstockLabel = new JLabel("  Number in Stock:"); 
        private JTextField numinstockText; 
        
        private final JLabel valueLabel = new JLabel("  Value:"); 
        private JTextField valueText; 
        
        private final JLabel restockingFeeLabel = new JLabel("  Restocking Fee:"); 
        private JTextField restockingFeeText; 
        
        private final JLabel totalValueLabel = new JLabel("  Inventory Total Value:"); 
        private JTextField totalValueText; 
    
        private JPanel centerPanel;
        private JPanel buttonPanel;
    
    
    
    
         
        InventoryGUI(Inventory inventory) {
            super(" Movie Inventory");
            final Dimension dim = new Dimension(140, 20);
            final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
            JPanel jp;
    
             
            theInventory = inventory;        
            
            
            
            centerPanel = new JPanel(); 
            centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
    
            buttonPanel = new JPanel();
            JButton nextButton = new JButton("Next");    
            nextButton.addActionListener(new NextButtonHandler());
            buttonPanel.add(nextButton); 
            centerPanel.add(buttonPanel);
            
            buttonPanel = new JPanel();
            JButton previousButton = new JButton("Previous");    
            previousButton.addActionListener(new PreviousHandler());
            buttonPanel.add(previousButton); 
            centerPanel.add(buttonPanel);
            
            buttonPanel = new JPanel();
            JButton firstButton = new JButton("First");    
            firstButton.addActionListener(new FirstHandler());
            buttonPanel.add(firstButton); 
            centerPanel.add(buttonPanel);
            
            buttonPanel = new JPanel();
            JButton lastButton = new JButton("Last");    
            lastButton.addActionListener(new LastHandler());
            buttonPanel.add(lastButton); 
            centerPanel.add(buttonPanel);
    
            jp = new JPanel(flo);
            itemNumberLabel.setPreferredSize(dim);
            jp.add(itemNumberLabel); 
            itemNumberText = new JTextField(10); 
            itemNumberText.setEditable(false); 
            jp.add(itemNumberText); 
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            prodnameLabel.setPreferredSize(dim);
            jp.add(prodnameLabel); 
            prodnameText = new JTextField(10); 
            prodnameText.setEditable(false); 
            jp.add(prodnameText); 
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            prodpriceLabel.setPreferredSize(dim);
            jp.add(prodpriceLabel); 
            prodpriceText = new JTextField(10); 
            prodpriceText.setEditable(false); 
            jp.add(prodpriceText); 
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            numinstockLabel.setPreferredSize(dim);
            jp.add(numinstockLabel); 
            numinstockText = new JTextField(10); 
            numinstockText.setEditable(false); 
            jp.add(numinstockText);  
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            restockingFeeLabel.setPreferredSize(dim);
            jp.add(restockingFeeLabel); 
            restockingFeeText = new JTextField(10); 
            restockingFeeText.setEditable(false); 
            jp.add(restockingFeeText); 
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            valueLabel.setPreferredSize(dim);
            jp.add(valueLabel); 
            valueText = new JTextField(10); 
            valueText.setEditable(false); 
            jp.add(valueText); 
            centerPanel.add(jp);
            
            
            jp = new JPanel(flo);
            totalValueLabel.setPreferredSize(dim);
            jp.add(totalValueLabel); 
            totalValueText = new JTextField(10);
            totalValueText.setEditable(false);
            jp.add(totalValueText); 
            centerPanel.add(jp);
    
            
            setContentPane(centerPanel);
    
            repaintGUI();
    
            setDefaultCloseOperation(EXIT_ON_CLOSE); 
            setSize(360, 450); 
            setResizable(false); 
            setLocationRelativeTo(null); 
            setVisible(true); 
        
            JLabel title = new JLabel("title name");
            add(title, "North");
            add(centerPanel); // no constraint -> center section
    
            repaintGUI();
    
            setDefaultCloseOperation(EXIT_ON_CLOSE); 
            setSize(360, 450); 
            setResizable(false); 
            setLocationRelativeTo(null); 
            setVisible(true);
        } 
    
         
        public void repaintGUI() { 
            Movie temp = (Movie) theInventory.getDVD(index); 
            
            if (temp != null) { 
                itemNumberText.setText("" + temp.getItemNo());     
                prodnameText.setText(temp.getTitle()); 
                prodpriceText.setText(String.format("$%.2f", temp.getUnitPrice())); 
                restockingFeeText.setText(String.format("$%.2f", temp.restockingFee())); 
                numinstockText.setText("" + temp.getInStock()); 
                valueText.setText(String.format("$%.2f", temp.value())); 
            }
            totalValueText.setText(String.format("$%.2f", theInventory.value())); 
        }
    
        class NextButtonHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (++index) % numItems;
                repaintGUI();
            }
        }
       class PreviousHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (index-1) % numItems;
                repaintGUI();  
            }
         }
         class FirstHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (index=0) % numItems;
                repaintGUI();
            }
         }   
       
         class LastHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (index=5) % numItems;
                repaintGUI();
            }
         }
         
    }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    at javaapplication52.InventoryGUI.<init>(Week8.java:3 16)
    Look at the source at line 316 in the InventoryGUI constructor.

    Its very hard to see where that line is from your posting.

  11. #11
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    JLabel title = new JLabel("title name");
    add(title, "North");
    add(centerPanel);// this is line 316

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    adding container's parent to itself
    How is centerPanel related to the container it is being added to?

  13. #13
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Looks like you did copy and paste without thinking about what changes you wanted/should make.
    IllegalArgumentException: adding container's parent to itself
    You've already set "centerPanel" as the contentPane:
    Java Code:
            setContentPane(centerPanel);
    Then you come along and try to add "centerPanel" to the center section of the contentPane (which is "centerPanel"):
    Java Code:
            JLabel title = new JLabel("title name");
            add(title, "North");
            add(centerPanel); // no constraint -> center section
    So change this
    Java Code:
            ...
            jp = new JPanel(flo);
            totalValueLabel.setPreferredSize(dim);
            jp.add(totalValueLabel); 
            totalValueText = new JTextField(10);
            totalValueText.setEditable(false);
            jp.add(totalValueText); 
            centerPanel.add(jp);
    
            
            setContentPane(centerPanel);
    
            repaintGUI();
    
            setDefaultCloseOperation(EXIT_ON_CLOSE); 
            setSize(360, 450); 
            setResizable(false); 
            setLocationRelativeTo(null); 
            setVisible(true); 
        
            JLabel title = new JLabel("title name");
            add(title, "North");
            add(centerPanel); // no constraint -> center section
    
            repaintGUI();
    
            setDefaultCloseOperation(EXIT_ON_CLOSE); 
            setSize(360, 450); 
            setResizable(false); 
            setLocationRelativeTo(null); 
            setVisible(true);
        }
    to this
    Java Code:
            ...
            jp = new JPanel(flo);
            totalValueLabel.setPreferredSize(dim);
            jp.add(totalValueLabel); 
            totalValueText = new JTextField(10);
            totalValueText.setEditable(false);
            jp.add(totalValueText); 
            centerPanel.add(jp);
    
            JLabel title = new JLabel("title name");
            add(title, "North");
            add(centerPanel); // no constraint -> center section
    
            repaintGUI();
    
            setDefaultCloseOperation(EXIT_ON_CLOSE); 
            setSize(360, 450); 
            setResizable(false); 
            setLocationRelativeTo(null); 
            setVisible(true);
        }

  14. #14
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    Ok I got it. Thanks

    Last thing. How do I get this to work? It needs to go back and keep going back. But if I get the the 1st on I get an error.

    Java Code:
     class PreviousHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (index-1) % numItems;
                repaintGUI();  
            }
         }

  15. #15
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
            int x = 4;
            int countUp = 0;
            int countDown = x;
            for(int i = 0; i < 6; i++) {
                countUp = (countUp+1) % x;
                countDown = countDown -1;
                if(countDown < 0)
                    countDown = x;
                System.out.printf("countUp = %d  countDown = %d%n",
                                   countUp, countDown);
            }

Posting Permissions

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