Created
October 24, 2012 06:12
-
-
Save TabsPH/3944315 to your computer and use it in GitHub Desktop.
Forecasting: 3 Moving Average
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// --------- TABS --------- // | |
import javax.swing.*; | |
import javax.swing.table.*; | |
import javax.swing.text.*; | |
import java.awt.event.*; | |
public class Main extends JFrame implements ActionListener { | |
private JTextField tf[] = new JTextField[12]; | |
private int xAxis = 12; | |
private int yAxis = 12; | |
private String[] months = { "January", "February", "March", "April", "May", "June", | |
"July", "August", "September", "October", "November", "December",}; | |
private GenericTableModel tblModel = new GenericTableModel(); | |
private JTable tbl = new JTable( tblModel ); | |
public static void main(String[] args) { | |
try { | |
Main dialog = new Main(); | |
dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
dialog.setVisible(true); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public Main() { | |
setTitle("Forecasting"); | |
setResizable(false); | |
setBounds(100, 100, 469, 392); | |
getContentPane().setLayout(null); | |
//Set the TextFields and Labels | |
for( int i=0; i<months.length; i++ ) { | |
getContentPane().add( getLabel( months[i], xAxis, yAxis, 80, 15) ); | |
getContentPane().add( getTextField( i, xAxis + 79, yAxis, 55, 19) ); | |
yAxis += 29; | |
} | |
JScrollPane scrollPane = new JScrollPane(); | |
scrollPane.setBounds(162, 12, 293, 299); | |
getContentPane().add(scrollPane); | |
scrollPane.setViewportView( tbl ); | |
JButton btnCompute = new JButton("Compute"); | |
btnCompute.setBounds(349, 323, 106, 25); | |
btnCompute.addActionListener( this ); | |
btnCompute.setActionCommand( "Compute" ); | |
getContentPane().add(btnCompute); | |
} | |
private JLabel getLabel( String caption, int x, int y, int w, int h ) { | |
JLabel lbl = new JLabel( caption ); | |
lbl.setBounds(x, y, w, h); | |
return lbl; | |
} | |
private JTextField getTextField( int index, int x, int y, int w, int h ) { | |
tf[index] = new JTextField(); | |
tf[index].setBounds(x, y, w, h); | |
tf[index].setDocument( new NumberDocument() ); | |
tf[index].addKeyListener( new KeyAdapter() { | |
public void keyPressed(java.awt.event.KeyEvent e) { | |
tblModel.fireTableStructureChanged(); | |
} | |
} ); | |
return tf[index]; | |
} | |
class NumberDocument extends PlainDocument { | |
private static final long serialVersionUID = 1L; | |
@Override | |
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { | |
String stockStr = getText(0, getLength() ); | |
if( (stockStr + str).matches( "[0-9.]{0,10}" ) ) | |
super.insertString(offs, str, a); | |
} | |
} | |
class GenericTableModel extends AbstractTableModel { | |
private static final long serialVersionUID = 1L; | |
private String colName[] = { "Months", "Value", "Forecast" }; | |
@Override | |
public String getColumnName(int col) { | |
return colName[col]; | |
} | |
@Override | |
public int getColumnCount() { | |
return 3; | |
} | |
@Override | |
public int getRowCount() { | |
return 12; | |
} | |
@Override | |
public Object getValueAt(int row, int col) { | |
switch (col) { | |
case 0: | |
return months[row]; | |
case 1: | |
return tf[row].getText(); | |
case 2: | |
if( row > 2 && computeTrigger ) | |
return forecast[row-3]; | |
default: | |
return ""; | |
} | |
} | |
} | |
private double forecast[] = new double[9]; | |
private boolean computeTrigger = false; | |
@Override | |
public void actionPerformed(ActionEvent evt) { | |
if( evt.getActionCommand().equals( "Compute" ) ) { | |
for( int i=0; i<9; i++ ){ | |
double sum = 0; | |
for( int j=i; j<i+3; j++ ) | |
sum += Double.parseDouble("0" + tbl.getValueAt(j, 1).toString() ); | |
forecast[i] = Double.parseDouble( String.format( "%.2f", (sum / 3)) ); | |
computeTrigger = true; | |
} | |
tblModel.fireTableStructureChanged(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment