• Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying in JFrame

 
Kyle Ruey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created an Inventory Program that run correctly. Then I was asked to create a GUI that would display the products one by one and then add a next last previous and first button. So I created the GUI from the with all the correct Labels and text fields and buttons. No I am having trouble inserting the correct code into the GUI to get it to display what I need it to.




public class InventoryStarter {















public InventoryStarter(){
}


private int position = 0;
Inventory[]productArray = null;




private void setProductData() {
Inventory book1 = new Books(2,"If you give a mouse a cookie",10,22.32,"Childrens");
Inventory book2 = new Books(4,"To Kill a Mocking bird",7,26.97,"Fiction");
Inventory book3 = new Books(3,"Unbroken", 5,39.20,"Non-Fiction");
Inventory book4 = new Books(1,"The War of the Worlds",9,18.59,"Sci-Fi");


// Create Array to store the objects
productArray = new Inventory[4];
productArray[0] = book1;
productArray[1] = book2;
productArray[2] = book3;
productArray[3] = book4;




System.out.printf("Welcome to the Inventory Program%n%n");


}
private static void sortProducts(Inventory[] productArray) {
int length = productArray.length;
Inventory obj1 = null;
Inventory obj2 = null;
Inventory temp = null;
for (int i = 1; i < length; i++) {
for (int j = 0; j < length - i; j++) {
obj1 = productArray[j];
obj2 = productArray[j + 1];
if (obj1.getItemName().compareTo(obj2.getItemName()) > 0) {
temp = obj1;
productArray[j] = productArray[j + 1];
productArray[j + 1] = temp;
}
}
}
}





private static double calculateEntireInventory(Inventory[] productArray) {
Inventory obj = null;
double totalInventory = 0;
for (int i = 0; i < productArray.length; i++) {
// Sets the current object in obj variable
obj = productArray[i];
totalInventory += obj.inventoryValue();
}
return totalInventory;
}








}










public class Inventory
{
// variables
protected String itemName;
protected double itemPrice;
protected int itemNumber,
itemStock;
private double inventoryValue;


public Inventory(int itemNumber, String itemName, int itemStock, double itemPrice)
{
this.itemNumber = itemNumber;
this.itemName = itemName;
this.itemStock = itemStock;
this.itemPrice = itemPrice;
} // end constructor


//---------------SET and GET METHODS --------------------
public void setItemName(String itemName)
{
this.itemName = itemName;
} //end setItemName

public String getItemName()
{
return this.itemName;
} // end getItemName


public void setItemNumber(int itemNumber)
{
this.itemNumber = itemNumber;
}//end setItemNmber

public int getItemNumber()
{
return this.itemNumber;
} // end getItemNumber


public void setItemStock(int itemStock)
{
this.itemStock = itemStock;
}//end setItemStock

public int getItemStock()
{
return this.itemStock;
} // end getItemStock


public void setItemPrice(double itemPrice)
{
this.itemPrice = itemPrice;
} // end setItemPrice

public double getItemPrice()
{
return this.itemPrice;
} // end getItemPrice


//----------Calculate Inventory Value for each Item-------------
public double inventoryValue()
{
this.inventoryValue =inventoryValue;
return itemStock * itemPrice;
} // end inventoryValue



//-------------Sort Inventory Method---------------------------
public static Inventory[] sortInventory(Inventory[] theInventory)
{
Inventory tmp;
for (int index = 0; index < theInventory.length; index++)
{
for (int anotherIndex = index + 1; anotherIndex < theInventory.length; anotherIndex++)
{
String s1 = theInventory[index].getItemName();
String s2 = theInventory[anotherIndex].getItemName();
if (s1.compareTo(s2) > 0)
{
tmp = theInventory[index];
theInventory[index] = theInventory[anotherIndex];
theInventory[anotherIndex] = tmp;
}
}
}
return theInventory;
}

// ----------convert to String-------------------------

@Override
public String toString()
{
return String.format("%nItem Number: %d%nItem Name: %s%nNumber in Stock: %d%nPrice: $%.2f%nTotal Cost: $%.2f%n",
getItemNumber(), getItemName(), getItemStock(), getItemPrice(), inventoryValue());
} // end method toString


//----------Calculate Total Inventory Value--------------
public static Inventory[] totalInventoryValue(Inventory[] theInventory)
{
double total = 0;
for (Inventory theInventory1 : theInventory) {
total += theInventory1.inventoryValue();
}
System.out.println("Total Inventory Value: " + total);
return theInventory;
} // end totalInventoryValue

void add(Books book, int i) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

int size() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

} // end class Inventory




public class Books extends Inventory
{

private String bookCategory;
private double restockingFee;






public Books(int itemNumber, String itemName, int itemStock, double itemPrice, String category)
{
super(itemNumber, itemName, itemStock, itemPrice);
this.bookCategory = category;
}






public String getBookCategory()
{
return bookCategory;
}


public double restockingFee()
{
return (itemPrice * .05);
}



@Override
public double inventoryValue()
{
return (itemStock * itemPrice + 05);
}



@Override
public String toString()
{

return String.format(" %nBookCategory: %s%nItem Number: %d%nItem Name: %s%nNumber in Stock: %d%nPrice: $%.2f%nRestocking Fee: $%.2f%nTotal Cost: $%.2f%n",

bookCategory, getItemNumber(), getItemName(), getItemStock(), getItemPrice(), restockingFee(), inventoryValue());
}








}








import java.awt.GraphicsConfiguration;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/*
* 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.
*/

/**
*
* @author kyle
*/
public class Library extends javax.swing.JFrame {






/**
* Creates new form Library
*/
public Library() {
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() {

jPanel1 = new javax.swing.JPanel();
bookCategory = new javax.swing.JLabel();
bookName = new javax.swing.JLabel();
bookNumber = new javax.swing.JLabel();
itemStock = new javax.swing.JLabel();
itemPrice = new javax.swing.JLabel();
restockingFee = new javax.swing.JLabel();
field1 = new javax.swing.JTextField();
field2 = new javax.swing.JTextField();
field3 = new javax.swing.JTextField();
field4 = new javax.swing.JTextField();
field5 = new javax.swing.JTextField();
field6 = new javax.swing.JTextField();
inventoryValue = new javax.swing.JLabel();
field7 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
exitBtm = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jLabel8 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Library Book Inventory");
setBackground(new java.awt.Color(0, 0, 0));
setForeground(java.awt.Color.white);

jPanel1.setBackground(new java.awt.Color(51, 255, 255));

bookCategory.setText("Book Category:");

bookName.setText("Book Name:");

bookNumber.setText("Book Number:");

itemStock.setText("Books In Stock:");

itemPrice.setText("Book Price:");

restockingFee.setText("Restocking Fee:");

field1.setEditable(false);
field1.setText(field1.getName());



field2.setEditable(false);



field3.setEditable(false);

field4.setEditable(false);

field5.setEditable(false);

field6.setEditable(false);

inventoryValue.setText("Total Cost:");

field7.setEditable(false);

jButton1.setText("First");

exitBtm.setText("Exit");
exitBtm.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitBtmActionPerformed(evt);
}
});

jButton2.setText("Last");

jButton3.setText("Next");

jButton4.setText("Last");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bookCategory)
.addComponent(bookName)
.addComponent(bookNumber))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(field1)
.addComponent(field2)
.addComponent(field3)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(inventoryValue)
.addGap(40, 40, 40)
.addComponent(field7))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(itemStock)
.addGap(18, 18, 18)
.addComponent(field4, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(restockingFee)
.addComponent(itemPrice))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(field5)
.addComponent(field6))))
.addContainerGap(206, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addGap(18, 18, 18)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(exitBtm)
.addGap(24, 24, 24))))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bookCategory)
.addComponent(field1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bookName)
.addComponent(field2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bookNumber)
.addComponent(field3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(itemStock)
.addComponent(field4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(itemPrice)
.addComponent(field5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(restockingFee)
.addComponent(field6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(inventoryValue)
.addComponent(field7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(36, 36, 36)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3)
.addComponent(jButton4)
.addComponent(exitBtm))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/me/myimage/images.jpe"))); // NOI18N
jLabel8.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/org/me/myimage/images.jpe"))); // NOI18N

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel8)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);

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

private void exitBtmActionPerformed(java.awt.event.ActionEvent evt) {
if (JOptionPane.showConfirmDialog(null,"Do you really want to exit Library Iventory ?", "Confirmation", JOptionPane.YES_NO_OPTION)==0);
{
System.exit(0);
}



System.exit(0);

}




/**
* @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(Library.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Library.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Library.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Library.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 Library().setVisible(true);




}

});
}

// Variables declaration - do not modify
private javax.swing.JLabel bookCategory;
private javax.swing.JLabel bookName;
private javax.swing.JLabel bookNumber;
private javax.swing.JButton exitBtm;
private javax.swing.JTextField field1;
private javax.swing.JTextField field2;
private javax.swing.JTextField field3;
private javax.swing.JTextField field4;
private javax.swing.JTextField field5;
private javax.swing.JTextField field6;
private javax.swing.JTextField field7;
private javax.swing.JLabel inventoryValue;
private javax.swing.JLabel itemPrice;
private javax.swing.JLabel itemStock;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel restockingFee;
// End of variables declaration

}

 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, nobody is going to read all that mess. Always use code tags when posting code and make sure it's properly indented in order to make it as easy as possible for others to help you.
 
Kyle Ruey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I knew how to print it with code tags I would. Ive tried to before but I could never get it to work.
 
Kyle Ruey
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i figured it out now.
















 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I deleted some of the posts where you were experimenting with code tags, because there is no point in reading the same code thrice. You will see that the code created by NetBeans' GUI app tends to be particularly illegible, and the excess empty lines do nothing to help. If I have mistakenly deleted something which wasn't duplicated please tell me and I can restore it.

That looks to me as though you are going about things the wrong way. Can you display books by using the library at the command line/terminal? If not you will never get the GUI to work. You should be able to write code likeWhen you can do that, then you can consider a GUI.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!