Hi,

My code compiles and runs but the GUI will not display. I have tinkered with it but cannot get it to come up. Someone said I may be miscalling my GUI in this code, help?

Thank you.

import javax.swing.*;

import java.awt.event.*;

public class Application_MainRunInv extends JFrame {

private JTextArea txt;
private Inventory inv;
private int currentDisplay = 0;

public Application_MainRunInv() {
super("Pencil Warehouse");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Prompts program to quit when closed
}

public void init() {
//Creates 3 Pencil Types
Brand p1 = new Brand(120, "Charcoal Pencil", 41, 1.97, "Stanford");
Brand p2 = new Brand(121, "Colored", 82, 1.49, "Stanford");
Brand p3 = new Brand(123, "Number2", 53, .97, "Woodward");

//Creates an inventory for the information
inv = new Inventory();
inv.add(p1);
inv.add(p2);
inv.add(p3);

inv.sort();

//Outputs the above information

for (int i = 0; i < inv.size(); i++) {
System.out.println("Pencil Number: " + inv.get(i).getPencilNumber());
System.out.println("Pencil Name: " + inv.get(i).getPencilName());
System.out.println("Pencil Stock: " + inv.get(i).getPencilStock());
System.out.println("Pencil Cost: $" + String.format("%.2f",inv.get(i).getPencilCost()));
System.out.println("Total Inventory Value: $" + String.format("%.2f",inv.get(i).TotalValue()));
System.out.println("Restocking-Fee: $" + String.format("%.2f",inv.get(i).RestockingFee()));
}


System.out.println("Total Inventory Value: $" + String.format("%.2f",inv.TotalValue()));

//Sets up the interface prior to button creation
JPanel panel = new JPanel();
txt = new JTextArea(15,40);
txt.setEditable(false);
panel.add(txt);

JButton original = new JButton("Original");
original.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentDisplay = 0;
displayPencil();
}
});
panel.add(original);
JButton previous = new JButton("Previous");
previous.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (currentDisplay > 0) currentDisplay--;
else currentDisplay = inv.size()-1;
displayPencil();
}
});
panel.add(previous);
JButton next = new JButton("Next");
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (currentDisplay < inv.size()-1) currentDisplay++;
else currentDisplay = 0;
displayPencil();
}
});
panel.add(next);
JButton end = new JButton("End");
end.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentDisplay = inv.size()-1;
displayPencil();
}
});

panel.add(end);
panel.add(new Graphic());

getContentPane().add(panel);

displayPencil();
}

public void displayPencil() {
txt.setText("Pencil Information:\n");
txt.append("Pencil Number: " + inv.get(currentDisplay).getPencilNumber() + "\n");
txt.append("Pencil Name: " + inv.get(currentDisplay).getPencilName() + "\n");
txt.append("Pencil Stock: " + inv.get(currentDisplay).getPencilStock() + "\n");
txt.append("Pencil Cost: $" + String.format("%.2f",inv.get(currentDisplay).getPencilCost()) + "\n");
txt.append("Total Inventory Value: $" + String.format("%.2f",inv.get(currentDisplay).TotalValue()) + "\n");
txt.append("Restocking-Fee: $" + String.format("%.2f",inv.get(currentDisplay).RestockingFee()) + "\n\n");
txt.append("Total Inventory Value: $" + String.format("%.2f",inv.TotalValue()));


}

public static void main(String args[]) {
Application_MainRunInv gui = new Application_MainRunInv();
gui.pack();
gui.setVisible(true);
}
//
}

Recommended Answers

All 5 Replies

I can't see where you call your init() method.

I can't see where you call your init() method.

Line 16, is that a call to init method?

No, that defines the method but it doesn't call it. You need to change the constructor to call init(), or put it in main like this

Application_MainRunInv gui = new Application_MainRunInv();
gui.init();
gui.pack();
gui.setVisible(true);

No, that defines the method but it doesn't call it. You need to change the constructor to call init(), or put it in main like this

Application_MainRunInv gui = new Application_MainRunInv();
gui.init();
gui.pack();
gui.setVisible(true);

Thank you very much. I input the line; spruced up the image a little bit and it is showing perfectly.

Glad to help. Suggest you mark this thread "solved".

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.