Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default Problem with GUI elements

    Good morning. I have been fighting with the layout of my program. Any help would be great.

    Line 247 states "local variable hides a field" (This is in FrameHelper Class)
    JLabel lblCompanyName = new JLabel(" Far North Skydive Center"); // label for company name
    Here is the GUI and how it appears.
    http://www.extremeshannon.com/gui.jpg

    Java Code:
    package frameviewer;
    
    /** Program:    InventoryProgram
     *  File:       FrameViewer.java
     *  Summary:    Inherits JFrame class to build a window application that contains labels and text fields.
     *  Author:     Shannon Jardine
     *  Date:       July 25, 2010
     **/
    
    import framehelper.FrameHelper; //Import the frame helper class
    import javax.swing.JFrame;
    
    public class FrameViewer
    {
          public static void main(String[] args)
          {
                JFrame frame = new FrameHelper(); // creat a frame object
                frame.setSize(400, 450); // set the size of the window - 400 is width, 200 is height
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // close the application
                frame.setTitle("My Inventory Application"); // set the title of the window
                frame.setLocationRelativeTo( null );  // center the form
                frame.setVisible(true); // display form
          } // end main
    } // end class
    Java Code:
    package framehelper;
    
    /** Program:    InventoryProgram
     *  File:       FrameHelper.java
     *  Summary:    Inherits JFrame class to build a window application that
     *              contains label and text fields.
     *  Author:     Shannon Jardine
     *  Date:       July 25, 2010
     **/
    import files.Files;
    import java.text.NumberFormat;  // import Java package used to format currency
    import manufacture.Manufacture; //Import the Manufacture Class
    import firstnamecomparator.FirstNameComparator; //Import the FirstNameComparator class
    import java.awt.GridLayout;       // required to create a grid layout
    import java.awt.BorderLayout;     // required to create a border layout
    import java.awt.event.ActionEvent; // required for click events
    import java.awt.event.ActionListener; // required for click events
    import javax.swing.JPanel;  // required to create panels
    import javax.swing.JFrame;  // required to use the frame
    import javax.swing.JButton;  // required to create buttons
    import javax.swing.JLabel;  // required to create text fields
    import javax.swing.JTextField; // required to create text fields
    import java.util.Arrays;  // required to sort array
    import javax.swing.ImageIcon;   // regired to access images
    import javax.swing.JOptionPane;  // required to use message dialog box
    
    
    public class FrameHelper extends JFrame
    {
          // Declare Class Variables
    
          // Declare two panels. gridPanel is main panel. panel is inserted into
    
          // gridPanel.
    
          private JPanel gridPanel = new JPanel(); // one panel that contains my gridlayout.
          private JPanel panel = new JPanel(); // panel used to contain buttons, labels and text fields
    
          //NumberFormat nf = NumberFormat.getCurrencyInstance();  // used to format currency
          // Declare buttons
    
          JButton firstButton; // first button
          JButton nextButton; // next button
          JButton previousButton; // previous button
          JButton lastButton; // last button
          JButton addButton;  // add button
          JButton deleteButton;  // delete button
          JButton modifyButton;  // modify button
          JButton saveButton;  // save button
          JButton searchButton;  // search button
    
    
          // Declare Text Fields
    
          JTextField nameField; // name field
          JTextField itemNumberField; // Iem number field
          JTextField numberOfUnitsInStockField; // Units in Stock field
          JTextField priceField; // Price of units field
          JTextField manufactureField; // item manufacture field
          JTextField totalInventoryField;  // sum of the inventory
          JTextField restockingFeeField; // restock fee field
          JTextField companyNameField; // company name label
    
          // Declare Labels
    
          JLabel lblName; // name label
          JLabel lbItemNumber; // Item label
          JLabel lblNumberOfUnitsInStock; // Units in Stock label
          JLabel lblManufacture; // Manufacture label
          JLabel lblPrice; // Price Label
          JLabel lblTotalInventory;  // total inventory label
          JLabel lblRestockingFee; // restock fee label
          JLabel lblLogo; // logo label
          JLabel lblCompanyName; // company name label
    
          // Declare 5 Manufacture objects
    
          Manufacture manufacture1;
          Manufacture manufacture2;
          Manufacture manufacture3;
          Manufacture manufacture4;
          Manufacture manufacture5;
    
          public static final int MAX_MANUFACTURES = 5; // set maximum size for Manufacture Array
    
          Manufacture[] item = new Manufacture[MAX_MANUFACTURES]; // create Manufacture Array object
    
          static int ArrayIndex = 0; // array index
    
          public FrameHelper() // constructor
          {
                // create and initialize five new Manufacture objects
              manufacture1 = new Manufacture();
              manufacture1.setItemNumber(1);
              manufacture1.setProductName("Slinks");
              manufacture1.setNumberOfUnitsInStock(4);
              manufacture1.setPriceOfUnit(26.00);
              manufacture1.setManufacture("PD");
    
    
              manufacture2 = new Manufacture();
              manufacture2.setItemNumber(1);
              manufacture2.setProductName("Googles");
              manufacture2.setNumberOfUnitsInStock(6);
              manufacture2.setPriceOfUnit(15.00);
              manufacture2.setManufacture("Flex Vision");
    
    
              manufacture3 = new Manufacture();
              manufacture3.setItemNumber(3);
              manufacture3.setProductName("LogBook");
              manufacture3.setNumberOfUnitsInStock(6);
              manufacture3.setPriceOfUnit(15.00);
              manufacture3.setManufacture("Log3");
    
              manufacture4 = new Manufacture();
              manufacture4.setItemNumber(4);
              manufacture4.setProductName("Gloves");
              manufacture4.setNumberOfUnitsInStock(4);
              manufacture4.setPriceOfUnit(12.00);
              manufacture4.setManufacture("Converse");
    
    
              manufacture5 = new Manufacture();
              manufacture5.setItemNumber(1);
              manufacture5.setProductName("Closing Loop");
              manufacture5.setNumberOfUnitsInStock(18);
              manufacture5.setPriceOfUnit(2.00);
              manufacture5.setManufacture("Here Hopin it Opens");
    
    
                // add Manufacture objects to Array of Manufactures object
    
                item[0] = manufacture1;
                item[1] = manufacture2;
                item[2] = manufacture3;
                item[3] = manufacture4;
                item[4] = manufacture5;
    
                gridPanel.setLayout(new BorderLayout()); // create a border layout
                gridPanel.add(this.createLabelPanel(), BorderLayout.WEST); // add label panel
                gridPanel.add(this.createTextPanel(), BorderLayout.CENTER); // add field panel
                gridPanel.add(this.createButtonPanel(), BorderLayout.SOUTH); // add button panel
                add(gridPanel);
          }
          // helper method creates a panel for the button
    
          private JPanel createButtonPanel()
          {
                ActionListener btnListen = new ButtonListener(); // create listener
    
                // create button objects
              firstButton = new JButton("First"); // create button object
              firstButton.setActionCommand("First"); // add actionCommand
              firstButton.addActionListener(btnListen); // add Listener command
    
              
              nextButton = new JButton("Next"); // create button object
              nextButton.setActionCommand("Next"); // add actionCommand
              nextButton.addActionListener(btnListen); // add Listener command
    
    
              previousButton = new JButton("Previous"); // create button object
              previousButton.setActionCommand("Previous"); // add actionCommand
              previousButton.addActionListener(btnListen); // add Listener command
    
              lastButton = new JButton("Last"); // create button object
              lastButton.setActionCommand("Last"); // add actionCommand
              lastButton.addActionListener(btnListen); // add Listener command
    
              addButton = new JButton("Add"); // create button object
              addButton.setActionCommand("Add"); // add actionCommand
              addButton.addActionListener(btnListen); // add Listener command
    
              deleteButton = new JButton("Delete"); // create button object
              deleteButton.setActionCommand("Delete"); // add actionCommand
              deleteButton.addActionListener(btnListen); // add Listener command
    
              modifyButton = new JButton("Modify"); // create button object
              modifyButton.setActionCommand("Modify"); // add actionCommand
              modifyButton.addActionListener(btnListen); // add Listener command
    
              saveButton = new JButton("Save"); // create button object
              saveButton.setActionCommand("Save"); // add actionCommand
              saveButton.addActionListener(btnListen); // add Listener command
    
              searchButton = new JButton("Search"); // create button object
              searchButton.setActionCommand("Search"); // add actionCommand
              searchButton.addActionListener(btnListen); // add Listener command
    
    
              // create panel object
    
              JPanel panels = new JPanel();
    
              panels.add(firstButton); // add first button to panel
              panels.add(nextButton); // add next button to panel
              panels.add(previousButton); // add previous button to panel
              panels.add(lastButton); // add last button to panel
              panels.add(addButton); // add add button to panel
              panels.add(deleteButton); // add delete button to panel
              panels.add(modifyButton); // add modify button to panel
              panels.add(saveButton); // add save button to panel
              panels.add(searchButton); // add search button to panel
    
    
                return panels; // return panel
    
         } // end createButtonPanel method
    
          private JPanel createLabelPanel()
          {
                // create instance of label objects
                ImageIcon icon = new ImageIcon(".\\src\\FarNorthLogo.jpg");   // load the image file
                lblLogo = new JLabel(icon); // add the image to the label
    
                lblName = new JLabel("Name:"); // label for name of item
                lbItemNumber = new JLabel("Item:"); // label for name of item
                lblNumberOfUnitsInStock = new JLabel("Units in Stock:"); // label for items in stock
                lblManufacture = new JLabel("Manufacture:"); // label for manufacture color
                lblPrice = new JLabel("Price");// Label price
                lblTotalInventory = new JLabel("Restocking Fee:"); // label for restocking fee
                lblRestockingFee = new JLabel("Sum of inv:");
    
                panel = new JPanel();
    
                panel.setLayout(new GridLayout(8, 2));
    
                // add labels to the panel
                panel.add(lblLogo);
                panel.add(lblName);
                panel.add(lbItemNumber);
                panel.add(lblNumberOfUnitsInStock);
                panel.add(lblManufacture);
                panel.add(lblPrice);
                panel.add(lblTotalInventory);
                panel.add(lblRestockingFee);
                
    
                return panel;
          } // end createLabelPanel method
    
          private JPanel createTextPanel()
          {
                //create instances of text box objects
                JLabel lblCompanyName = new JLabel("      Far North Skydive Center"); // label for company name
    
                nameField = new JTextField(); // name text field
                nameField.setEditable(false);  // set field to not editable to prevent user from changing the data
    
                itemNumberField = new JTextField(); // name text field
                itemNumberField.setEditable(false);  // set field to not editable to prevent user from changing the data
    
                numberOfUnitsInStockField = new JTextField(); // age text field
                numberOfUnitsInStockField.setEditable(false);  // set field to not editable to prevent user from changing the data
             
                manufactureField = new JTextField();   // manufacture text field
                manufactureField.setEditable(false);   // set field to not editable to prevent user from changing the data
    
                priceField = new JTextField();   // manufacture text field
                priceField.setEditable(false);   // set field to not editable to prevent user from changing the data
    
                totalInventoryField = new JTextField();    // total inventory text field to sum the total of the manufacture's ages.
                totalInventoryField.setEditable(false);   // set field to not editable to prevent user from changing the data
    
                restockingFeeField = new JTextField(); // restocking fee field
                restockingFeeField.setEditable(false);  // set field to not editable to prevent user from changing the data
    
                panel = new JPanel(); // create panel object
                panel.setLayout(new GridLayout(9, 2)); // create grid layout for fields.
    
               // panel.add(companyNameField);   // logo field
                panel.add(nameField); // add the name field to the grid layout
                panel.add(itemNumberField); // add the name field to the grid layout
                panel.add(numberOfUnitsInStockField); // add the units in stock field to the grid layout
                panel.add(manufactureField); // add the manufacture field to the grid layout
                panel.add(priceField); // add the manufacture field to the grid layout
                panel.add(restockingFeeField); // add the restock field to the grid layout
                panel.add(totalInventoryField); // add the total inventory field to the grid layout
                 
    
                return panel; // return the panel
    
          } // end createTextPanel method
    
    
               // nested ButtonListener class
    
          class ButtonListener implements ActionListener
    
          {
    
                public void actionPerformed(ActionEvent e)
    
                {
    
                      // Parameters for the sort method are the girls array and an instance of my FirstNameComparator class
    
                      Arrays.sort(item, new FirstNameComparator()); // Sort the girls array
    
                      totalInventoryField.setText(String.valueOf(item[ArrayIndex].getValueOfInventory()));  // display the sum of the girl's age
    
    
    
                      Files file = new Files();  // instantiate th efiles
    
    
    
                      //add button functions
    
                      if (e.getActionCommand() == null ? "First" == null : e.getActionCommand().equals("First")) // if First button is clicked
    
                      {
    
                            ArrayIndex = 0;  // set the value of the array index
    
                            nameField.setText(item[ArrayIndex].getProductName()); // get the person's name and assign it the name text field.
    
                            priceField.setText(String.valueOf(item[ArrayIndex].getPriceOfUnit())); // get the persons age and assign it the age text field.
    
                            restockingFeeField.setText(String.valueOf(item[ArrayIndex].computeRestockingFee(item[ArrayIndex].getPriceOfUnit())));  // pass in the person's age to the method that returns the age in ten years
    
                            manufactureField.setText(String.valueOf(item[ArrayIndex].getManufacture()));    // get the persons hair color and assign it the hair color text field.
    
    
    
                      }
    
    
    
                      if (e.getActionCommand() == null ? "Next" == null : e.getActionCommand().equals("Next")) // if Next button is clicked
    
                      {
    
                            // code here
    
    
    
    
    
                      }
    
    
    
                      if (e.getActionCommand() == null ? "Previous" == null : e.getActionCommand().equals("Previous")) // if Previous button is clicked
    
                      {
    
    
    
                            // code here
    
    
    
                      }
    
    
    
                      if (e.getActionCommand() == null ? "Last" == null : e.getActionCommand().equals("Last")) // if Last button is clicked
    
                      {
    
    
    
                            // code here
    
    
    
                      }
    
    
    
                      if (e.getActionCommand() == null ? "Add" == null : e.getActionCommand().equals("Add")) // if Add button is clicked
    
                      {
    
                            // code here
    
                      }
    
    
    
                      if (e.getActionCommand() == null ? "Delete" == null : e.getActionCommand().equals("Delete")) // if Delete button is clicked
    
                      {
    
                            // code here
    
                      }
    
    
    
                      if (e.getActionCommand() == null ? "Modify" == null : e.getActionCommand().equals("Modify")) // if Modify button is clicked
    
                      {
    
                            // code here
    
                      }
    
    
    
                      if (e.getActionCommand() == null ? "Search" == null : e.getActionCommand().equals("Search")) // if Search button is clicked
    
                      {
    
                            // code here
    
                      }
    
    
    
                      if (e.getActionCommand() == null ? "Save" == null : e.getActionCommand().equals("Save")) // if Save button is clicked
    
                      {
    
                            try
    
                            {
    
                                  file.appendToFile(item[ArrayIndex].getProductName());
    
                                  file.appendToFile(", ");
    
                                  file.appendToFile(String.valueOf(item[ArrayIndex].getPriceOfUnit()));
    
                                  file.appendToFile(", ");
    
                                  file.appendToFile(item[ArrayIndex].getManufacture());
    
                                  file.appendToFile(", ");
    
                                  file.appendToFile(String.valueOf(item[ArrayIndex].getTotalInventory()));
    
                                  file.appendToFile(", ");
    
                                  JOptionPane.showMessageDialog(null, item[ArrayIndex].getProductName() + " has been saved to the file.");  // message indicating the file has been saved
    
                            }
    
                            catch (Exception ex)
    
                            {//Catch exception if any
    
    
    
                                  JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage());  // catch errors that can occur
    
                            }
    
    
    
                      }  // end if
    
    
    
                } // end actionPerformed
    
    
    
    
    
          } // end class ButonListerner
    } // end FrameHelperClass

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    lblCompanyName is defined twice, once as a field of the class and once method local in createTextPanel(), that's the problem.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default

    Great got that fixed. Now looking at the GUI 2 more problems.
    1 The Labels and the fields do not line up accross from each other

    2 all the buttons are on the same line. i would like to drop 5 buttons bellow the other 4.
    First Next Previous Last
    Add Delete Modify Save Search

    New GUI jpg
    http://www.extremeshannon.com/gui.jpg

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    The problem with the labels and fields is that they are not on the same panel, put them onto one panel.
    Regarding the buttons: the panel containing the buttons does not have a LayoutManager at all.
    Read this: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. DefaultListModel Elements
    By jboy in forum New To Java
    Replies: 7
    Last Post: 10-24-2009, 03:23 AM
  2. what does <Elements> mean
    By JordashTalon in forum New To Java
    Replies: 1
    Last Post: 01-31-2009, 10:19 PM
  3. Negative elements
    By swikar.java in forum New To Java
    Replies: 6
    Last Post: 12-15-2008, 04:48 AM
  4. PROBLEM - calculating with array elements
    By ella in forum New To Java
    Replies: 13
    Last Post: 12-04-2008, 12:36 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
  •