Welcome to Dream.In.Code
Become a Java Expert!

Join 149,602 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,899 people online right now. Registration is fast and FREE... Join Now!




Need Help with JButtons Functionality

 
Reply to this topicStart new topic

Need Help with JButtons Functionality

mariejt
11 Oct, 2007 - 07:01 AM
Post #1

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 7


My Contributions
Hello Everyone,

I feel like I'm going around in circles! I can't figure out why by JButtons won't perform their assigned functionality. I have a first, previous, next, last, and save button. I've created actionListeners and actionEvents for all butons, but they still produce nothing. Any insight is extremely appreciated!!!!

The file complies, but the Jbuttons don't do anything......

CODE
import java.awt.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JLabel;

public class Inventory_Program6a extends JFrame
{
    // instance variables
    static int displayBooks = 0; // variable for actionEvents

    private JButton firstButton;
    private JButton previousButton;
    private JButton nextButton;
    private JButton lastButton;
    private JButton addButton;
    private JButton deleteButton;
    private JButton modifyButton;
    private JButton saveButton;
    private JButton searchButton;

    private JTextField bookTitleField;
    private JTextField itemNumberField;
    private JTextField unitsInStockField;
    private JTextField bookPriceField;
    private JTextField inventoryValueField;
    private JTextField bookAuthorField;
    private JTextField restockingFeeField;

    private JLabel bookTitleLabel;
    private JLabel itemNumberLabel;
    private JLabel unitsInStockLabel;
    private JLabel bookPriceLabel;
    private JLabel inventoryValueLabel;
    private JLabel bookAuthorLabel;
    private JLabel restockingFeeLabel;

    private JPanel display;
    private JPanel content;
    private JTextArea textArea;
    private JLabel label;

    // Declare an array of classes
    
    private Book2 myBook[];

    // method to sort inventory by book title
    public Book2[] sortBookInventory()
        {
            for(int i = 0; i < myBook.length; i++)
        {
                String bookTitle1 = myBook[i].getBookTitle();
                int min = i;
                String bookTitle = bookTitle1;
                    for(int j = i+1; j < myBook.length; j++)
            {
                    String bookTitle2 = myBook[j].getBookTitle();
                        if(bookTitle2.compareTo(bookTitle) < 0)
                {
                            min = j;
                            bookTitle = bookTitle2;
                        }
                    }
                if(!bookTitle.equals(bookTitle1))
        {
                    Book2 temp = myBook[i];
                    myBook[i] = myBook[min];
                    myBook[min] = temp;
               }
            }
                return myBook;
    } // end method sortBookInventory

    // constructor
    public Inventory_Program6a()
    {        
        double totalInventoryValue = 0.0;

        // Specify how many items are in the array
        // instantiate Book2 object
        myBook = new Book2[5];
        myBook[0] = new Book2("The Key Triology", 1, 25, 7.99, "Nora Roberts");
        myBook[1] = new Book2("The Shinning", 2, 15, 5.99, "Stephen King");
        myBook[2] = new Book2("Wild Acre", 3, 7, 4.99, "Phillipa Gregory");
        myBook[3] = new Book2("Dark Paradise", 4, 2, 12.99, "Tami Hoag");
        myBook[4] = new Book2("Dollhouse Murders", 5, 18, 2.95, "Betty Ren Wright");
            
        // call method sort book inventory for display
        sortBookInventory();

        for (int i = 0; i<5; i++)
        {

            // display the book title
            System.out.println("The book title is: " + myBook[i].getBookTitle());

            // display the item number
            System.out.println("The item number is: " + myBook[i].getItemNumber());

            // display the number of units in stock
            System.out.println("The number of units in stock is: " + myBook[i].getBookUnits());

            // display the price per book
            System.out.printf("The price of the book is: $%.2f\n", myBook[i].getBookPrice());

            // display the value of the inventory
            System.out.printf("The value of the inventory is: $%.2f\n", myBook[i].inventoryValue());

            // display the book author
            System.out.println("The book author is: " + myBook[i].getBookAuthor());

            // display the restocking fee
            System.out.println("Restock Fee: " + myBook[i].inventoryValue() * 0.05);

            // calculate total inventory value with restocking fee added
            totalInventoryValue += (myBook[i].inventoryValue() * 1.05);

            // insert blank line
            System.out.println();

        } // end for

        // display the total value of the inventory with the restocking fee
        System.out.printf("The total value of the book inventory including the restocking fee is: $%5.2f.\n",         

totalInventoryValue);

        // display the total value of the inventory excluding the restocking fee
        System.out.println("The total value of the book inventory is: " + totalInventoryValue());

        //JLabel constructor for logo
        Icon logo = new ImageIcon("C:/book.jpg");
        label = new JLabel(logo);
        label.setToolTipText("Joyce's Logo");
        
        // Create content panel, set layout
        content = new JPanel();
        content.setLayout(new FlowLayout());
        content.setLayout(new GridLayout(3,4));

        // create JPanel for array items
        display = new JPanel();
        display.setLayout(new FlowLayout());
        display.setLayout(new GridLayout(7,1));

        // create textArea
        textArea = new JTextArea(myBook[3]+"\n");

        // set window (JFrame) characteristics
        setLayout(new BorderLayout());
        getContentPane().add(new JScrollPane(display), BorderLayout.CENTER);
        getContentPane().add(content, BorderLayout.SOUTH);
        getContentPane().add(label, BorderLayout.NORTH);
        pack();
        setTitle("Book Inventory Program");
        setSize(400, 400); // set frame size
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // initialize components    
        bookTitleField = new JTextField();
        bookTitleField.setText(myBook[displayBooks].getBookTitle()+"\n");
        bookTitleField.setEditable(false);

        itemNumberField = new JTextField();
        itemNumberField.setText(myBook[displayBooks].getItemNumber()+"\n");
        itemNumberField.setEditable(false);
    
        unitsInStockField = new JTextField();
        unitsInStockField.setText(myBook[displayBooks].getBookUnits()+"\n");
        unitsInStockField.setEditable(false);

        bookPriceField = new JTextField();
        bookPriceField.setText(myBook[displayBooks].getBookPrice()+"\n");
        bookPriceField.setEditable(false);

        inventoryValueField = new JTextField();
        inventoryValueField.setText(myBook[displayBooks].inventoryValue()+"\n");
        inventoryValueField.setEditable(false);

        bookAuthorField = new JTextField();
        bookAuthorField.setText(myBook[displayBooks].getBookAuthor()+"\n");
        bookAuthorField.setEditable(false);                

        restockingFeeField = new JTextField();
        restockingFeeField.setText(myBook[displayBooks].bookRestockingFee()+"\n");
        restockingFeeField.setEditable(false);    
            
        // initialize components
        firstButton = new JButton("First");
        content.add(firstButton);

        previousButton = new JButton("Previous");
        content.add(previousButton);

        nextButton = new JButton("Next");
        content.add(nextButton);

        lastButton = new JButton("Last");
        content.add(lastButton);

        addButton = new JButton("Add");
        content.add(addButton);

        deleteButton = new JButton("Delete");
        content.add(deleteButton);

        modifyButton = new JButton("Modify");
        content.add(modifyButton);

        saveButton = new JButton("Save");
        content.add(saveButton);

        searchButton = new JButton("Search");
        content.add(searchButton);

        // display JLabels and JTextFields on display panel
        display.add(new JLabel("Book Title: "));
        display.add(bookTitleField);

        display.add(new JLabel("Item Number: "));
        display.add(itemNumberField);

        display.add(new JLabel("Units in Stock: "));
        display.add(unitsInStockField);

        display.add(new JLabel("Book Price: "));
        display.add(bookPriceField);

        display.add(new JLabel("Inventory Value: "));
        display.add(inventoryValueField);
    
        display.add(new JLabel("Book Author: "));
        display.add(bookAuthorField);
    
        display.add(new JLabel("Restocking Fee: "));
        display.add(restockingFeeField);

        toString();

        // assign actionListener and actionEvent to firstButton
        firstButton.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent event)
        {
            if (event.getActionCommand()== "First")
            {
            displayBooks = 0;
            }
        
        
        } // end firstButton actionEvent
        }); // end lastButton actionListener

        // assign actionListener and actionEvent to previousButton
        previousButton.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent event)
        {
        if (event.getActionCommand()== "Previous")
        displayBooks--;
        if (displayBooks < 0)
        {
        displayBooks = 0;
            if (displayBooks == 0)
            {
            displayBooks = displayBooks = myBook.length-1;
            }
        }
        } // end previousButton actionEvent
        }); // end previousBUtton actionListener

        // assign actionListener and actionEvent to nextButton
        nextButton.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent event)
        {
        if (event.getActionCommand()== "Next")
        displayBooks++;
        if (displayBooks >= myBook.length)
        {
        displayBooks = myBook.length-1;
            if (displayBooks == myBook.length-1)
            {
            displayBooks = 0;
            }
        }
        } // end nextButton actionEvent
        }); // end nextButton actionListener

        // assign actionListener and actionEvent to lastButton
        lastButton.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent event)
        {
        if (event.getActionCommand()== "Last")
        {
        displayBooks = myBook.length-1;
        }
        } // end lastButton actionEvent
        }); // end lastButton actionListener


        // assign actionListener and actionEvent to saveButton
        saveButton.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent event)
        {
        if (event.getActionCommand()== "Save")
        {
            FileOutputStream out;
            PrintStream p;
            try
            {
                String strDirectory = "c:/data";
                new File(strDirectory).mkdir();
                out = new FileOutputStream ("C:/data/Inventory_Program6a.dat");
                p = new PrintStream(out);
                    for (int i = 0; i<= 5 - 1; i++)
                    {
                        p.println("Book Title: " + myBook[i].getBookTitle()+"\n");
                        p.println("Item Number: " + myBook[i].getItemNumber()+"\n");
                        p.println("Book Units: " + myBook[i].getBookUnits()+"\n");
                        p.println("Book Price: " + myBook[i].getBookPrice()+"\n");
                        p.println("Inventory Value: " + myBook[i].inventoryValue()+"\n");
                        p.println("Book Restocking Fee: " + myBook[i].bookRestockingFee()+"\n");
                        p.println("");
                    }
                p.close();
                }
            catch (Exception e)
            {
                System.out.println("error");
            }
            }
            toString();
        } // end lastButton actionEvent
        }); // end lastButton actionListener
    } // end constructor

    // method to calculate the value of the entire inventory
    public double totalInventoryValue()
    {
        double totalInventoryValue = 0.00;

        for ( int counter = 0; counter < myBook.length; counter++ )

            totalInventoryValue += myBook[counter].inventoryValue();

        return totalInventoryValue;

    } // end method totalBookValue

    // main method begins execution of Java application
    public static void main( String args [])
    {
        new Inventory_Program6a();
        //create JFrame
        Inventory_Program6a inventory = new Inventory_Program6a();
        inventory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        inventory.setVisible(true);        

    } // end method main

} // end class Inventory_Program6a

User is offlineProfile CardPM
+Quote Post

hyperionza
RE: Need Help With JButtons Functionality
11 Oct, 2007 - 12:46 PM
Post #2

New D.I.C Head
*

Joined: 15 Sep, 2007
Posts: 30


My Contributions
Have you tried creating classes implementing ActionListener for each instead of doing it as you are currently?

Ive never seen it done quite like yours before.

This post has been edited by hyperionza: 11 Oct, 2007 - 12:47 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Need Help With JButtons Functionality
11 Oct, 2007 - 05:07 PM
Post #3

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(hyperionza @ 11 Oct, 2007 - 01:46 PM) *

Have you tried creating classes implementing ActionListener for each instead of doing it as you are currently?

Ive never seen it done quite like yours before.



I don't think that is the problem, you can define entire classes as arguments of functions. In this case the argument is an object whose class is ActionListener, so it's cool.

But the thing is that mariejt only changes the values of some variables and she/he doesn't update those values to the window.


User is offlineProfile CardPM
+Quote Post

mariejt
RE: Need Help With JButtons Functionality
12 Oct, 2007 - 04:55 AM
Post #4

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 7


My Contributions
Good Morning Everyone!

I actually figured this out yesterday late afternoon.

Thanks so much for the reply!
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Need Help With JButtons Functionality
12 Oct, 2007 - 02:05 PM
Post #5

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(mariejt @ 12 Oct, 2007 - 05:55 AM) *

Good Morning Everyone!

I actually figured this out yesterday late afternoon.

Thanks so much for the reply!

It would be nice of you if you post the solution, so that others will see and learn from it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:49PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month