I am a student really struggling with Java and needless to say, I am pretty isolated from anyone who gives a care enough to help me, I am on my 8th week and really struggling with GUI's I have some code written, but the gui will not display, I am defintely missing something in my code....can someone help me?? I used our schools Java lab and the person there just gave me some code to use, but I want to figure this out. I have seen many students here post their Inventory5 program, but I want to use MY code anyways here is what I have so far

//This is an inventory program that stores
//and lists the inventory of Coaxial Connector-fittings
//Author: 
//Class: IT215
//Date: October 17, 2009

package inventory;


import javax.swing.*;
import javax.swing.JFrame; // provides basic window features
import javax.swing.JLabel; // displays text and images
import javax.swing.JButton;
import java.awt.*;
import javax.swing.JTextPane;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.Icon;






public class Main
{//Start of class Main

    //Main method begins program execution
    public static void main(String[] args)
    {//Begin main method

        //Welcome message
        System.out.println("Welcome to the Inventory Program!!\n");

        //Create object or instance of classes Fittings and Inventory

        MetalFittings Fittings;
        Inventory myinv = new Inventory (3);


        //Assign values to variables in array
        Fittings = new MetalFittings("RG-06",10,34,25.66, "Anodized Titanium\n");
        myinv.add (Fittings, 0);

        Fittings = new MetalFittings("RG-59",20,28,18.33, "Aluminum\n");
        myinv.add (Fittings, 1);

        Fittings = new MetalFittings("RG-11",30,56,32.21, "Brass\n");
        myinv.add (Fittings, 2);

        //Sort array elements
        myinv.sort();

        //Output array elements
        for (int i = 0; i < 3; i++)
        {
           System.out.println(myinv.get(i));
        }
        //Get and display total assets of inventory (price * stock)
        System.out.printf("\nTotal of Assets in Inventory:$%.2f\n\n", myinv.assets());





        }//End method main


}//End class Main


class Fittings
    {//Start of class Fittings
        private String size;//size variable
        private int item_number;//item number variable
        private int stock;//stock variable
        private double price;//price variable


        //Constructor with parameters
        public Fittings(String f_size, int f_item_number, int f_stock, double f_price)
        {
            size = f_size;
            item_number = f_item_number;
            stock = f_stock;
            price = f_price ;


        }//End default constructor



        //Method to set size to memory
        public void setsize(String fsize)
        {
           size = fsize;
        }//End method setsize

        //Method to get size from memory
        String getsize()
        {
           return size;
        }//End method get size

        //Method to set item_number to memory
        public void setitem_number(int f_item_number)
        {
           item_number = f_item_number;
        }//End method set item_number

        //Method to get item_number from memory
        int getitem_number()
        {
           return item_number;
        }//End method getitem_number

        //Method to set stock to memory
        public void setstock(int f_stock)
        {
           stock = f_stock;
        }//End method setstock

        //Method to get stock from memory
        int getstock()
        {
           return stock;
        }//End method getstock

        //Method to set price to memory
        public void setprice(double f_price)
        {
           price = f_price;
        }//End method setprice

        //Method to get price from memory
        double getprice()
        {
           return price;
        }//End method getprice

        //Method to set total_assets (calculated result)
        public double total_assets()
        {
           return stock * price;
        }




        //Print format displayed in array
        public String toString()
        {
           return String.format("Fitting Type:%s   Item Number:%d   Fittings in Stock:%d   Price per Fitting:$%.2f",
                size, item_number, stock, price, total_assets());

        }


    }//End of class Fittings

//Sub-class of Fittings
class MetalFittings extends Fittings

{//Start of class MetalFittings

    //Variable metal
    private String metal;

//Constructor
public MetalFittings(String f_size,int f_item_number ,int f_stock, double f_price,String metal )

   {
      super (f_size, f_item_number, f_stock, f_price);
      this.metal = metal;
   }//End constructor

//Method to return "metal"
public String getmetal()
      {
         return metal;
      }

      public double total_assets()
      {
        return super.total_assets()*1.05;
      }

      //Method to apply fee
      public double stock_fee()
      {
         return super.total_assets()*.05;
      }//End stock_fee method

      //Method to set "metal"
      public void setmetal(String metal)
      {
         this.metal = metal;
      }//End method to set "metal"

      //Method to add metal and fee to array
      public String toString()

      {
         return super.toString()+ String.format(" Restocking fee: $%.2f Metal: %-20s",
              stock_fee(), metal);
      }//End tostring methos

   }//End class MetalFittings










class Inventory

{//Start of class Inventory
    double asset;
    String tostring;

private Fittings[] list;

public Fittings[] getArray()
{
   return list;
}
  //Constructor
  public Inventory (int size)



  {
     list = new Fittings[size];
  }

  //Method to total indvidual "total_assets" calculations
  public double assets()
  {
     double asset = 0.0;
     for (int i = 0; i < list.length; i++)
     {
     asset += list[i].total_assets();
     }


     //Return asset variable
     return asset;
  }



  //Add item to array
  public void add(Fittings d, int p)
    {
       list [p] = d;
    }
  //Get item for array
  public Fittings get(int i)
    {
       return list[i];
    }
  //Sort by fitting-size
  public void sort()
    {  //Bubble sort
       int n = list.length;
       for (int search = 1; search < n; search++){
       for (int i= 0; i < n-search; i++){
       if (list[i].getsize() . compareToIgnoreCase (list[i=1].getsize()) > 0){
       //Method to swap
       Fittings temp = list[i];
       list[i] = list[i+1];
       list [i+1] = temp;




       }
       }
       }
       }

       }//End of class Inventory


class gui extends JFrame
         {
             private JLabel labelA;

             private JLabel labelB;

             private JLabel labelC;

             private JLabel labelD;

             private JLabel labelE;

             private JLabel labelF;

             private JLabel labelG;

             private JLabel labelH;



         public gui()
             {
                super("Fittings Inventory");
                setLayout (new FlowLayout());



                labelA = new JLabel ("Size");
                add(labelA);

                labelB = new JLabel ("Item Number");
                add(labelB);

                labelC = new JLabel ("Fittings in stock");
                add(labelC);

                labelD = new JLabel("Price per Fitting");
                add(labelD);

                labelE = new JLabel ("Metal");
                add(labelE);

                labelF = new JLabel ("Stock fee");
                add(labelF);

                labelG = new JLabel ("Total assets in inventory");
                add(labelG);


                Icon icon = new ImageIcon(getClass().getResource(""));
                labelH = new JLabel(icon);
                add(labelH);


         }


         class outlabels extends JPanel
         {
             public outlabels()
         {
              JPanel Outlabels = new JPanel();
              this.add (Outlabels);

              this.add(labelA);

              this.add(labelB);

              this.add(labelC);

              this.add(labelD);

              this.add(labelE);

              this.add(labelF);

              this.add(labelG);

              this.add(labelH);
         }

         }
         class button extends JPanel
         {
               JPanel button = new JPanel();

               JButton nextbut = new JButton("NEXT");

               JButton backbut = new JButton("BACK");

               JButton donebut = new JButton("DONE");



         }

         JTextPane newpane = new JTextPane();

         {
            setLayout (new GridLayout());

            setLayout (new BorderLayout());

            JPanel mypanel = new JPanel();

                 JScrollPane scroll = new JScrollPane(newpane, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER,
                         JScrollPane.VERTICAL_SCROLLBAR_NEVER);

                    scroll.setPreferredSize(new Dimension(250, 400));

                    newpane.setEditable(false);

                    mypanel.add(scroll);

                    scroll.setVisible (true);

            Container c = getContentPane();

               c.setLayout(new BorderLayout());

               newpane.setLayout(new BorderLayout());

               newpane.setLayout (new GridLayout(6,3));

               newpane.setLayout(new FlowLayout());

               c.setVisible(true);

            mypanel.setLayout(new FlowLayout(FlowLayout.CENTER));

            this.add(newpane, BorderLayout.CENTER);

            this.add(newpane, BorderLayout.SOUTH);

            scroll.setVisible(true);



         }
         }

Sorry for the long code; I need to create a next, previous and last button so the user can move through the array elements (if clicking on first they move to the 1st array element, next would be incremented to the next and last would goto the last element ad the user should be able to loop through the array (clicking on next while on the last array element they would move to first vice versa)

I really want to learn Java, but starting to feel too stupid!

Recommended Answers

All 6 Replies

Please post again the code but this time, the code tags end like this:
(/CODE) not: (CODE)
Also have each class in its own code tags.
Use the (code) button when you create a post

deleted

deleted

Here's your code, formatted and in code tags:

//This is an inventory program that stores
//and lists the inventory of Coaxial Connector-fittings
//Author:
//Class: IT215
//Date: October 17, 2009
package inventory;

import javax.swing.*;
import javax.swing.JFrame; // provides basic window features
import javax.swing.JLabel; // displays text and images
import javax.swing.JButton;
import java.awt.*;
import javax.swing.JTextPane;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.Icon;

public class Main
{//Start of class Main

//Main method begins program execution
    public static void main(String[] args)
    {//Begin main method

//Welcome message
        System.out.println("Welcome to the Inventory Program!!\n");

//Create object or instance of classes Fittings and Inventory

        MetalFittings Fittings;
        Inventory myinv = new Inventory(3);


//Assign values to variables in array
        Fittings = new MetalFittings("RG-06", 10, 34, 25.66, "Anodized Titanium\n");
        myinv.add(Fittings, 0);

        Fittings = new MetalFittings("RG-59", 20, 28, 18.33, "Aluminum\n");
        myinv.add(Fittings, 1);

        Fittings = new MetalFittings("RG-11", 30, 56, 32.21, "Brass\n");
        myinv.add(Fittings, 2);

//Sort array elements
        myinv.sort();

//Output array elements
        for (int i = 0; i < 3; i++)
        {
            System.out.println(myinv.get(i));
        }
//Get and display total assets of inventory (price * stock)
        System.out.printf("\nTotal of Assets in Inventory%.2f\n\n", myinv.assets());

        
//        new gui ();
    }//End method main
}//End class Main

class Fittings
{//Start of class Fittings
    private String size;//size variable
    private int item_number;//item number variable
    private int stock;//stock variable
    private double price;//price variable


//Constructor with parameters
    public Fittings(String f_size, int f_item_number, int f_stock, double f_price)
    {
        size = f_size;
        item_number = f_item_number;
        stock = f_stock;
        price = f_price;


    }//End default constructor

//Method to set size to memory
    public void setsize(String fsize)
    {
        size = fsize;
    }//End method setsize

//Method to get size from memory
    String getsize()
    {
        return size;
    }//End method get size

//Method to set item_number to memory
    public void setitem_number(int f_item_number)
    {
        item_number = f_item_number;
    }//End method set item_number

//Method to get item_number from memory
    int getitem_number()
    {
        return item_number;
    }//End method getitem_number

//Method to set stock to memory
    public void setstock(int f_stock)
    {
        stock = f_stock;
    }//End method setstock

//Method to get stock from memory
    int getstock()
    {
        return stock;
    }//End method getstock

//Method to set price to memory
    public void setprice(double f_price)
    {
        price = f_price;
    }//End method setprice

//Method to get price from memory
    double getprice()
    {
        return price;
    }//End method getprice

//Method to set total_assets (calculated result)
    public double total_assets()
    {
        return stock * price;
    }

//Print format displayed in array
    public String toString()
    {
        return String.format("Fitting Type:%s Item Number:%d Fittings in Stock:%d Price per Fitting%.2f",
                size, item_number, stock, price, total_assets());

    }
}//End of class Fittings

//Sub-class of Fittings
class MetalFittings extends Fittings
{//Start of class MetalFittings

//Variable metal
    private String metal;

//Constructor
    public MetalFittings(String f_size, int f_item_number, int f_stock, double f_price, String metal)
    {
        super(f_size, f_item_number, f_stock, f_price);
        this.metal = metal;
    }//End constructor

//Method to return "metal"
    public String getmetal()
    {
        return metal;
    }

    public double total_assets()
    {
        return super.total_assets() * 1.05;
    }

//Method to apply fee
    public double stock_fee()
    {
        return super.total_assets() * .05;
    }//End stock_fee method

//Method to set "metal"
    public void setmetal(String metal)
    {
        this.metal = metal;
    }//End method to set "metal"

//Method to add metal and fee to array
    public String toString()
    {
        return super.toString() + String.format(" Restocking fee: $%.2f Metal: %-20s",
                stock_fee(), metal);
    }//End tostring methos
}//End class MetalFittings

class Inventory
{//Start of class Inventory
    double asset;
    String tostring;
    private Fittings[] list;

    public Fittings[] getArray()
    {
        return list;
    }
//Constructor
    public Inventory(int size)
    {
        list = new Fittings[size];
    }

//Method to total indvidual "total_assets" calculations
    public double assets()
    {
        double asset = 0.0;
        for (int i = 0; i < list.length; i++)
        {
            asset += list[i].total_assets();
        }


//Return asset variable
        return asset;
    }

//Add item to array
    public void add(Fittings d, int p)
    {
        list[p] = d;
    }
//Get item for array
    public Fittings get(int i)
    {
        return list[i];
    }
//Sort by fitting-size
    public void sort()
    { //Bubble sort
        int n = list.length;
        for (int search = 1; search < n; search++)
        {
            for (int i = 0; i < n - search; i++)
            {
                if (list[i].getsize().compareToIgnoreCase(list[i = 1].getsize()) > 0)
                {
//Method to swap
                    Fittings temp = list[i];
                    list[i] = list[i + 1];
                    list[i + 1] = temp;
                }
            }
        }
    }
}//End of class Inventory


/*
class gui extends JFrame
{

    private JLabel labelA;
    private JLabel labelB;
    private JLabel labelC;
    private JLabel labelD;
    private JLabel labelE;
    private JLabel labelF;
    private JLabel labelG;
    private JLabel labelH;

    public gui()
    {
        super("Fittings Inventory");
        setLayout(new FlowLayout());



        labelA = new JLabel("Size");
        add(labelA);

        labelB = new JLabel("Item Number");
        add(labelB);

        labelC = new JLabel("Fittings in stock");
        add(labelC);

        labelD = new JLabel("Price per Fitting");
        add(labelD);

        labelE = new JLabel("Metal");
        add(labelE);

        labelF = new JLabel("Stock fee");
        add(labelF);

        labelG = new JLabel("Total assets in inventory");
        add(labelG);


        Icon icon = new ImageIcon(getClass().getResource(""));
        labelH = new JLabel(icon);
        add(labelH);
    }

    class outlabels extends JPanel
    {

        public outlabels()
        {
            JPanel Outlabels = new JPanel();
            this.add(Outlabels);

            this.add(labelA);

            this.add(labelB);

            this.add(labelC);

            this.add(labelD);

            this.add(labelE);

            this.add(labelF);

            this.add(labelG);

            this.add(labelH);
        }
    }

    class button extends JPanel
    {

        JPanel button = new JPanel();
        JButton nextbut = new JButton("NEXT");
        JButton backbut = new JButton("BACK");
        JButton donebut = new JButton("DONE");
    }
    JTextPane newpane = new JTextPane();


    {
        setLayout(new GridLayout());

        setLayout(new BorderLayout());

        JPanel mypanel = new JPanel();

        JScrollPane scroll = new JScrollPane(newpane, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER,
                JScrollPane.VERTICAL_SCROLLBAR_NEVER);

        scroll.setPreferredSize(new Dimension(250, 400));

        newpane.setEditable(false);

        mypanel.add(scroll);

        scroll.setVisible(true);

        Container c = getContentPane();

        c.setLayout(new BorderLayout());

        newpane.setLayout(new BorderLayout());

        newpane.setLayout(new GridLayout(6, 3));

        newpane.setLayout(new FlowLayout());

        c.setVisible(true);

        mypanel.setLayout(new FlowLayout(FlowLayout.CENTER));

        this.add(newpane, BorderLayout.CENTER);

        this.add(newpane, BorderLayout.SOUTH);

        scroll.setVisible(true);
    }
}*/

I added and commented out line 58. You have no GUI because you've created no GUI/Java Swing objects. You have no call to your gui class constructor (i.e. line 58), which presumably calls all of the other Swing objects. To prove this, I've commented out everything past line 250 above and the program still compiles and runs. Hence none of that stuff was being used. You need to use it. You'll use it by having a line something like line 58 that I added. Delete that line. You'll end up with something like it somewhere, but it'll probably be somewhere different. I just added it and commented it out to show my point. No such line exists in your program and it needs to.

You should set aside this project and learn the basics of Java Swing, the come back to this. Right now it's too big to handle. There's too much going on, so it'll be hard to debug. You have Labels, Layouts, Buttons, etc., but don't yet know how to display the JFrame that these go on.

http://java.sun.com/docs/books/tutorial/uiswing/

The above link is your starting point. Draw some JFrames with a few simple Labels and Buttons that don't do anything, play with other GUI components, add a LayoutManager, etc. till you feel comfortable. Then come back and work on the Inventory project again.

Code tags are simply this.

[code]

// paste code here

[/code]

You had it close on your first post.

I have also started another modification using the NetBeans design tools like the tutorial on the link you posted, I will post that code instead, I am having problems calling methods from my "original" (non gui) class, I am getting static can be called from non static (or vice versa) errors

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.