I am trying to have a window open and have it display the text from a file. However when I run it, I simply get a blank window. Here is the code for the window – I get no errors from eclipse at all.
package presentation;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import domain.Items;
import services.exceptions.ItemNotFoundException;
import services.itemservice.*;
public class ShowAllInventory extends JFrame {
/**
*
*/
private static final long serialVersionUID = -4498395613773129897L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ShowAllInventory frame = new ShowAllInventory();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the window frame.
*/
public ShowAllInventory() throws ItemNotFoundException {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
/**
* Load the inventory
*/
IItemsService service = new ItemsServiceImpl();
try {
Items items = service.getItems();
} catch (ItemNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println ("Items Not Found");
}
}
}
Here is the code for the interface the code is calling –
public Items getItems () throws ItemNotFoundException;
}
And here is the implementation…
/**
* Getting Items from the database
* @return
* @throws ItemNotFoundException
*/
@Override
public Items getItems() throws ItemNotFoundException {
Items items = (Items) null;
try {
ObjectInputStream input = new
ObjectInputStream (new FileInputStream("itemdatabase"));
items = (Items)input.readObject();
input.close();
} catch (IOException ioe) {
System.out.println ("IOException");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return items;
}
I don’t know if your
IItemsServicecan load the file properly. But most likely you get a blank window because in your code, I think you forgot to add a component to display the items. If yourItemsworks as aList. You can use aJListto display them.IMHO, I think you should not throw
ItemNotFoundExceptionsince finding no items should be a normal case.FileNotFoundExceptionshould be the one to be thrown here.UPDATED
If you want to use
JTextFieldorJTextAreayou have to something like this: