Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.WindowAdapter;
  6. import java.awt.event.WindowEvent;
  7. import java.io.*;
  8. import java.util.Random;
  9.  
  10. /**
  11.  * Created by Onur on 23.01.2015.
  12.  */
  13. public class Oblig13GUI extends JFrame implements ActionListener {
  14.  
  15.     private JButton btn1 = new JButton("Legg til vare");
  16.     private JButton btn2 = new JButton("Slett vare");
  17.     private JButton btn3 = new JButton("Γ…pne beholdningen");
  18.     private JButton btn4 = new JButton("Lagre beholdningen");
  19.     public static DefaultListModel liste = new DefaultListModel();
  20.     public JList jList = new JList(liste);
  21.     private JScrollPane jscrollPane = new JScrollPane(jList);
  22.  
  23.  
  24.     public Oblig13GUI() {
  25.  
  26.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.         this.setSize(01320, 0x2d0);
  28.         this.addWindowListener(new WindowAdapter() {
  29.  
  30.             public void windowOpened(WindowEvent e) {
  31.                 try {
  32.                     openFile();
  33.                 } catch (Exception e1) {
  34.  
  35.                     e1.printStackTrace();
  36.                 }
  37.             }
  38.  
  39.             public void windowClosing(WindowEvent e) {
  40.                 try {
  41.                     writeFile();
  42.                 } catch (Exception e1) {
  43.                     e1.printStackTrace();
  44.                 }
  45.             }
  46.         });
  47.  
  48.         this.setLayout(new FlowLayout());
  49.         addButtons();
  50.  
  51.         this.setVisible(true);
  52.     }
  53.  
  54.     public void addButtons() {
  55.  
  56.         this.add(btn1);
  57.         this.add(btn2);
  58.         this.add(btn3);
  59.         this.add(btn4);
  60.         btn1.addActionListener(this);
  61.         btn2.addActionListener(this);
  62.         btn3.addActionListener(this);
  63.         btn4.addActionListener(this);
  64.         jscrollPane.setPreferredSize(new Dimension(495, 320));
  65.         this.add(jscrollPane);
  66.     }
  67.  
  68.     @Override
  69.     public void actionPerformed(ActionEvent e) {
  70.  
  71.         if (e.getSource().equals(btn1)) {
  72.             System.out.println(btn2.getText());
  73.             String itemName = JOptionPane.showInputDialog(this, "Item Name: ");
  74.  
  75.             if (!itemName.equals("")) {
  76.                 int itemPrice = Integer.parseInt(JOptionPane.showInputDialog(this, "Item Price: "));
  77.                 String itemLocation = JOptionPane.showInputDialog(this, "Item Location: ");
  78.                 liste.addElement(new Oblig13Vare(itemName, itemLocation, itemPrice));
  79.             } else {
  80.                 int c = new Random().nextInt(3);
  81.                 if (c == 1)
  82.                     liste.addElement(new Oblig13Vare("Asus n550jk", "A30", 5400));
  83.                 else if (c == 2) {
  84.                     liste.addElement(new Oblig13Vare("Lenovo y50", "A31", 8000));
  85.                 } else {
  86.  
  87.                     liste.addElement(new Oblig13Vare("Macbook 2014", "A32", 15030));
  88.  
  89.                 }
  90.             }
  91.  
  92.  
  93.         } else if (e.getSource().equals(btn2)) {
  94.             System.out.println(btn2.getText());
  95.             try {
  96.                 liste.remove(jList.getSelectedIndex());
  97.             } catch (Exception e1) {
  98.                 System.out.println("Could not remove item from array. Did you select an item?");
  99.             }
  100.  
  101.         } else if (e.getSource().equals(btn3)) {
  102.             System.out.println(btn3.getText());
  103.             openFile();
  104.         } else if (e.getSource().equals(btn4)) {
  105.             System.out.println(btn4.getText());
  106.             writeFile();
  107.  
  108.         }
  109.  
  110.     }
  111.  
  112.     private void openFile() {
  113.  
  114.         try {
  115.             FileInputStream fos = new FileInputStream(new File("inventory.txt"));
  116.             ObjectInputStream oos = new ObjectInputStream(fos);
  117.  
  118.             liste = (DefaultListModel) oos.readObject();
  119.             jList.setModel(liste);
  120.  
  121.         } catch (IOException e) {
  122.             e.printStackTrace();
  123.         } catch (ClassNotFoundException e) {
  124.             e.printStackTrace();
  125.         }
  126.  
  127.     }
  128.  
  129.     private void writeFile() {
  130.         try {
  131.             FileOutputStream fos = new FileOutputStream("inventory.txt");
  132.             ObjectOutputStream oos = new ObjectOutputStream(fos);
  133.  
  134.             oos.writeObject(liste);
  135.             oos.close();
  136.  
  137.  
  138.         } catch (IOException e) {
  139.             e.printStackTrace();
  140.         }
  141.     }
  142.  
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement