1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| package com.louloux.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import com.louloux.controler.AbstractControler;
import com.louloux.observer.Observer;
public class Window extends JFrame implements Observer {
public static int height = 600;
public static int width = (int) (1.618 * height);
private double[] list = {};
private JPanel container = new JPanel();
private JPanel center = new JPanel();
private JPanel west = new JPanel();
private JButton button = new JButton("Calculate");
String[] title = { "", "", "", "", "", "", "", "Total" };
Double[][] data = { { .0, .0, .0, .0, .0, .0, .0, .0 }, { .0, .0, .0, .0, .0, .0, .0, .0 },
{ .0, .0, .0, .0, .0, .0, .0, .0 }, { .0, .0, .0, .0, .0, .0, .0, .0 },
{ .0, .0, .0, .0, .0, .0, .0, .0 } };
private JTable table = new JTable(data, title);
private JScrollPane scrollPane = new JScrollPane(table);
private AbstractControler controler;
public Window(AbstractControler controler) {
this.setSize(width, height);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
initComponents();
this.controler = controler;
this.setVisible(true);
}
public void initComponents() {
center.setLayout(new BorderLayout());
center.add(scrollPane, BorderLayout.CENTER);
west.add(button);
west.setPreferredSize(new Dimension(150, 600));
west.setBackground(Color.BLUE);
container.setLayout(new BorderLayout());
container.add(west, BorderLayout.WEST);
container.add(center, BorderLayout.CENTER);
this.setContentPane(container);
button.addActionListener(new CalculateListener());
initTable();
}
public void initTable() {
int colWidth = 100;
int rowHeight = 50;
table.setFont(new Font("Arial", Font.BOLD, 15));
TableColumn col;
for (int i = 0; i < table.getColumnCount(); i++) {
col = table.getColumnModel().getColumn(i);
col.setPreferredWidth(colWidth);
}
for (int i = 0; i < table.getRowCount(); i++) {
table.setRowHeight(i, rowHeight);
}
DefaultTableCellRenderer custom = new DefaultTableCellRenderer();
custom.setHorizontalAlignment(JLabel.CENTER);
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumnModel().getColumn(i).setCellRenderer(custom);
}
}
public void calculate() {
double value = 0;
for (int i = 0; i < data[0].length - 1; i++) {
if (table.getValueAt(0, i).equals(null))
value = 0;
else
value = (Double) table.getValueAt(table.convertRowIndexToView(0), table.convertColumnIndexToView(i));
this.list[i] = value;
}
for (double number : list) {
System.out.println(number);
}
// controler.setList(this.list);
}
@Override
public void update(double[] list) {
for (int i = 0; i < list.length; i++) {
table.setValueAt(String.valueOf(list[i]), 0, i);
}
}
// CLASS //
public class CalculateListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
calculate();
}
}
} |