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

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




Need Help with JTextFiles and JPanel

 
Reply to this topicStart new topic

Need Help with JTextFiles and JPanel

mariejt
9 Oct, 2007 - 08:21 AM
Post #1

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 7


My Contributions
Hello everyone,

I am really stuck on my GUI. I need to populate JTextFields with items from my array as well as create JButtons that have the functionality of selecting the first, last, next, and previous items in my array. I was able to successfully do this with a JTextArea, but I now need to use JTextFields.

I think I’m on the right track, but stuck when trying to update the actionListener and actionEvent coding for my JButtons. I am trying to change my code to point to my JPanel rather than the JTextArea, but I am getting error messages when I do this.

For example:
C:\java>javac Inventory_Program6a.java
Inventory_Program6a.java:262: cannot find symbol
symbol : method getText(java.lang.String)
location: class javax.swing.JPanel
display.getText(myBook[displayBooks]+"\n");
^
1 error

“display” is my JPanel and it doesn’t like the commands (getText, addText, or setText)

I hope this makes sense. Thank you so much for any assistance you can provide.

I’ve attached my code:
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

CODE
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[0].getBookTitle()+"\n");
        bookTitleField.setEditable(false);

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

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

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

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

        restockingFeeField = new JTextField();
        restockingFeeField.setText(myBook[0].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);

        
        // assign actionListener and actionEvent to firstButton
        firstButton.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent ae)
        {
        displayBooks = 0;
        
        display.getText(myBook[displayBooks]+"\n");
        } // end firstButton actionEvent
        }); // end lastButton actionListener


        // assign actionListener and actionEvent to previousButton
        previousButton.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent ae)
        {
        displayBooks--;
        if (displayBooks < 0)
        {
        displayBooks = 0;
            if (displayBooks == 0)
            {
            displayBooks = displayBooks = myBook.length-1;
            }
        }
        textArea.setText(myBook[displayBooks]+"\n");
        } // end previousButton actionEvent
        }); // end previousBUtton actionListener



        // assign actionListener and actionEvent to nextButton
        nextButton.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent ae)
        {
        displayBooks++;
        if (displayBooks >= myBook.length)
        {
        displayBooks = myBook.length-1;
            if (displayBooks == myBook.length-1)
            {
            displayBooks = 0;
            }
        }
        textArea.setText(myBook[displayBooks]+"\n");
        } // end nextButton actionEvent
        }); // end nextButton actionListener

        // assign actionListener and actionEvent to lastButton
        lastButton.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent ae)
        {
        displayBooks = myBook.length-1;
        textArea.setText(myBook[displayBooks]+"\n");
        } // 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

PennyBoki
RE: Need Help With JTextFiles And JPanel
9 Oct, 2007 - 08:44 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



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

My Contributions
Well the problem is as the compiler pointed out:

display.getText(myBook[displayBooks]+"\n");

getText shouldn't take arguments. getText simply returns the text in String of that certain Text Field.

BUT I just swaw display is a JPanel, so try to use the function paramString().

I cannot compile this since I don't have the complete code. If you post the Book2 class I could be able to tell more precisely.

This post has been edited by PennyBoki: 9 Oct, 2007 - 08:49 AM
User is offlineProfile CardPM
+Quote Post

mariejt
RE: Need Help With JTextFiles And JPanel
9 Oct, 2007 - 08:54 AM
Post #3

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 7


My Contributions
QUOTE(PennyBoki @ 9 Oct, 2007 - 09:44 AM) *

Well the problem is as the compiler pointed out:

display.getText(myBook[displayBooks]+"\n");

getText shouldn't take arguments. getText simply returns the text in String of that certain Text Field.

BUT I just swaw display is a JPanel, so try to use the function paramString().

I cannot compile this since I don't have the complete code. If you post the Book2 class I could be able to tell more precisely.



Thank you so much!!!!!!!

I've attached my Book2 and Book classes.

Here is Book2
CODE
public class Book2 extends Book
{

    // Book2 variable
    private String bookAuthor; // Author of the book

    // Book2 constructor
    public Book2(String bookTitle, int itemNumber, int bookUnits, double bookPrice, String bookAuthor)
    {

        // call super class constructor
        super(bookTitle, itemNumber, bookUnits, bookPrice);
        
        this.bookAuthor = bookAuthor; // initializes bookTitle for Book2
    } // end constructor

    // method to set the book author
    public void setBookAuthor (String bookAuthor)
    {
        this.bookAuthor = bookAuthor;
    } // end method setBookAuthor

    // method to retrieve the book author
    public String getBookAuthor()
    {
        return bookAuthor;
    } // end method getBookAuthor

    // Calculate the stock value
    // Returns the total value of books in stock, adding a 5% restocking fee
    public double inventoryValue()
    {
        return (super.inventoryValue() * 1.05);
    }

    // method for returning the book restocking fee
    public double bookRestockingFee()
    {
        return(super.inventoryValue() * 0.05);
    }

    public String toString()
    {
    return String.format("Book Title: %s\nItem Number: %d\nUnits in Stock: %d\nBook Price: $%.2f\nInventory Value: $%.2f\nBook Author: %s\nRestocking Fee: $%.2f\n", getBookTitle(), getItemNumber(), getBookUnits(), getBookPrice(),     inventoryValue(), getBookAuthor(), bookRestockingFee());
    }

} // end class Book2


Here is my Book class:
CODE
public class Book // Book has four attributes
{

    private String bookTitle = "";    
    private int itemNumber = 0;
    private int bookUnits = 0;
    private double bookPrice = 0.00;

    // Book constructor
    public Book (String bookTitle, int itemNumber, int bookUnits, double bookPrice)
    {
        this.bookTitle = bookTitle; // initializes bookTitle for Book        
        this.itemNumber = itemNumber; // initializes itemNumber for Book
        this.bookUnits = bookUnits; // initializes bookUnits for Book
        this.bookPrice = bookPrice; // initializes bookPrice for Book
    } // end constructor

    
    // method to set the book title
    public void setBookTitle (String bookTitle)
    {
        this.bookTitle = bookTitle;
    } // end method setBookTitle

    // method to retrieve the book title
    public String getBookTitle()
    {
        return bookTitle;
    } // end method getBookTitle

    // method to set the item number
    public void setItemNumber (int itemNumber)
    {
        this.itemNumber = itemNumber;
    } // end method setItemNumber

    // method to retrieve the item number
    public int getItemNumber()
    {
        return itemNumber;
    } // end method getItemNumber

    // method to set the book units
    public void setBookUnits (int bookUnits)
    {
        this.bookUnits = bookUnits;
    } // end method setBookUnits

    // method to retrieve the book units
    public int getBookUnits()
    {
        return bookUnits;
    } // end method getBookUnits

    // method to set the book price
    public void setBookPrice (double bookPrice)
    {
        this.bookPrice = bookPrice;
    } // end method setBookPrice

    // method to retrieve the book price
    public double getBookPrice()
    {
        return bookPrice;
    } // end method getBookPrice

    // method to calculate the inventory value
    public double inventoryValue()
    {
        return bookPrice * bookUnits;
    } // end method inventoryValue


        

} // end class Book



User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Need Help With JTextFiles And JPanel
9 Oct, 2007 - 09:32 AM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



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

My Contributions
Hi, OK let me say first that I like what you've done so far it looks cool, but now come the tricky part, and that is assigning functionalities to the buttons. But before that I suggest you make(create) a function called e.g. refreshTextArea() which would be helpful. What would it do? it would simply rename the values that need to change of the already created textArea. That function you can call from the actionPerformed() method, now it would have been even better if you used array of JButtons because you could catch them like this simple code below and see inside I use the method that I mentioned refreshTextArea() that method does not exist but you have to create it:

CODE
public void actionPerformed(ActionEvent ae)
{

     for(int i=0; i<MyButton.length; i++)
    {//maybeanother if here for the buttons that don't need to refresh the textArea
           if(ae.getSource() == MyButton[i])
              refreshTextArea(MyButton[i]);
     }
}


So from what you can see there is a lot of work. But eventually you'll get it. I hope that was that simple assigning functionalities to buttons, but it's not.
Another very important thing you need the refreshTextArea(MyButton[i]); for say only three to four buttons, you know next, prev... etc...
What about the others? Well you could create functions for them as well, and just call those functions from the actionPerformed method.

NOTE: the code above is only example, not a solution. So the compiling error you get in the first post is jaust a minor detail, and you can simple remove that line of code.

Hope you find this useful. smile.gif Give it a try.

User is offlineProfile CardPM
+Quote Post

mariejt
RE: Need Help With JTextFiles And JPanel
9 Oct, 2007 - 09:51 AM
Post #5

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 7


My Contributions
QUOTE(PennyBoki @ 9 Oct, 2007 - 10:32 AM) *

Hi, OK let me say first that I like what you've done so far it looks cool, but now come the tricky part, and that is assigning functionalities to the buttons. But before that I suggest you make(create) a function called e.g. refreshTextArea() which would be helpful. What would it do? it would simply rename the values that need to change of the already created textArea. That function you can call from the actionPerformed() method, now it would have been even better if you used array of JButtons because you could catch them like this simple code below and see inside I use the method that I mentioned refreshTextArea() that method does not exist but you have to create it:

CODE
public void actionPerformed(ActionEvent ae)
{

     for(int i=0; i<MyButton.length; i++)
    {//maybeanother if here for the buttons that don't need to refresh the textArea
           if(ae.getSource() == MyButton[i])
              refreshTextArea(MyButton[i]);
     }
}


So from what you can see there is a lot of work. But eventually you'll get it. I hope that was that simple assigning functionalities to buttons, but it's not.
Another very important thing you need the refreshTextArea(MyButton[i]); for say only three to four buttons, you know next, prev... etc...
What about the others? Well you could create functions for them as well, and just call those functions from the actionPerformed method.

NOTE: the code above is only example, not a solution. So the compiling error you get in the first post is jaust a minor detail, and you can simple remove that line of code.

Hope you find this useful. smile.gif Give it a try.


This is awesome!!!! Thank you so much for your quick response and assistance. I will give it a try ASAP!

User is offlineProfile CardPM
+Quote Post

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

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