Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.io.*;
- import java.util.Random;
- /**
- * Created by Onur on 23.01.2015.
- */
- public class Oblig13GUI extends JFrame implements ActionListener {
- private JButton btn1 = new JButton("Legg til vare");
- private JButton btn2 = new JButton("Slett vare");
- private JButton btn3 = new JButton("Γ pne beholdningen");
- private JButton btn4 = new JButton("Lagre beholdningen");
- public static DefaultListModel liste = new DefaultListModel();
- public JList jList = new JList(liste);
- private JScrollPane jscrollPane = new JScrollPane(jList);
- public Oblig13GUI() {
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(01320, 0x2d0);
- this.addWindowListener(new WindowAdapter() {
- public void windowOpened(WindowEvent e) {
- try {
- openFile();
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- }
- public void windowClosing(WindowEvent e) {
- try {
- writeFile();
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- }
- });
- this.setLayout(new FlowLayout());
- addButtons();
- this.setVisible(true);
- }
- public void addButtons() {
- this.add(btn1);
- this.add(btn2);
- this.add(btn3);
- this.add(btn4);
- btn1.addActionListener(this);
- btn2.addActionListener(this);
- btn3.addActionListener(this);
- btn4.addActionListener(this);
- jscrollPane.setPreferredSize(new Dimension(495, 320));
- this.add(jscrollPane);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource().equals(btn1)) {
- System.out.println(btn2.getText());
- String itemName = JOptionPane.showInputDialog(this, "Item Name: ");
- if (!itemName.equals("")) {
- int itemPrice = Integer.parseInt(JOptionPane.showInputDialog(this, "Item Price: "));
- String itemLocation = JOptionPane.showInputDialog(this, "Item Location: ");
- liste.addElement(new Oblig13Vare(itemName, itemLocation, itemPrice));
- } else {
- int c = new Random().nextInt(3);
- if (c == 1)
- liste.addElement(new Oblig13Vare("Asus n550jk", "A30", 5400));
- else if (c == 2) {
- liste.addElement(new Oblig13Vare("Lenovo y50", "A31", 8000));
- } else {
- liste.addElement(new Oblig13Vare("Macbook 2014", "A32", 15030));
- }
- }
- } else if (e.getSource().equals(btn2)) {
- System.out.println(btn2.getText());
- try {
- liste.remove(jList.getSelectedIndex());
- } catch (Exception e1) {
- System.out.println("Could not remove item from array. Did you select an item?");
- }
- } else if (e.getSource().equals(btn3)) {
- System.out.println(btn3.getText());
- openFile();
- } else if (e.getSource().equals(btn4)) {
- System.out.println(btn4.getText());
- writeFile();
- }
- }
- private void openFile() {
- try {
- FileInputStream fos = new FileInputStream(new File("inventory.txt"));
- ObjectInputStream oos = new ObjectInputStream(fos);
- liste = (DefaultListModel) oos.readObject();
- jList.setModel(liste);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- private void writeFile() {
- try {
- FileOutputStream fos = new FileOutputStream("inventory.txt");
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- oos.writeObject(liste);
- oos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement