107 lines
3.4 KiB
Java
107 lines
3.4 KiB
Java
import javax.swing.*;
|
|
import javax.swing.event.ListSelectionEvent;
|
|
import javax.swing.event.ListSelectionListener;
|
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
|
import java.awt.*;
|
|
import java.io.*;
|
|
import java.util.Vector;
|
|
|
|
public class OrderFrame extends JFrame {
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private ProductPanel productPanel = new ProductPanel();
|
|
private JButton buttonOpen = new JButton("Open");
|
|
|
|
private Vector<Product> bla = new Vector();
|
|
private JList list;
|
|
|
|
// TODO: Add property for products
|
|
|
|
public OrderFrame() {
|
|
this.setTitle("Lukas HEiligenbrunner");
|
|
this.add(buttonOpen, BorderLayout.NORTH);
|
|
this.add(productPanel);
|
|
|
|
JFrame _this = this;
|
|
buttonOpen.addActionListener(e -> {
|
|
System.out.println("Btn clicked!");
|
|
|
|
JFileChooser chooser = new JFileChooser(new File("."));
|
|
FileNameExtensionFilter filter = new FileNameExtensionFilter("CSV Images", "csv");
|
|
chooser.setFileFilter(filter);
|
|
|
|
int returnVal = chooser.showOpenDialog(_this);
|
|
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
|
System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
|
|
try {
|
|
openFileAndFillToListBox(chooser.getSelectedFile().getName());
|
|
list.repaint();
|
|
} catch (WrongFileFormatException ee) {
|
|
ee.printStackTrace();
|
|
bla.clear();
|
|
}
|
|
}
|
|
});
|
|
|
|
try {
|
|
openFileAndFillToListBox("products.csv");
|
|
} catch (WrongFileFormatException e) {
|
|
e.printStackTrace();
|
|
bla.clear();
|
|
}
|
|
|
|
|
|
list = new JList(bla);
|
|
list.addListSelectionListener(new ListSelectionListener() {
|
|
@Override
|
|
public void valueChanged(ListSelectionEvent e) {
|
|
|
|
productPanel.setData(bla.get(e.getLastIndex()));
|
|
System.out.println("something was selected" + e.getLastIndex());
|
|
|
|
}
|
|
});
|
|
this.add(list, BorderLayout.WEST);
|
|
|
|
|
|
this.setSize(600, 400);
|
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
this.setVisible(true);
|
|
}
|
|
|
|
public void openFileAndFillToListBox(String filename) {
|
|
bla.clear();
|
|
File myfile = new File(filename);
|
|
try {
|
|
BufferedReader reader = new BufferedReader(new FileReader(myfile));
|
|
|
|
while (reader.ready()) {
|
|
String str = reader.readLine();
|
|
String[] stringsiarri = str.split(";");
|
|
|
|
if (stringsiarri.length != 2) {
|
|
System.out.println("error occured");
|
|
throw new WrongFileFormatException("too many rows");
|
|
}
|
|
|
|
|
|
try {
|
|
Float.parseFloat(stringsiarri[1]);
|
|
} catch (NumberFormatException e) {
|
|
// no float in csv
|
|
throw new WrongFileFormatException("Wrong data type in csv");
|
|
}
|
|
|
|
Product tempproduct = new Product(stringsiarri[0], stringsiarri[1]);
|
|
bla.add(tempproduct);
|
|
}
|
|
|
|
reader.close();
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|