I having major issues trying to make a GUI for this program. How do I connect my program to my GUI? I don't get it?Can someone help me with this? I also can't get result to read on my GUI. e23b489d81b74c698e1f29a66ca236f6

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ofelia.s_inventory_program_3_and_4_gui;

/**
 *
 * @author Ofelia G. S. Stewart
 */
public class GUI extends javax.swing.JFrame {

    /**
     * Creates new form GUI
     */
    public GUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        Exit = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Exit.setText("Exit");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "DVD Title:", "Qty. of Stock:", "Item #:", "Price:", "Restocking Fee:", "Total Value:"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setRowHeight(24);
        jScrollPane1.setViewportView(jTable1);
        if (jTable1.getColumnModel().getColumnCount() > 0) {
            jTable1.getColumnModel().getColumn(0).setResizable(false);
            jTable1.getColumnModel().getColumn(1).setResizable(false);
            jTable1.getColumnModel().getColumn(2).setResizable(false);
            jTable1.getColumnModel().getColumn(3).setResizable(false);
            jTable1.getColumnModel().getColumn(4).setResizable(false);
            jTable1.getColumnModel().getColumn(5).setResizable(false);
        }

        jLabel1.setText("Welcome to DVD Inventory");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 554, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(251, 251, 251)
                        .addComponent(Exit)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(207, 207, 207)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(Exit)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jLabel1.getAccessibleContext().setAccessibleName("Welcome to DVD Inventory");

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton Exit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration                   
}




/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ofelia.s_inventory_program_3_and_4_gui;

/**
 *
 * @author Ofelia G. S. Stewart
 */
public class TestClass{
    public static void main(String arg[]){
        Product dvd;

        dvd = new DVD(" Armegeddon",1,5,17.00);
        System.out.println(dvd);

        dvd = new DVD(" Fast 5",2,3,17.00);
        System.out.println(dvd);

        dvd = new DVD(" Independence Day",3,10,19.95);
        System.out.println(dvd);

        dvd = new DVD(" Tokyo Drift",4,11,19.95);
        System.out.println(dvd);

        dvd = new DVD(" Ride Along",5,12,11.00);
        System.out.println(dvd);
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ofelia.s_inventory_program_3_and_4_gui;

/**
 *
 * @author Ofelia G. S. Stewart
 */
class Product{

    private double prdStock;
    private double prdPrice;
    private double prdItem;
    Product(double Stock, double Price, double Item){
         prdStock = Stock;
        prdPrice = Price;
        prdItem = Item;
    }
    public void setPrdItem(int item){
    prdItem = item;
}//end method set DVD Item
//return DVD Item

public double getPrdItem(){
    return prdItem;
}//end method get DVD Item

//set DVD Title


public void setPrdStock(double Stock){
    prdStock = Stock;
}//end method set DVD Stock

//return DVD Stock

public double getPrdStock(){

    return prdStock;
}//end method get DVD Stock

public void setPrdPrice(double Price){
    prdPrice = Price;
}//end method Set DVD Price

//return DVD price
public double getPrdPrice(){
    return prdPrice;
}//end method get DVD Price

//calculate inventory value

public double value(){
    return prdPrice * prdStock;
}//end method value
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ofelia.s_inventory_program_3_and_4_gui;

/**
 *
 * @author Ofelia G. S. Stewart
 */
class DVD extends Product{
    String title;
    double restockingFee=.05;
    DVD(String title,double Stock, double Price, double Item){
        super(Stock,Price,Item);
        this.title=title;
    }

    public void setTitle(String title){
        this.title=title;       
    }

    public String getTitle(){
        return this.title;
    }

    @Override
    public double value(){
        double temp=getPrdPrice() * getPrdStock();
        double temp1=temp*restockingFee;
        return temp+temp1;
    }

        @Override
    public String toString()
{
    return "Product: " + getTitle() + "\n" +
           "Product Item: " + getPrdItem() + "\n" +
           "Qty In Stock: " + getPrdStock() + "\n" +
           "Product Price: $"  + getPrdPrice() + "\n" +
           "Stock Value without Restocking Fee: $" + super.value()+ "\n" +
           "Stock Value with Restocking Fee: $" + value();
}
}// end

Recommended Answers

All 10 Replies

Assignmentis to :

Quoted Text Here

Modify the Inventory Program to use a GUI. The GUI should display the
information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee.

Quoted Text Here

Please search for "What is a graphical user interface" and read more about the purpose of a gui. Basically you are going to eliminate "TestClass" and replace it with your "GUI". Click Here to see an example of what someone else who had this assignment did.

To enter data in your table, you are going to need to set CellSelectionEnabled(true); or click on the checkbox in NetBeans. However, from what you wrote, it looks like your instructor wants you to display one record at a time, not multiple records.

I think that it makes it easier to follow the code you posted if you separate your code blocks by class when you post them.

Class 1

//Class 1 code

Class 2

//Class 2 code

Rather than posting all of your code in one code block.

//Class 1

//Class 1 code

//Class 2
//Class 2 code

I looked over everything from the chapters read in last weeks and this weeks class, there is not much discussion on this, only to create a GUI. I have reviewed this site, I did so 2 days ago as well, I just don't get it. I doon't understand why my teacher wants this when we have not learned much of anything about GUIs in this class so far? Idon't understand how to get my GUI to compile and run my code, and I don't know how to connect it to my code.This is killing me and I can't seem to get anyone to help me understand this. I read the book, and I read many online tutorials and watched videos, none of this is helping me, only making it more confusing.

I'm going to try this, this was a bit easier to understand than the tutorials and readings I keep finding. Seperate code blocks? What does this do, if I may ask?

Code blocks. What I meant by that is to click "</> Code", paste your code for your first class. Click "Insert Code Snippit". Then repeat this for each class. Rather than clicking it once and posting it all together. What will this do? Make it so people are more likely to read your code.

Are you using NetBeans or Eclipse? If so, you may want to check out some tutorials on how to use those programs. Basically, you want to use a JFrame as you have done. Put some buttons (and maybe textboxes) on it. And do something when the buttons are pressed. You have to listen for events (button presses) and take action based on which button was pressed.

You seem to be using NetBeans, so add your button. Then

  • Right-click on your button, select "Events"
  • Select "Action"
  • Select "actionPerformed"

add your code to perform when the button is pressed here.

Here is what NetBeans does:

private javax.swing.JButton jButton1;

//set button text
jButton1.setText("jButton1");

//add event listener
jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
});

//event handler - do this when jButton1 button is pressed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:


}//jButton1ActionPerformed


private javax.swing.JButton jButton2;
jButton2.setText("jButton2");

//add event listener
jButton2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
});


//event handler - do this when jButton2 button is pressed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      // TODO add your handling code here:

}//jButton2ActionPerformed

Here is another way to do it:

public class GUI extends JFrame implements ActionListener{

    private javax.swing.JButton jButton1;

    jButton1 = new JButton("jButton1");

    //add listener
    jButton1.addActionListener(this);


    private javax.swing.JButton jButton2;

    jButton2 = new JButton("jButton2");

    //add listener
    jButton2.addActionListener(this);


    //event handler -- this is called when any button is pressed
    public void actionPerformed(ActionEvent event) {

        //determine which button was pressed
        if (event.getSource() == jButton1) {
            //do this when jButton1 button is pressed

            // TODO add your handling code here:

        }//if 
        else if (event.getSource() == jButton2) {
            //do this when jButton2 button is pressed

            // TODO add your handling code here:

        }//else if
    }//actionPerformed

}//class

Click Here to see documentation.

I think I might understand this, but what button are you speaking about? I have a table and an exit button, are you referring to the table?

I'm not very familiar with using tables in java. You might check this out: Click Here

Also this: Click Here

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.